-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathexponential_test.go
86 lines (69 loc) · 1.71 KB
/
exponential_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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package backoff
import (
"errors"
"testing"
"time"
)
func TestNextExponentialBackoff(t *testing.T) {
e := Exponential()
e.Interval = 1 * time.Second
e.MaxRetries = 5
expectedRetries := []int{1, 2, 3, 4, 5, 5, 5}
expectedDelays := []time.Duration{1, 3, 7, 15, 31, 31, 31}
for i, v := range expectedDelays {
expectedDelays[i] = v * time.Second
}
for i, expected := range expectedRetries {
e.Next()
assertEquals(t, expected, e.Retries)
assertEquals(t, expectedDelays[i], e.Delay)
}
}
func TestRetryExponential(t *testing.T) {
e := Exponential()
e.Interval = 1 * time.Millisecond
e.MaxRetries = 5
test := func() error {
return errors.New("an error occurred")
}
e.Retry(test)
if e.Retries != e.MaxRetries {
t.Errorf("e.Retries does not match e.MaxRetries: got %d, expected %d", e.Retries, e.MaxRetries)
}
if e.Retries > e.MaxRetries {
t.Errorf("overflow: retries %d greater than maximum retries %d", e.Retries, e.MaxRetries)
}
e.Reset()
test = func() error {
return nil
}
err := e.Retry(test)
if e.Retries > 0 && err != nil {
t.Errorf("failure in retry logic. expected success but got a failure: %+v", err)
}
retries := 0
test = func() error {
if retries == 0 {
retries++
return errors.New("an error occurred")
}
return nil
}
e.Reset()
retries = 0
err = e.Retry(test)
if err != nil {
t.Errorf("failure in retry logic. expected success but got a failure: %+v", err)
}
}
func TestResetExponential(t *testing.T) {
e := Exponential()
e.Interval = 1 * time.Second
e.MaxRetries = 5
e.Next()
assertEquals(t, e.Retries, 1)
assertEquals(t, e.Delay, time.Duration(1*time.Second))
e.Reset()
assertEquals(t, e.Retries, 0)
assertEquals(t, e.Delay, time.Duration(0*time.Second))
}