8
8
"github.com/stretchr/testify/assert"
9
9
)
10
10
11
- func TestDo (t * testing.T ) {
11
+ func TestDoAllFailed (t * testing.T ) {
12
12
var retrySum uint
13
13
err := Do (
14
14
func () error { return errors .New ("test" ) },
@@ -30,17 +30,22 @@ func TestDo(t *testing.T) {
30
30
#10: test`
31
31
assert .Equal (t , expectedErrorFormat , err .Error (), "retry error format" )
32
32
assert .Equal (t , uint (45 ), retrySum , "right count of retry" )
33
+ }
33
34
34
- retrySum = 0
35
- err = Do (
35
+ func TestDoFirstOk (t * testing.T ) {
36
+ var retrySum uint
37
+ err := Do (
36
38
func () error { return nil },
37
39
OnRetry (func (n uint , err error ) { retrySum += n }),
38
40
)
39
41
assert .NoError (t , err )
40
42
assert .Equal (t , uint (0 ), retrySum , "no retry" )
41
43
44
+ }
45
+
46
+ func TestRetryIf (t * testing.T ) {
42
47
var retryCount uint
43
- err = Do (
48
+ err : = Do (
44
49
func () error {
45
50
if retryCount >= 2 {
46
51
return errors .New ("special" )
@@ -56,11 +61,22 @@ func TestDo(t *testing.T) {
56
61
)
57
62
assert .Error (t , err )
58
63
59
- expectedErrorFormat = `All attempts fail:
64
+ expectedErrorFormat : = `All attempts fail:
60
65
#1: test
61
66
#2: test
62
67
#3: special`
63
68
assert .Equal (t , expectedErrorFormat , err .Error (), "retry error format" )
64
69
assert .Equal (t , uint (3 ), retryCount , "right count of retry" )
65
70
66
71
}
72
+
73
+ func TestDefaultSleep (t * testing.T ) {
74
+ start := time .Now ()
75
+ err := Do (
76
+ func () error { return errors .New ("test" ) },
77
+ Attempts (3 ),
78
+ )
79
+ dur := time .Since (start )
80
+ assert .Error (t , err )
81
+ assert .True (t , dur > 10 * time .Millisecond , "3 times default retry is longer then 10ms" )
82
+ }
0 commit comments