-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbackoff_test.go
110 lines (96 loc) · 2.68 KB
/
backoff_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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package stubborn
import (
"reflect"
"testing"
"time"
)
func TestBackoff_Duration(t *testing.T) {
b := &backoff{
Min: 100 * time.Millisecond,
Max: 10 * time.Second,
Exponentiation: 2,
}
equals(t, b.Duration(), 100*time.Millisecond)
equals(t, b.Duration(), 200*time.Millisecond)
equals(t, b.Duration(), 400*time.Millisecond)
equals(t, b.Duration(), 800*time.Millisecond)
b.Reset()
equals(t, b.Duration(), 100*time.Millisecond)
}
func TestBackoff_Duration2(t *testing.T) {
b := &backoff{
Min: 100 * time.Millisecond,
Max: 10 * time.Second,
Exponentiation: 1.5,
}
equals(t, b.Duration(), 100*time.Millisecond)
equals(t, b.Duration(), 150*time.Millisecond)
equals(t, b.Duration(), 225*time.Millisecond)
b.Reset()
equals(t, b.Duration(), 100*time.Millisecond)
}
func TestBackoff_Duration3(t *testing.T) {
b := &backoff{
Min: 100 * time.Nanosecond,
Max: 10 * time.Second,
Exponentiation: 1.75,
}
equals(t, b.Duration(), 100*time.Nanosecond)
equals(t, b.Duration(), 175*time.Nanosecond)
equals(t, b.Duration(), 306*time.Nanosecond)
b.Reset()
equals(t, b.Duration(), 100*time.Nanosecond)
}
func TestBackoff_Max(t *testing.T) {
b := &backoff{
Min: 500 * time.Second,
Max: 100 * time.Second,
Exponentiation: 1,
}
equals(t, b.Duration(), b.Max)
}
func TestBackoff_Attempt(t *testing.T) {
b := &backoff{
Min: 100 * time.Millisecond,
Max: 10 * time.Second,
Exponentiation: 2,
}
equals(t, b.Attempt(), float64(0))
equals(t, b.Duration(), 100*time.Millisecond)
equals(t, b.Attempt(), float64(1))
equals(t, b.Duration(), 200*time.Millisecond)
equals(t, b.Attempt(), float64(2))
equals(t, b.Duration(), 400*time.Millisecond)
equals(t, b.Attempt(), float64(3))
b.Reset()
equals(t, b.Attempt(), float64(0))
equals(t, b.Duration(), 100*time.Millisecond)
equals(t, b.Attempt(), float64(1))
}
func TestBackoff_Jitter(t *testing.T) {
b := &backoff{
Min: 100 * time.Millisecond,
Max: 10 * time.Second,
Exponentiation: 2,
Jitter: true,
}
equals(t, b.Duration(), 100*time.Millisecond)
between(t, b.Duration(), 100*time.Millisecond, 200*time.Millisecond)
between(t, b.Duration(), 100*time.Millisecond, 400*time.Millisecond)
b.Reset()
equals(t, b.Duration(), 100*time.Millisecond)
}
func between(t *testing.T, actual, low, high time.Duration) {
if actual < low {
t.Fatalf("Got %s, Expecting >= %s", actual, low)
}
if actual > high {
t.Fatalf("Got %s, Expecting <= %s", actual, high)
}
}
func equals(t *testing.T, v1, v2 interface{}) {
if !reflect.DeepEqual(v1, v2) {
t.Logf("Got %v, Expecting %v", v1, v2)
t.Fail()
}
}