@@ -451,6 +451,45 @@ func TestContext(t *testing.T) {
451451 assert .Equal (t , 2 , retrySum , "called at most once" )
452452 }()
453453 })
454+
455+ t .Run ("cancelled on retry infinte attempts - wraps context error with last retried function error" , func (t * testing.T ) {
456+ ctx , cancel := context .WithCancel (context .Background ())
457+ defer cancel ()
458+
459+ retrySum := 0
460+ err := Do (
461+ func () error { return fooErr {str : fmt .Sprintf ("error %d" , retrySum + 1 )} },
462+ OnRetry (func (n uint , err error ) {
463+ retrySum += 1
464+ if retrySum == 2 {
465+ cancel ()
466+ }
467+ }),
468+ Context (ctx ),
469+ Attempts (0 ),
470+ WrapContextErrorWithLastError (true ),
471+ )
472+ assert .ErrorIs (t , err , context .Canceled )
473+ assert .ErrorIs (t , err , fooErr {str : "error 2" })
474+ })
475+
476+ t .Run ("timed out on retry infinte attempts - wraps context error with last retried function error" , func (t * testing.T ) {
477+ ctx , cancel := context .WithTimeout (context .Background (), time .Millisecond * 500 )
478+ defer cancel ()
479+
480+ retrySum := 0
481+ err := Do (
482+ func () error { return fooErr {str : fmt .Sprintf ("error %d" , retrySum + 1 )} },
483+ OnRetry (func (n uint , err error ) {
484+ retrySum += 1
485+ }),
486+ Context (ctx ),
487+ Attempts (0 ),
488+ WrapContextErrorWithLastError (true ),
489+ )
490+ assert .ErrorIs (t , err , context .DeadlineExceeded )
491+ assert .ErrorIs (t , err , fooErr {str : "error 2" })
492+ })
454493}
455494
456495type testTimer struct {
0 commit comments