@@ -605,6 +605,48 @@ func TestOperation(t *testing.T) {
605
605
assert .Nil (t , err , "ExecuteExhaust error: %v" , err )
606
606
assert .True (t , conn .CurrentlyStreaming (), "expected CurrentlyStreaming to be true" )
607
607
})
608
+ t .Run ("context deadline exceeded not marked as TransientTransactionError" , func (t * testing.T ) {
609
+ conn := new (mockConnection )
610
+ // Create a context that's already timed out.
611
+ ctx , cancel := context .WithDeadline (context .Background (), time .Unix (893934480 , 0 ))
612
+ defer cancel ()
613
+
614
+ op := Operation {
615
+ Database : "foobar" ,
616
+ Deployment : SingleConnectionDeployment {C : conn },
617
+ CommandFn : func (dst []byte , desc description.SelectedServer ) ([]byte , error ) {
618
+ dst = bsoncore .AppendInt32Element (dst , "ping" , 1 )
619
+ return dst , nil
620
+ },
621
+ }
622
+
623
+ err := op .Execute (ctx , nil )
624
+ assert .NotNil (t , err , "expected an error from Execute(), got nil" )
625
+ // Assert that error is just context deadline exceeded and is therefore not a driver.Error marked
626
+ // with the TransientTransactionError label.
627
+ assert .Equal (t , err , context .DeadlineExceeded , "expected context.DeadlineExceeded error, got %v" , err )
628
+ })
629
+ t .Run ("canceled context not marked as TransientTransactionError" , func (t * testing.T ) {
630
+ conn := new (mockConnection )
631
+ // Create a context and cancel it immediately.
632
+ ctx , cancel := context .WithCancel (context .Background ())
633
+ cancel ()
634
+
635
+ op := Operation {
636
+ Database : "foobar" ,
637
+ Deployment : SingleConnectionDeployment {C : conn },
638
+ CommandFn : func (dst []byte , desc description.SelectedServer ) ([]byte , error ) {
639
+ dst = bsoncore .AppendInt32Element (dst , "ping" , 1 )
640
+ return dst , nil
641
+ },
642
+ }
643
+
644
+ err := op .Execute (ctx , nil )
645
+ assert .NotNil (t , err , "expected an error from Execute(), got nil" )
646
+ // Assert that error is just context canceled and is therefore not a driver.Error marked with
647
+ // the TransientTransactionError label.
648
+ assert .Equal (t , err , context .Canceled , "expected context.Canceled error, got %v" , err )
649
+ })
608
650
}
609
651
610
652
func createExhaustServerResponse (response bsoncore.Document , moreToCome bool ) []byte {
0 commit comments