@@ -348,9 +348,36 @@ func TestConvenientTransactions(t *testing.T) {
348
348
return nil
349
349
})
350
350
})
351
- t .Run ("commitTransaction timeout does not retry" , func (t * testing.T ) {
351
+ t .Run ("context error before commitTransaction does not retry and aborts " , func (t * testing.T ) {
352
352
withTransactionTimeout = 2 * time .Second
353
353
354
+ // Create a special CommandMonitor that only records information about abortTransaction events.
355
+ var abortStarted []* event.CommandStartedEvent
356
+ var abortSucceeded []* event.CommandSucceededEvent
357
+ var abortFailed []* event.CommandFailedEvent
358
+ monitor := & event.CommandMonitor {
359
+ Started : func (ctx context.Context , evt * event.CommandStartedEvent ) {
360
+ if evt .CommandName == "abortTransaction" {
361
+ abortStarted = append (abortStarted , evt )
362
+ }
363
+ },
364
+ Succeeded : func (_ context.Context , evt * event.CommandSucceededEvent ) {
365
+ if evt .CommandName == "abortTransaction" {
366
+ abortSucceeded = append (abortSucceeded , evt )
367
+ }
368
+ },
369
+ Failed : func (_ context.Context , evt * event.CommandFailedEvent ) {
370
+ if evt .CommandName == "abortTransaction" {
371
+ abortFailed = append (abortFailed , evt )
372
+ }
373
+ },
374
+ }
375
+
376
+ // Set up a new Client using the command monitor defined above get a handle to a collection. The collection
377
+ // needs to be explicitly created on the server because implicit collection creation is not allowed in
378
+ // transactions for server versions <= 4.2.
379
+ client := setupConvenientTransactions (t , options .Client ().SetMonitor (monitor ))
380
+ db := client .Database ("foo" )
354
381
coll := db .Collection ("test" )
355
382
// Explicitly create the collection on server because implicit collection creation is not allowed in
356
383
// transactions for server versions <= 4.2.
@@ -377,7 +404,8 @@ func TestConvenientTransactions(t *testing.T) {
377
404
}
378
405
}()
379
406
380
- // Insert a document within a session and manually cancel context.
407
+ // Insert a document within a session and manually cancel context before
408
+ // "commitTransaction" can be sent.
381
409
callback := func (ctx context.Context ) {
382
410
transactionCtx , cancel := context .WithCancel (ctx )
383
411
@@ -391,6 +419,12 @@ func TestConvenientTransactions(t *testing.T) {
391
419
392
420
// Assert that transaction is canceled within 500ms and not 2 seconds.
393
421
assert .Soon (t , callback , 500 * time .Millisecond )
422
+
423
+ // Assert that AbortTransaction was started once and succeeded.
424
+ assert .Equal (t , 1 , len (abortStarted ), "expected 1 abortTransaction started event, got %d" , len (abortStarted ))
425
+ assert .Equal (t , 1 , len (abortSucceeded ), "expected 1 abortTransaction succeeded event, got %d" ,
426
+ len (abortSucceeded ))
427
+ assert .Equal (t , 0 , len (abortFailed ), "expected 0 abortTransaction failed events, got %d" , len (abortFailed ))
394
428
})
395
429
t .Run ("wrapped transient transaction error retried" , func (t * testing.T ) {
396
430
sess , err := client .StartSession ()
@@ -416,7 +450,7 @@ func TestConvenientTransactions(t *testing.T) {
416
450
assert .True (t , ok , "expected result type %T, got %T" , false , res )
417
451
assert .False (t , resBool , "expected result false, got %v" , resBool )
418
452
})
419
- t .Run ("expired context before commitTransaction does not retry" , func (t * testing.T ) {
453
+ t .Run ("expired context before callback does not retry" , func (t * testing.T ) {
420
454
withTransactionTimeout = 2 * time .Second
421
455
422
456
coll := db .Collection ("test" )
@@ -446,7 +480,7 @@ func TestConvenientTransactions(t *testing.T) {
446
480
// Assert that transaction fails within 500ms and not 2 seconds.
447
481
assert .Soon (t , callback , 500 * time .Millisecond )
448
482
})
449
- t .Run ("canceled context before commitTransaction does not retry" , func (t * testing.T ) {
483
+ t .Run ("canceled context before callback does not retry" , func (t * testing.T ) {
450
484
withTransactionTimeout = 2 * time .Second
451
485
452
486
coll := db .Collection ("test" )
@@ -476,7 +510,7 @@ func TestConvenientTransactions(t *testing.T) {
476
510
// Assert that transaction fails within 500ms and not 2 seconds.
477
511
assert .Soon (t , callback , 500 * time .Millisecond )
478
512
})
479
- t .Run ("slow operation before commitTransaction retries" , func (t * testing.T ) {
513
+ t .Run ("slow operation in callback retries" , func (t * testing.T ) {
480
514
withTransactionTimeout = 2 * time .Second
481
515
482
516
coll := db .Collection ("test" )
0 commit comments