Skip to content

Commit d6c31b1

Browse files
guillaumerosepraveenkumar
authored andcommitted
Add tests for errors.RetryAfter
1 parent db8aba4 commit d6c31b1

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

pkg/crc/errors/multierror_test.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package errors
2+
3+
import (
4+
"errors"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
func TestRetryAfter(t *testing.T) {
11+
calls := 0
12+
ret := RetryAfter(10, func() error {
13+
calls++
14+
return nil
15+
}, 0)
16+
assert.NoError(t, ret)
17+
assert.Equal(t, 1, calls)
18+
}
19+
20+
func TestRetryAfterFailure(t *testing.T) {
21+
calls := 0
22+
ret := RetryAfter(10, func() error {
23+
calls++
24+
return errors.New("failed")
25+
}, 0)
26+
assert.EqualError(t, ret, "failed")
27+
assert.Equal(t, 1, calls)
28+
}
29+
30+
func TestRetryAfterMaxAttempts(t *testing.T) {
31+
calls := 0
32+
ret := RetryAfter(3, func() error {
33+
calls++
34+
return &RetriableError{Err: errors.New("failed")}
35+
}, 0)
36+
assert.EqualError(t, ret, "Temporary Error: failed\nTemporary Error: failed\nTemporary Error: failed")
37+
assert.Equal(t, 3, calls)
38+
}
39+
40+
func TestRetryAfterSuccessAfterFailures(t *testing.T) {
41+
calls := 0
42+
ret := RetryAfter(5, func() error {
43+
calls++
44+
if calls < 3 {
45+
return &RetriableError{Err: errors.New("failed")}
46+
}
47+
return nil
48+
}, 0)
49+
assert.NoError(t, ret)
50+
assert.Equal(t, 3, calls)
51+
}

0 commit comments

Comments
 (0)