-
Notifications
You must be signed in to change notification settings - Fork 0
/
backoff_test.go
60 lines (44 loc) · 1.39 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
package boomerang
import (
"github.com/stretchr/testify/assert"
"testing"
"time"
)
func TestExponentialBackoffNextInterval(t *testing.T) {
eb := NewExponentialBackoff(2*time.Millisecond, 10*time.Millisecond, 2.0)
assert.Equal(t, 4*time.Millisecond, eb.NextInterval(1))
}
func TestExponentialBackoffMaxOverflow(t *testing.T) {
eb := NewExponentialBackoff(2*time.Millisecond, 10*time.Millisecond, 2.0)
assert.Equal(t, 10*time.Millisecond, eb.NextInterval(4))
}
func TestConstantBackoffNextInterval(t *testing.T) {
cb := NewConstantBackoff(10 * time.Millisecond)
assert.Equal(t, 10*time.Millisecond, cb.NextInterval(1))
assert.Equal(t, 10*time.Millisecond, cb.NextInterval(10))
}
func TestJitterBackoffMaxOverflow(t *testing.T) {
jb := NewJitterBackoff(2*time.Millisecond, 10*time.Millisecond, 2.0)
assert.Equal(t, 10*time.Millisecond, jb.NextInterval(10))
}
func TestJitterBackoffNextInterval(t *testing.T) {
var dur time.Duration
low := 10 * time.Millisecond
jb := NewJitterBackoff(low, 3*time.Second, 2.0)
dur = jb.NextInterval(1)
assert.Equal(t, between(t, dur, low, 20*time.Millisecond), true)
dur = jb.NextInterval(2)
assert.Equal(t, between(t, dur, low, 50*time.Millisecond), true)
}
func between(t *testing.T, val, low, high time.Duration) bool {
if val < low {
return false
}
if val > high {
return false
}
if val >= low && val <= high {
return true
}
return false
}