-
Notifications
You must be signed in to change notification settings - Fork 3
/
mild_test.go
64 lines (53 loc) · 1.8 KB
/
mild_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
package backoff
import (
"testing"
"time"
)
func TestNextMILDBackoff(t *testing.T) {
f := MILD()
f.Interval = 1 * time.Second
f.MaxRetries = 5
expectedRetries := []int{1, 2, 3, 4, 5}
expectedDelays := []time.Duration{}
expectedDelays = append(expectedDelays, time.Duration(1*f.Interval))
expectedDelays = append(expectedDelays, time.Duration(expectedDelays[len(expectedDelays)-1]+(expectedDelays[len(expectedDelays)-1]/2)))
expectedDelays = append(expectedDelays, time.Duration(expectedDelays[len(expectedDelays)-1]+(expectedDelays[len(expectedDelays)-1]/2)))
expectedDelays = append(expectedDelays, time.Duration(expectedDelays[len(expectedDelays)-1]+(expectedDelays[len(expectedDelays)-1]/2)))
expectedDelays = append(expectedDelays, time.Duration(expectedDelays[len(expectedDelays)-1]+(expectedDelays[len(expectedDelays)-1]/2)))
expectedDelays = append(expectedDelays, expectedDelays[len(expectedDelays)-1])
expectedDelays = append(expectedDelays, expectedDelays[len(expectedDelays)-1])
for i, expected := range expectedRetries {
f.Next()
assertEquals(t, expected, f.Retries)
assertEquals(t, expectedDelays[i], f.Delay)
}
}
func TestResetMILD(t *testing.T) {
m := MILD()
m.Interval = 1 * time.Second
m.MaxRetries = 5
for i := 0; i < 4; i++ {
m.Next()
}
assertEquals(t, m.Retries, 4)
m.Reset()
assertEquals(t, m.Retries, 0)
assertEquals(t, m.Delay, time.Duration(0*time.Second))
}
func TestDecrementMILD(t *testing.T) {
m := MILD()
m.Interval = 1 * time.Second
m.MaxRetries = 5
m.Next()
assertEquals(t, m.Retries, 1)
assertEquals(t, len(m.Slots), 1)
m.Next()
assertEquals(t, m.Retries, 2)
assertEquals(t, len(m.Slots), 2)
m.decrement()
assertEquals(t, m.Retries, 1)
assertEquals(t, len(m.Slots), 1)
m.decrement()
assertEquals(t, m.Retries, 0)
assertEquals(t, len(m.Slots), 0)
}