@@ -67,9 +67,8 @@ func (s *ClientTest) getLangfuseClientForTest(promptName, promptContent string)
67
67
"commitMessage" : null,
68
68
"resolutionGraph" : null
69
69
}` , promptContent , promptName )
70
- return golangfuse .NewWithHttpClient (
71
- httpmock .NewMockClient (http .StatusOK , apiResponse ),
72
- "https://langfuse3.data.divar.cloud" , "pk" , "sk" )
70
+ return golangfuse .New (s .ctx , "https://langfuse3.data.divar.cloud" , "pk" , "sk" ,
71
+ golangfuse .WithHTTPClient (httpmock .NewMockClient (http .StatusOK , apiResponse )))
73
72
}
74
73
75
74
func (s * ClientTest ) TestShouldSetBasicAuth () {
@@ -91,19 +90,6 @@ func (s *ClientTest) TestShouldSetBasicAuth() {
91
90
)
92
91
}
93
92
94
- func (s * ClientTest ) TestStartSendingShouldReturnErrorIfAlreadyStarted () {
95
- // Given
96
- c := s .getClient ()
97
- err := c .StartSendingEvents (s .ctx , 1 * time .Microsecond )
98
- s .Require ().NoError (err )
99
-
100
- // When
101
- err = c .StartSendingEvents (s .ctx , 1 * time .Microsecond )
102
-
103
- // Then
104
- s .Require ().ErrorContains (err , "already started" )
105
- }
106
-
107
93
func (s * ClientTest ) TestShouldSendEvent () {
108
94
// Given
109
95
var sentRequestBody []byte
@@ -118,8 +104,6 @@ func (s *ClientTest) TestShouldSendEvent() {
118
104
sentRequestBody = body
119
105
return & http.Response {}, nil
120
106
})
121
- err := c .StartSendingEvents (s .ctx , 1 * time .Microsecond )
122
- s .Require ().NoError (err )
123
107
124
108
// When
125
109
c .Trace ("input" , "output" )
@@ -135,7 +119,7 @@ func (s *ClientTest) TestShouldSendEvent() {
135
119
} `json:"batch"`
136
120
}
137
121
bodyObj := requestBody {}
138
- err = json .Unmarshal (sentRequestBody , & bodyObj )
122
+ err : = json .Unmarshal (sentRequestBody , & bodyObj )
139
123
s .Require ().NoError (err )
140
124
s .Require ().Len (bodyObj .Batch , 1 )
141
125
s .Require ().Equal ("input" , bodyObj .Batch [0 ].Body .Input )
@@ -145,15 +129,13 @@ func (s *ClientTest) TestShouldSendEvent() {
145
129
func (s * ClientTest ) TestShouldNotSendAnythingWhenNoEventIsReported () {
146
130
// Given
147
131
httpCallHappened := false
148
- c := s .getClientWithMockedHttpTransport (func (req * http.Request ) (* http.Response , error ) {
132
+ s .getClientWithMockedHttpTransport (func (req * http.Request ) (* http.Response , error ) {
149
133
httpCallHappened = true
150
134
return & http.Response {}, nil
151
135
})
152
136
153
137
// When
154
- err := c .StartSendingEvents (s .ctx , 1 * time .Microsecond )
155
- s .Require ().NoError (err )
156
- time .Sleep (1 * time .Millisecond )
138
+ time .Sleep (2 * time .Millisecond )
157
139
158
140
// Then
159
141
s .Require ().False (httpCallHappened , "http call happened, unexpectedly" )
@@ -173,8 +155,6 @@ func (s *ClientTest) TestShouldSendEventsInBatch() {
173
155
sentRequestBody = body
174
156
return & http.Response {}, nil
175
157
})
176
- err := c .StartSendingEvents (s .ctx , 1 * time .Microsecond )
177
- s .Require ().NoError (err )
178
158
179
159
// When
180
160
c .Trace ("input" , "output" )
@@ -186,7 +166,7 @@ func (s *ClientTest) TestShouldSendEventsInBatch() {
186
166
Batch []struct {} `json:"batch"`
187
167
}
188
168
bodyObj := requestBody {}
189
- err = json .Unmarshal (sentRequestBody , & bodyObj )
169
+ err : = json .Unmarshal (sentRequestBody , & bodyObj )
190
170
s .Require ().NoError (err )
191
171
s .Require ().Len (bodyObj .Batch , 2 )
192
172
}
@@ -200,21 +180,24 @@ func (s *ClientTest) getClient() golangfuse.Langfuse {
200
180
})
201
181
}
202
182
203
- func (s * ClientTest ) getClientWithMockedHttpTransport (transport httpmock.RoundTripFunc ) golangfuse.Langfuse {
204
- return golangfuse .NewWithHttpClient (
205
- & http.Client {Transport : transport },
183
+ func (s * ClientTest ) getClientWithMockedHttpTransport (
184
+ transport httpmock.RoundTripFunc ,
185
+ opts ... golangfuse.Option ,
186
+ ) golangfuse.Langfuse {
187
+ opts = append (opts , golangfuse .WithHTTPClient (& http.Client {Transport : transport }))
188
+ return golangfuse .New (
189
+ s .ctx ,
206
190
"https://test.com" ,
207
191
"test-pk" ,
208
192
"test-sk" ,
193
+ opts ... ,
209
194
)
210
195
}
211
196
212
197
func (s * ClientTest ) TestShutdownShouldReturnErrorWhenAlreadyShutdown () {
213
198
// Given
214
199
c := s .getClient ()
215
- err := c .StartSendingEvents (s .ctx , 1 * time .Microsecond )
216
- s .Require ().NoError (err )
217
- err = c .Shutdown (s .ctx )
200
+ err := c .Shutdown (s .ctx )
218
201
s .Require ().NoError (err )
219
202
220
203
// When
@@ -224,14 +207,12 @@ func (s *ClientTest) TestShutdownShouldReturnErrorWhenAlreadyShutdown() {
224
207
s .Require ().ErrorContains (err , "already shutdown" )
225
208
}
226
209
227
- func (s * ClientTest ) TestShutdownShouldSucceedAfterStart () {
210
+ func (s * ClientTest ) TestShutdownShouldSucceedInHappyPath () {
228
211
// Given
229
212
c := s .getClient ()
230
- err := c .StartSendingEvents (s .ctx , 1 * time .Microsecond )
231
- s .Require ().NoError (err )
232
213
233
214
// When
234
- err = c .Shutdown (s .ctx )
215
+ err : = c .Shutdown (s .ctx )
235
216
236
217
// Then
237
218
s .Require ().NoError (err )
@@ -253,13 +234,13 @@ func (s *ClientTest) TestShutdownShouldFlushPendingEvents() {
253
234
StatusCode : http .StatusMultiStatus ,
254
235
Body : io .NopCloser (strings .NewReader ("{}" )),
255
236
}, nil
256
- })
257
- err := c . StartSendingEvents ( s . ctx , 1 * time .Hour ) // Long period to prevent automatic Flush
258
- s . Require (). NoError ( err )
237
+ },
238
+ golangfuse . WithFlushPeriod ( 1 * time .Hour ), // Long period to prevent automatic Flush
239
+ )
259
240
260
241
// When
261
242
c .Trace ("input" , "output" )
262
- err = c .Shutdown (s .ctx )
243
+ err : = c .Shutdown (s .ctx )
263
244
s .Require ().NoError (err )
264
245
wg .Wait ()
265
246
@@ -280,21 +261,6 @@ func (s *ClientTest) TestShutdownShouldFlushPendingEvents() {
280
261
s .Require ().Equal ("output" , bodyObj .Batch [0 ].Body .Output )
281
262
}
282
263
283
- func (s * ClientTest ) TestStartSendingEventsShouldReturnErrorAfterShutdown () {
284
- // Given
285
- c := s .getClient ()
286
- err := c .StartSendingEvents (s .ctx , 1 * time .Microsecond )
287
- s .Require ().NoError (err )
288
- err = c .Shutdown (s .ctx )
289
- s .Require ().NoError (err )
290
-
291
- // When
292
- err = c .StartSendingEvents (s .ctx , 1 * time .Microsecond )
293
-
294
- // Then
295
- s .Require ().ErrorContains (err , "already shutdown" )
296
- }
297
-
298
264
func TestLangfuseClient (t * testing.T ) {
299
265
suite .Run (t , new (ClientTest ))
300
266
}
0 commit comments