-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
rehttp_delayfn_test.go
40 lines (35 loc) · 1.11 KB
/
rehttp_delayfn_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
package rehttp
import (
"math"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestConstDelay(t *testing.T) {
want := 2 * time.Second
fn := ConstDelay(want)
for i := 0; i < 5; i++ {
delay := fn(Attempt{Index: i})
assert.Equal(t, want, delay, "%d", i)
}
}
func TestExpJitterDelay(t *testing.T) {
fn := ExpJitterDelay(time.Second, 5*time.Second)
for i := 0; i < 10; i++ {
delay := fn(Attempt{Index: i})
top := math.Pow(2, float64(i)) * float64(time.Second)
actual := time.Duration(math.Min(float64(5*time.Second), top))
assert.True(t, delay > 0, "%d: %s <= 0", i, delay)
assert.True(t, delay <= actual, "%d: %s > %s", i, delay, actual)
}
}
func TestExpJitterDelayWithRand(t *testing.T) {
fn := ExpJitterDelayWithRand(time.Second, 5*time.Second, func(n int64) int64 { return 999_999_999 % n })
for i := 0; i < 10; i++ {
delay := fn(Attempt{Index: i})
top := math.Pow(2, float64(i)) * float64(time.Second)
actual := time.Duration(math.Min(float64(5*time.Second), top))
assert.True(t, delay > 0, "%d: %s <= 0", i, delay)
assert.True(t, delay <= actual, "%d: %s > %s", i, delay, actual)
}
}