-
Notifications
You must be signed in to change notification settings - Fork 3
/
backoff_test.go
80 lines (75 loc) · 1.41 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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package dbw
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestConstBackoff_Duration(t *testing.T) {
tests := []struct {
name string
b ConstBackoff
attempt int
want time.Duration
}{
{
name: "one",
b: ConstBackoff{DurationMs: 2},
attempt: 1,
want: time.Millisecond * 2,
},
{
name: "two",
b: ConstBackoff{DurationMs: 2},
attempt: 2,
want: time.Millisecond * 2,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert := assert.New(t)
got := tt.b.Duration(uint(tt.attempt))
assert.Equal(tt.want, got)
})
}
}
func TestExpBackoff_Duration(t *testing.T) {
tests := []struct {
name string
b ExpBackoff
attempt int
want time.Duration
wantRand bool
}{
{
name: "one",
b: ExpBackoff{testRand: 1},
attempt: 1,
want: time.Millisecond * 15,
},
{
name: "two",
b: ExpBackoff{testRand: 2},
attempt: 1,
want: time.Millisecond * 25,
},
{
name: "rand",
b: ExpBackoff{},
attempt: 1,
wantRand: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert := assert.New(t)
got := tt.b.Duration(uint(tt.attempt))
if tt.wantRand {
assert.NotZero(got)
return
}
assert.Equal(tt.want, got)
})
}
}