-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathretry_test.go
62 lines (49 loc) · 950 Bytes
/
retry_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package commander
import (
"errors"
"testing"
)
// TestRetry tries to retry a method x amount of times
func TestRetry(t *testing.T) {
retry := Retry{
Amount: 10,
}
err := retry.Attempt(func() error {
return nil
})
if err != nil {
t.Error(err)
}
}
// TestRetryFail tests if a retry attempt is failing
func TestRetryFail(t *testing.T) {
retry := Retry{
Amount: 1,
}
err := retry.Attempt(func() error {
return errors.New("failed")
})
if err == nil {
t.Error("No error is thrown")
}
}
// TestRetryNotFail tests if a retry attempt is not failing after the second attempt
func TestRetryNotFail(t *testing.T) {
retry := Retry{
Amount: 5,
}
count := 0
err := retry.Attempt(func() error {
count++
if count == 2 {
return nil
}
return errors.New("failed")
})
if err != nil {
t.Error("A error is thrown")
}
if retry.Retries != 1 {
t.Errorf("Retry did not retry 1 time but: %d", retry.Retries)
}
}