@@ -35,7 +35,7 @@ func TestDoWithDataAllFailed(t *testing.T) {
3535 assert .Len (t , err , 10 )
3636 fmt .Println (err .Error ())
3737 assert .Equal (t , expectedErrorFormat , err .Error (), "retry error format" )
38- assert .Equal (t , uint (45 ), retrySum , "right count of retry" )
38+ assert .Equal (t , uint (36 ), retrySum , "right count of retry" )
3939}
4040
4141func TestDoFirstOk (t * testing.T ) {
@@ -632,6 +632,28 @@ func BenchmarkDoWithDataNoErrors(b *testing.B) {
632632 }
633633}
634634
635+ func TestOnRetryNotCalledOnLastAttempt (t * testing.T ) {
636+ callCount := 0
637+ onRetryCalls := make ([]uint , 0 )
638+
639+ err := Do (
640+ func () error {
641+ callCount ++
642+ return errors .New ("test error" )
643+ },
644+ Attempts (3 ),
645+ OnRetry (func (n uint , err error ) {
646+ onRetryCalls = append (onRetryCalls , n )
647+ }),
648+ Delay (time .Nanosecond ),
649+ )
650+
651+ assert .Error (t , err )
652+ assert .Equal (t , 3 , callCount , "function should be called 3 times" )
653+ assert .Equal (t , []uint {0 , 1 }, onRetryCalls , "onRetry should only be called for first 2 attempts, not the final one" )
654+ assert .Len (t , onRetryCalls , 2 , "onRetry should be called exactly 2 times (not on last attempt)" )
655+ }
656+
635657func TestIsRecoverable (t * testing.T ) {
636658 err := errors .New ("err" )
637659 assert .True (t , IsRecoverable (err ))
0 commit comments