@@ -131,12 +131,16 @@ func TestDo_ParentContextCancelled(t *testing.T) {
131131}
132132
133133func TestDo_ZeroValueReturnedOnError (t * testing.T ) {
134- _ , err := Do (context .Background (), defaultOpts (), func (ctx context.Context ) (string , error ) {
134+ result , err := Do (context .Background (), defaultOpts (), func (ctx context.Context ) (string , error ) {
135135 return "partial" , errors .New ("fail" )
136136 })
137137 if err == nil {
138138 t .Fatal ("expected error" )
139139 }
140+
141+ if result != "" {
142+ t .Fatalf ("expected zero value, got %v" , result )
143+ }
140144}
141145
142146func TestDoVoid_Success (t * testing.T ) {
@@ -169,7 +173,7 @@ func TestDoVoid_RetriesAndFails(t *testing.T) {
169173
170174func TestFixedBackoff (t * testing.T ) {
171175 b := FixedBackoff (100 * time .Millisecond )
172- for _ , attempt := range []int {0 , 1 , 2 , 5 } {
176+ for _ , attempt := range []uint {0 , 1 , 2 , 5 } {
173177 if got := b (attempt ); got != 100 * time .Millisecond {
174178 t .Fatalf ("attempt %d: expected 100ms, got %v" , attempt , got )
175179 }
@@ -178,7 +182,7 @@ func TestFixedBackoff(t *testing.T) {
178182
179183func TestLinearBackoff (t * testing.T ) {
180184 b := LinearBackoff (100 * time .Millisecond )
181- cases := map [int ]time.Duration {
185+ cases := map [uint ]time.Duration {
182186 0 : 0 ,
183187 1 : 100 * time .Millisecond ,
184188 2 : 200 * time .Millisecond ,
@@ -193,7 +197,7 @@ func TestLinearBackoff(t *testing.T) {
193197
194198func TestExponentialBackoff (t * testing.T ) {
195199 b := ExponentialBackoff (100 * time .Millisecond )
196- cases := map [int ]time.Duration {
200+ cases := map [uint ]time.Duration {
197201 0 : 100 * time .Millisecond ,
198202 1 : 200 * time .Millisecond ,
199203 2 : 400 * time .Millisecond ,
@@ -216,9 +220,13 @@ func BenchmarkDo_AlwaysSucceeds(b *testing.B) {
216220
217221 b .ResetTimer ()
218222 for b .Loop () {
219- Do (context .Background (), opts , func (ctx context.Context ) (string , error ) {
223+ _ , err := Do (context .Background (), opts , func (ctx context.Context ) (string , error ) {
220224 return "ok" , nil
221225 })
226+
227+ if err != nil {
228+ b .Fatal (err )
229+ }
222230 }
223231}
224232
@@ -232,9 +240,13 @@ func BenchmarkDo_AlwaysFails(b *testing.B) {
232240
233241 b .ResetTimer ()
234242 for b .Loop () {
235- Do (context .Background (), opts , func (ctx context.Context ) (string , error ) {
243+ _ , err := Do (context .Background (), opts , func (ctx context.Context ) (string , error ) {
236244 return "" , errors .New ("fail" )
237245 })
246+
247+ if err == nil {
248+ b .Fatal ("expected error, got nil" )
249+ }
238250 }
239251}
240252
@@ -249,13 +261,17 @@ func BenchmarkDo_SuccessOnLastAttempt(b *testing.B) {
249261 b .ResetTimer ()
250262 for b .Loop () {
251263 attempt := 0
252- Do (context .Background (), opts , func (ctx context.Context ) (string , error ) {
264+ _ , err := Do (context .Background (), opts , func (ctx context.Context ) (string , error ) {
253265 attempt ++
254266 if attempt < 5 {
255267 return "" , errors .New ("transient" )
256268 }
257269 return "ok" , nil
258270 })
271+
272+ if err != nil {
273+ b .Fatal (err )
274+ }
259275 }
260276}
261277
@@ -270,9 +286,13 @@ func BenchmarkDo_ShouldRetryFalse(b *testing.B) {
270286
271287 b .ResetTimer ()
272288 for b .Loop () {
273- Do (context .Background (), opts , func (ctx context.Context ) (string , error ) {
289+ _ , err := Do (context .Background (), opts , func (ctx context.Context ) (string , error ) {
274290 return "" , permanent
275291 })
292+
293+ if ! errors .Is (err , permanent ) {
294+ b .Fatalf ("expected permanent error, got %v" , err )
295+ }
276296 }
277297}
278298
@@ -286,9 +306,13 @@ func BenchmarkDoVoid_AlwaysSucceeds(b *testing.B) {
286306
287307 b .ResetTimer ()
288308 for b .Loop () {
289- DoVoid (context .Background (), opts , func (ctx context.Context ) error {
309+ err := DoVoid (context .Background (), opts , func (ctx context.Context ) error {
290310 return nil
291311 })
312+
313+ if err != nil {
314+ b .Fatal (err )
315+ }
292316 }
293317}
294318
@@ -317,7 +341,7 @@ func BenchmarkExponentialBackoff(b *testing.B) {
317341}
318342
319343func BenchmarkDo_MaxAttempts (b * testing.B ) {
320- for _ , maxAttempts := range []int {1 , 5 , 10 , 50 } {
344+ for _ , maxAttempts := range []uint {1 , 5 , 10 , 50 } {
321345 b .Run (fmt .Sprintf ("attempts=%d" , maxAttempts ), func (b * testing.B ) {
322346 opts := Options {
323347 MaxAttempts : maxAttempts ,
@@ -328,16 +352,20 @@ func BenchmarkDo_MaxAttempts(b *testing.B) {
328352
329353 b .ResetTimer ()
330354 for b .Loop () {
331- Do (context .Background (), opts , func (ctx context.Context ) (string , error ) {
355+ _ , err := Do (context .Background (), opts , func (ctx context.Context ) (string , error ) {
332356 return "" , errors .New ("fail" )
333357 })
358+
359+ if err == nil {
360+ b .Fatal ("expected error, got nil" )
361+ }
334362 }
335363 })
336364 }
337365}
338366
339367func BenchmarkDo_BackoffStrategies (b * testing.B ) {
340- strategies := map [string ]func (int ) time.Duration {
368+ strategies := map [string ]func (uint ) time.Duration {
341369 "fixed" : FixedBackoff (0 ),
342370 "linear" : LinearBackoff (0 ),
343371 "exponential" : ExponentialBackoff (0 ),
@@ -354,9 +382,13 @@ func BenchmarkDo_BackoffStrategies(b *testing.B) {
354382
355383 b .ResetTimer ()
356384 for b .Loop () {
357- Do (context .Background (), opts , func (ctx context.Context ) (string , error ) {
385+ _ , err := Do (context .Background (), opts , func (ctx context.Context ) (string , error ) {
358386 return "" , errors .New ("fail" )
359387 })
388+
389+ if err == nil {
390+ b .Fatal ("expected error, got nil" )
391+ }
360392 }
361393 })
362394 }
0 commit comments