@@ -464,6 +464,45 @@ func TestContext(t *testing.T) {
464464 assert .Equal (t , 2 , retrySum , "called at most once" )
465465 }()
466466 })
467+
468+ t .Run ("cancelled on retry infinte attempts - wraps context error with last retried function error" , func (t * testing.T ) {
469+ ctx , cancel := context .WithCancel (context .Background ())
470+ defer cancel ()
471+
472+ retrySum := 0
473+ err := Do (
474+ func () error { return fooErr {str : fmt .Sprintf ("error %d" , retrySum + 1 )} },
475+ OnRetry (func (n uint , err error ) {
476+ retrySum += 1
477+ if retrySum == 2 {
478+ cancel ()
479+ }
480+ }),
481+ Context (ctx ),
482+ Attempts (0 ),
483+ WrapContextErrorWithLastError (true ),
484+ )
485+ assert .ErrorIs (t , err , context .Canceled )
486+ assert .ErrorIs (t , err , fooErr {str : "error 2" })
487+ })
488+
489+ t .Run ("timed out on retry infinte attempts - wraps context error with last retried function error" , func (t * testing.T ) {
490+ ctx , cancel := context .WithTimeout (context .Background (), time .Millisecond * 500 )
491+ defer cancel ()
492+
493+ retrySum := 0
494+ err := Do (
495+ func () error { return fooErr {str : fmt .Sprintf ("error %d" , retrySum + 1 )} },
496+ OnRetry (func (n uint , err error ) {
497+ retrySum += 1
498+ }),
499+ Context (ctx ),
500+ Attempts (0 ),
501+ WrapContextErrorWithLastError (true ),
502+ )
503+ assert .ErrorIs (t , err , context .DeadlineExceeded )
504+ assert .ErrorIs (t , err , fooErr {str : "error 2" })
505+ })
467506}
468507
469508type testTimer struct {
0 commit comments