This repository has been archived by the owner on Feb 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbackoff_test.go
97 lines (81 loc) · 2.13 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
package rex
import (
"testing"
"time"
. "github.com/smartystreets/goconvey/convey"
)
func TestBackoff(t *testing.T) {
Convey("should backoff properly", t, func() {
var b BackoffCallback
correct := []int{0, 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192}
var result []int = make([]int, 0)
for i := 0; i < 10000; i++ {
b.Step(func() {
result = append(result, i)
})
}
So(result, ShouldResemble, correct)
})
}
func Test1(t *testing.T) {
b := &BackoffDuration{
Min: 100 * time.Millisecond,
Max: 10 * time.Second,
Factor: 2,
}
equals(t, b.Duration(), 100*time.Millisecond)
equals(t, b.Duration(), 200*time.Millisecond)
equals(t, b.Duration(), 400*time.Millisecond)
b.Reset()
equals(t, b.Duration(), 100*time.Millisecond)
}
func Test2(t *testing.T) {
b := &BackoffDuration{
Min: 100 * time.Millisecond,
Max: 10 * time.Second,
Factor: 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 Test3(t *testing.T) {
b := &BackoffDuration{
Min: 100 * time.Nanosecond,
Max: 10 * time.Second,
Factor: 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 TestJitter(t *testing.T) {
b := &BackoffDuration{
Min: 100 * time.Millisecond,
Max: 10 * time.Second,
Factor: 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, d1, d2 time.Duration) {
if d1 != d2 {
t.Fatalf("Got %s, Expecting %s", d1, d2)
}
}