forked from Diggs/go-backoff
-
Notifications
You must be signed in to change notification settings - Fork 0
/
backoff_test.go
42 lines (37 loc) · 994 Bytes
/
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
package backoff
import (
"github.com/stretchr/testify/assert"
"math"
"testing"
"time"
)
func TestExponentialBackoff(t *testing.T) {
exp := NewExponential(time.Millisecond, 0)
for i := 0; i < 5; i++ {
assert.Equal(t, int64(float64(time.Millisecond)*math.Pow(2, float64(i))), exp.NextDuration)
exp.Backoff()
}
}
func TestExponentialLimit(t *testing.T) {
exp := NewExponential(time.Millisecond, 4*time.Millisecond)
for i := 0; i < 5; i++ {
exp.Backoff()
}
assert.Equal(t, 5, exp.count)
assert.Equal(t, 4*time.Millisecond, exp.NextDuration)
}
func TestLinearBackoff(t *testing.T) {
lin := NewLinear(time.Millisecond, 0)
for i := 0; i < 5; i++ {
assert.Equal(t, int64(float64(time.Millisecond)*float64(i)), lin.NextDuration)
lin.Backoff()
}
}
func TestLinearLimit(t *testing.T) {
lin := NewLinear(time.Millisecond, 4*time.Millisecond)
for i := 0; i < 5; i++ {
lin.Backoff()
}
assert.Equal(t, 5, lin.count)
assert.Equal(t, 4*time.Millisecond, lin.NextDuration)
}