@@ -316,3 +316,102 @@ func TestQueue_SubscribeUnprocessedTasks(t *testing.T) {
316
316
})
317
317
}
318
318
}
319
+
320
+ func TestQueue_Ack (t * testing.T ) {
321
+ setNowFunc (func () time.Time {
322
+ t , _ := time .Parse (time .DateTime , "2024-10-12 15:04:05" )
323
+ return t
324
+ })
325
+
326
+ tests := []struct {
327
+ name string
328
+ taskId string
329
+ error error
330
+ }{
331
+ {
332
+ name : "Success" ,
333
+ taskId : "67211cb175b7564a5cd9ce3f" ,
334
+ },
335
+ {
336
+ name : "InvalidObjectId" ,
337
+ taskId : "xxx" ,
338
+ },
339
+ }
340
+
341
+ for _ , tt := range tests {
342
+ t .Run (tt .name , func (t * testing.T ) {
343
+ dbMock := NewDbInterfaceMock (t )
344
+
345
+ q := NewQueue (dbMock )
346
+
347
+ oId , err := primitive .ObjectIDFromHex (tt .taskId )
348
+
349
+ if err == nil {
350
+ dbMock .EXPECT ().UpdateOne (
351
+ bson.M {"_id" : oId },
352
+ bson.M {"$set" : bson.M {
353
+ "state" : StateCompleted ,
354
+ "meta.completed" : nowFunc (),
355
+ }}).Return (tt .error )
356
+ }
357
+
358
+ err = q .Ack (tt .taskId )
359
+
360
+ if tt .name == "InvalidObjectId" {
361
+ assert .Equal (t , "the provided hex string is not a valid ObjectID" , err .Error ())
362
+ } else {
363
+ assert .Equal (t , tt .error , err )
364
+ }
365
+ })
366
+ }
367
+ }
368
+
369
+ func TestQueue_Err (t * testing.T ) {
370
+ setNowFunc (func () time.Time {
371
+ t , _ := time .Parse (time .DateTime , "2024-10-12 15:04:05" )
372
+ return t
373
+ })
374
+
375
+ tests := []struct {
376
+ name string
377
+ taskId string
378
+ error error
379
+ }{
380
+ {
381
+ name : "Success" ,
382
+ taskId : "67211cb175b7564a5cd9ce3f" ,
383
+ },
384
+ {
385
+ name : "InvalidObjectId" ,
386
+ taskId : "xxx" ,
387
+ },
388
+ }
389
+
390
+ for _ , tt := range tests {
391
+ t .Run (tt .name , func (t * testing.T ) {
392
+ dbMock := NewDbInterfaceMock (t )
393
+
394
+ q := NewQueue (dbMock )
395
+
396
+ oId , err := primitive .ObjectIDFromHex (tt .taskId )
397
+
398
+ if err == nil {
399
+ dbMock .EXPECT ().UpdateOne (
400
+ bson.M {"_id" : oId },
401
+ bson.M {"$set" : bson.M {
402
+ "state" : StateError ,
403
+ "meta.completed" : nowFunc (),
404
+ "message" : "some error" ,
405
+ }}).Return (tt .error )
406
+ }
407
+
408
+ err = q .Err (tt .taskId , errors .New ("some error" ))
409
+
410
+ if tt .name == "InvalidObjectId" {
411
+ assert .Equal (t , "the provided hex string is not a valid ObjectID" , err .Error ())
412
+ } else {
413
+ assert .Equal (t , tt .error , err )
414
+ }
415
+ })
416
+ }
417
+ }
0 commit comments