@@ -209,6 +209,103 @@ func (s *ClientTest) getClientWithMockedHttpTransport(transport httpmock.RoundTr
209
209
)
210
210
}
211
211
212
+ func (s * ClientTest ) TestShutdownShouldReturnErrorWhenNotStarted () {
213
+ // Given
214
+ c := s .getClient ()
215
+
216
+ // When
217
+ err := c .Shutdown (s .ctx )
218
+
219
+ // Then
220
+ s .Require ().ErrorContains (err , "not started" )
221
+ }
222
+
223
+ func (s * ClientTest ) TestShutdownShouldReturnErrorWhenAlreadyShutdown () {
224
+ // Given
225
+ c := s .getClient ()
226
+ err := c .StartSendingEvents (s .ctx , 1 * time .Microsecond )
227
+ s .Require ().NoError (err )
228
+ err = c .Shutdown (s .ctx )
229
+ s .Require ().NoError (err )
230
+
231
+ // When
232
+ err = c .Shutdown (s .ctx )
233
+
234
+ // Then
235
+ s .Require ().ErrorContains (err , "already shutdown" )
236
+ }
237
+
238
+ func (s * ClientTest ) TestShutdownShouldSucceedAfterStart () {
239
+ // Given
240
+ c := s .getClient ()
241
+ err := c .StartSendingEvents (s .ctx , 1 * time .Microsecond )
242
+ s .Require ().NoError (err )
243
+
244
+ // When
245
+ err = c .Shutdown (s .ctx )
246
+
247
+ // Then
248
+ s .Require ().NoError (err )
249
+ }
250
+
251
+ func (s * ClientTest ) TestShutdownShouldFlushPendingEvents () {
252
+ // Given
253
+ var sentRequestBody []byte
254
+ wg := & sync.WaitGroup {}
255
+ wg .Add (1 )
256
+ c := s .getClientWithMockedHttpTransport (func (req * http.Request ) (* http.Response , error ) {
257
+ defer wg .Done ()
258
+ body , err := io .ReadAll (req .Body )
259
+ if err != nil {
260
+ return nil , err
261
+ }
262
+ sentRequestBody = body
263
+ return & http.Response {
264
+ StatusCode : http .StatusMultiStatus ,
265
+ Body : io .NopCloser (strings .NewReader ("{}" )),
266
+ }, nil
267
+ })
268
+ err := c .StartSendingEvents (s .ctx , 1 * time .Hour ) // Long period to prevent automatic Flush
269
+ s .Require ().NoError (err )
270
+
271
+ // When
272
+ c .Trace ("input" , "output" )
273
+ err = c .Shutdown (s .ctx )
274
+ s .Require ().NoError (err )
275
+ wg .Wait ()
276
+
277
+ // Then
278
+ type requestBody struct {
279
+ Batch []struct {
280
+ Body struct {
281
+ Input string `json:"input"`
282
+ Output string `json:"output"`
283
+ }
284
+ } `json:"batch"`
285
+ }
286
+ bodyObj := requestBody {}
287
+ err = json .Unmarshal (sentRequestBody , & bodyObj )
288
+ s .Require ().NoError (err )
289
+ s .Require ().Len (bodyObj .Batch , 1 )
290
+ s .Require ().Equal ("input" , bodyObj .Batch [0 ].Body .Input )
291
+ s .Require ().Equal ("output" , bodyObj .Batch [0 ].Body .Output )
292
+ }
293
+
294
+ func (s * ClientTest ) TestStartSendingEventsShouldReturnErrorAfterShutdown () {
295
+ // Given
296
+ c := s .getClient ()
297
+ err := c .StartSendingEvents (s .ctx , 1 * time .Microsecond )
298
+ s .Require ().NoError (err )
299
+ err = c .Shutdown (s .ctx )
300
+ s .Require ().NoError (err )
301
+
302
+ // When
303
+ err = c .StartSendingEvents (s .ctx , 1 * time .Microsecond )
304
+
305
+ // Then
306
+ s .Require ().ErrorContains (err , "already shutdown" )
307
+ }
308
+
212
309
func TestLangfuseClient (t * testing.T ) {
213
310
suite .Run (t , new (ClientTest ))
214
311
}
0 commit comments