Debian Codegraph @

tries_test.go

raw
1 //go:build ignore
2
3 package backoff
4
5 import (
6 "errors"
7 "math/rand/v2"
8 "testing"
9 "time"
10 )
11
12 func TestMaxTriesHappy(t *testing.T) {
13 max := 17 + rand.IntN(13)
14 bo := WithMaxRetries(&ZeroBackOff{}, uint64(max))
15
16 // Load up the tries count, but reset should clear the record
17 for ix := 0; ix < max/2; ix++ {
18 bo.NextBackOff()
19 }
20 bo.Reset()
21
22 // Now fill the tries count all the way up
23 for ix := 0; ix < max; ix++ {
24 d := bo.NextBackOff()
25 if d == Stop {
26 t.Errorf("returned Stop on try %d", ix)
27 }
28 }
29
30 // We have now called the BackOff max number of times, we expect
31 // the next result to be Stop, even if we try it multiple times
32 for ix := 0; ix < 7; ix++ {
33 d := bo.NextBackOff()
34 if d != Stop {
35 t.Error("invalid next back off")
36 }
37 }
38
39 // Reset makes it all work again
40 bo.Reset()
41 d := bo.NextBackOff()
42 if d == Stop {
43 t.Error("returned Stop after reset")
44 }
45 }
46
47 // https://github.com/cenkalti/backoff/issues/80
48 func TestMaxTriesZero(t *testing.T) {
49 var called int
50
51 b := WithMaxRetries(&ZeroBackOff{}, 0)
52
53 err := Retry(func() error {
54 called++
55 return errors.New("err")
56 }, b)
57
58 if err == nil {
59 t.Errorf("error expected, nil found")
60 }
61 if called != 1 {
62 t.Errorf("operation is called %d times", called)
63 }
64 }