|
| 1 | +package backoff |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "math/rand" |
| 6 | + "testing" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/stretchr/testify/require" |
| 10 | +) |
| 11 | + |
| 12 | +func TestDelays(t *testing.T) { |
| 13 | + duration := func(s string) (d time.Duration) { |
| 14 | + d, err := time.ParseDuration(s) |
| 15 | + if err != nil { |
| 16 | + panic(err) |
| 17 | + } |
| 18 | + return d |
| 19 | + } |
| 20 | + b := New( |
| 21 | + WithSlotDuration(duration("500ms")), |
| 22 | + WithCeiling(6), |
| 23 | + WithJitterLimit(1), |
| 24 | + ) |
| 25 | + for i, d := range map[int]time.Duration{ |
| 26 | + 0: duration("500ms"), |
| 27 | + 1: duration("1s"), |
| 28 | + 2: duration("2s"), |
| 29 | + 3: duration("4s"), |
| 30 | + 4: duration("8s"), |
| 31 | + 5: duration("16s"), |
| 32 | + 6: duration("32s"), |
| 33 | + 7: duration("32s"), |
| 34 | + 8: duration("32s"), |
| 35 | + } { |
| 36 | + t.Run(fmt.Sprintf("%v -> %v", i, d), func(t *testing.T) { |
| 37 | + require.Equal(t, d, b.Delay(i)) |
| 38 | + }) |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +func TestLogBackoff(t *testing.T) { |
| 43 | + type exp struct { |
| 44 | + eq time.Duration |
| 45 | + gte time.Duration |
| 46 | + lte time.Duration |
| 47 | + } |
| 48 | + for _, test := range []struct { |
| 49 | + name string |
| 50 | + backoff Backoff |
| 51 | + exp []exp |
| 52 | + seeds int64 |
| 53 | + }{ |
| 54 | + { |
| 55 | + backoff: New( |
| 56 | + WithSlotDuration(time.Second), |
| 57 | + WithCeiling(3), |
| 58 | + WithJitterLimit(0), |
| 59 | + ), |
| 60 | + 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) |
| 68 | + }, |
| 69 | + seeds: 1000, |
| 70 | + }, |
| 71 | + { |
| 72 | + backoff: New( |
| 73 | + WithSlotDuration(time.Second), |
| 74 | + WithCeiling(3), |
| 75 | + WithJitterLimit(0.5), |
| 76 | + ), |
| 77 | + 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) |
| 85 | + }, |
| 86 | + seeds: 1000, |
| 87 | + }, |
| 88 | + { |
| 89 | + backoff: New( |
| 90 | + WithSlotDuration(time.Second), |
| 91 | + WithCeiling(3), |
| 92 | + WithJitterLimit(1), |
| 93 | + ), |
| 94 | + 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) |
| 102 | + }, |
| 103 | + }, |
| 104 | + } { |
| 105 | + t.Run(test.name, func(t *testing.T) { |
| 106 | + if test.seeds == 0 { |
| 107 | + test.seeds = 1 |
| 108 | + } |
| 109 | + for seed := int64(0); seed < test.seeds; seed++ { |
| 110 | + // Fix random to reproduce the tests. |
| 111 | + rand.Seed(seed) |
| 112 | + |
| 113 | + for n, exp := range test.exp { |
| 114 | + act := test.backoff.Delay(n) |
| 115 | + if exp := exp.eq; exp != 0 { |
| 116 | + if exp != act { |
| 117 | + t.Fatalf( |
| 118 | + "unexpected Backoff delay: %s; want %s", |
| 119 | + act, exp, |
| 120 | + ) |
| 121 | + } |
| 122 | + continue |
| 123 | + } |
| 124 | + if gte := exp.gte; act < gte { |
| 125 | + t.Errorf( |
| 126 | + "unexpected Backoff delay: %s; want >= %s", |
| 127 | + act, gte, |
| 128 | + ) |
| 129 | + } |
| 130 | + if lte := exp.lte; act > lte { |
| 131 | + t.Errorf( |
| 132 | + "unexpected Backoff delay: %s; want <= %s", |
| 133 | + act, lte, |
| 134 | + ) |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | + }) |
| 139 | + } |
| 140 | +} |
0 commit comments