@@ -3,6 +3,7 @@ package backoff
33import (
44 "fmt"
55 "math/rand"
6+ "strconv"
67 "testing"
78 "time"
89
@@ -45,8 +46,7 @@ func TestLogBackoff(t *testing.T) {
4546 gte time.Duration
4647 lte time.Duration
4748 }
48- for _ , test := range []struct {
49- name string
49+ for _ , tt := range []struct {
5050 backoff Backoff
5151 exp []exp
5252 seeds int64
@@ -58,13 +58,13 @@ func TestLogBackoff(t *testing.T) {
5858 WithJitterLimit (0 ),
5959 ),
6060 exp : []exp {
61- {gte : 0 , lte : time .Second }, // 1 << min(0, 3)
62- {gte : 0 , lte : 2 * time .Second }, // 1 << min(1, 3)
63- {gte : 0 , lte : 4 * time .Second }, // 1 << min(2, 3)
64- {gte : 0 , lte : 8 * time .Second }, // 1 << min(3, 3)
65- {gte : 0 , lte : 8 * time .Second }, // 1 << min(4, 3)
66- {gte : 0 , lte : 8 * time .Second }, // 1 << min(5, 3)
67- {gte : 0 , lte : 8 * time .Second }, // 1 << min(6, 3)
61+ {gte : 0 , lte : time .Second },
62+ {gte : 0 , lte : 2 * time .Second },
63+ {gte : 0 , lte : 4 * time .Second },
64+ {gte : 0 , lte : 8 * time .Second },
65+ {gte : 0 , lte : 8 * time .Second },
66+ {gte : 0 , lte : 8 * time .Second },
67+ {gte : 0 , lte : 8 * time .Second },
6868 },
6969 seeds : 1000 ,
7070 },
@@ -75,13 +75,13 @@ func TestLogBackoff(t *testing.T) {
7575 WithJitterLimit (0.5 ),
7676 ),
7777 exp : []exp {
78- {gte : 500 * time .Millisecond , lte : time .Second }, // 1 << min(0, 3)
79- {gte : 1 * time .Second , lte : 2 * time .Second }, // 1 << min(1, 3)
80- {gte : 2 * time .Second , lte : 4 * time .Second }, // 1 << min(2, 3)
81- {gte : 4 * time .Second , lte : 8 * time .Second }, // 1 << min(3, 3)
82- {gte : 4 * time .Second , lte : 8 * time .Second }, // 1 << min(4, 3)
83- {gte : 4 * time .Second , lte : 8 * time .Second }, // 1 << min(5, 3)
84- {gte : 4 * time .Second , lte : 8 * time .Second }, // 1 << min(6, 3)
78+ {gte : 500 * time .Millisecond , lte : time .Second },
79+ {gte : 1 * time .Second , lte : 2 * time .Second },
80+ {gte : 2 * time .Second , lte : 4 * time .Second },
81+ {gte : 4 * time .Second , lte : 8 * time .Second },
82+ {gte : 4 * time .Second , lte : 8 * time .Second },
83+ {gte : 4 * time .Second , lte : 8 * time .Second },
84+ {gte : 4 * time .Second , lte : 8 * time .Second },
8585 },
8686 seeds : 1000 ,
8787 },
@@ -92,13 +92,13 @@ func TestLogBackoff(t *testing.T) {
9292 WithJitterLimit (1 ),
9393 ),
9494 exp : []exp {
95- {eq : time .Second }, // 1 << min(0, 3)
96- {eq : 2 * time .Second }, // 1 << min(1, 3)
97- {eq : 4 * time .Second }, // 1 << min(2, 3)
98- {eq : 8 * time .Second }, // 1 << min(3, 3)
99- {eq : 8 * time .Second }, // 1 << min(4, 3)
100- {eq : 8 * time .Second }, // 1 << min(5, 3)
101- {eq : 8 * time .Second }, // 1 << min(6, 3)
95+ {eq : time .Second },
96+ {eq : 2 * time .Second },
97+ {eq : 4 * time .Second },
98+ {eq : 8 * time .Second },
99+ {eq : 8 * time .Second },
100+ {eq : 8 * time .Second },
101+ {eq : 8 * time .Second },
102102 },
103103 },
104104 {
@@ -108,47 +108,47 @@ func TestLogBackoff(t *testing.T) {
108108 WithJitterLimit (1 ),
109109 ),
110110 exp : []exp {
111- {eq : time .Second }, // 1 << min(0, 3)
112- {eq : 2 * time .Second }, // 1 << min(1, 3)
113- {eq : 4 * time .Second }, // 1 << min(2, 3)
114- {eq : 8 * time .Second }, // 1 << min(3, 3)
115- {eq : 16 * time .Second }, // 1 << min(4, 3)
116- {eq : 32 * time .Second }, // 1 << min(5, 3)
117- {eq : 64 * time .Second }, // 1 << min(6, 3)
118- {eq : 64 * time .Second }, // 1 << min(6, 3)
119- {eq : 64 * time .Second }, // 1 << min(6, 3)
120- {eq : 64 * time .Second }, // 1 << min(6, 3)
121- {eq : 64 * time .Second }, // 1 << min(6, 3)
122- {eq : 64 * time .Second }, // 1 << min(6, 3)
111+ {eq : time .Second },
112+ {eq : 2 * time .Second },
113+ {eq : 4 * time .Second },
114+ {eq : 8 * time .Second },
115+ {eq : 16 * time .Second },
116+ {eq : 32 * time .Second },
117+ {eq : 64 * time .Second },
118+ {eq : 64 * time .Second },
119+ {eq : 64 * time .Second },
120+ {eq : 64 * time .Second },
121+ {eq : 64 * time .Second },
122+ {eq : 64 * time .Second },
123123 },
124124 },
125125 } {
126- t .Run (test . name , func (t * testing.T ) {
127- if test .seeds == 0 {
128- test .seeds = 1
126+ t .Run ("" , func (t * testing.T ) {
127+ if tt .seeds == 0 {
128+ tt .seeds = 1
129129 }
130- for seed := int64 (0 ); seed < test .seeds ; seed ++ {
130+ for seed := int64 (0 ); seed < tt .seeds ; seed ++ {
131131 // Fix random to reproduce the tests.
132132 rand .Seed (seed )
133133
134- for n , exp := range test .exp {
135- act := test .backoff .Delay (n )
136- if exp := exp .eq ; exp != 0 {
137- if exp != act {
134+ for n , exp := range tt .exp {
135+ act := tt .backoff .Delay (n )
136+ if eq := exp .eq ; eq != 0 {
137+ if eq != act {
138138 t .Fatalf (
139139 "unexpected Backoff delay: %s; want %s" ,
140- act , exp ,
140+ act , eq ,
141141 )
142142 }
143143 continue
144144 }
145- if gte := exp .gte ; act < gte {
145+ if gte := exp .gte ; act <= gte {
146146 t .Errorf (
147147 "unexpected Backoff delay: %s; want >= %s" ,
148148 act , gte ,
149149 )
150150 }
151- if lte := exp .lte ; act > lte {
151+ if lte := exp .lte ; act >= lte {
152152 t .Errorf (
153153 "unexpected Backoff delay: %s; want <= %s" ,
154154 act , lte ,
@@ -159,3 +159,65 @@ func TestLogBackoff(t *testing.T) {
159159 })
160160 }
161161}
162+
163+ func TestFastSlowDelaysWithoutJitter (t * testing.T ) {
164+ type exp struct {
165+ }
166+ for _ , tt := range []struct {
167+ name string
168+ backoff Backoff
169+ exp []time.Duration
170+ }{
171+ {
172+ name : "FastBackoff" ,
173+ backoff : func () (backoff logBackoff ) {
174+ backoff = Fast
175+ backoff .jitterLimit = 1
176+ return backoff
177+ }(),
178+ exp : []time.Duration {
179+ 5 * time .Millisecond ,
180+ 10 * time .Millisecond ,
181+ 20 * time .Millisecond ,
182+ 40 * time .Millisecond ,
183+ 80 * time .Millisecond ,
184+ 160 * time .Millisecond ,
185+ 320 * time .Millisecond ,
186+ 320 * time .Millisecond ,
187+ 320 * time .Millisecond ,
188+ 320 * time .Millisecond ,
189+ 320 * time .Millisecond ,
190+ },
191+ },
192+ {
193+ name : "SlowBackoff" ,
194+ backoff : func () (backoff logBackoff ) {
195+ backoff = Slow
196+ backoff .jitterLimit = 1
197+ return backoff
198+ }(),
199+ exp : []time.Duration {
200+ time .Second ,
201+ 2 * time .Second ,
202+ 4 * time .Second ,
203+ 8 * time .Second ,
204+ 16 * time .Second ,
205+ 32 * time .Second ,
206+ 64 * time .Second ,
207+ 64 * time .Second ,
208+ 64 * time .Second ,
209+ 64 * time .Second ,
210+ 64 * time .Second ,
211+ },
212+ },
213+ } {
214+ t .Run (tt .name , func (t * testing.T ) {
215+ for n , exp := range tt .exp {
216+ t .Run ("delay#" + strconv .Itoa (n ), func (t * testing.T ) {
217+ act := tt .backoff .Delay (n )
218+ require .Equal (t , exp , act )
219+ })
220+ }
221+ })
222+ }
223+ }
0 commit comments