| 1 |
package retrier |
| 2 |
|
| 3 |
import ( |
| 4 |
"testing" |
| 5 |
"time" |
| 6 |
) |
| 7 |
|
| 8 |
func TestConstantBackoff(t *testing.T) { |
| 9 |
b := ConstantBackoff(1, 10*time.Millisecond) |
| 10 |
if len(b) != 1 { |
| 11 |
t.Error("incorrect length") |
| 12 |
} |
| 13 |
for i := range b { |
| 14 |
if b[i] != 10*time.Millisecond { |
| 15 |
t.Error("incorrect value at", i) |
| 16 |
} |
| 17 |
} |
| 18 |
|
| 19 |
b = ConstantBackoff(10, 250*time.Hour) |
| 20 |
if len(b) != 10 { |
| 21 |
t.Error("incorrect length") |
| 22 |
} |
| 23 |
for i := range b { |
| 24 |
if b[i] != 250*time.Hour { |
| 25 |
t.Error("incorrect value at", i) |
| 26 |
} |
| 27 |
} |
| 28 |
} |
| 29 |
|
| 30 |
func TestExponentialBackoff(t *testing.T) { |
| 31 |
b := ExponentialBackoff(1, 10*time.Millisecond) |
| 32 |
if len(b) != 1 { |
| 33 |
t.Error("incorrect length") |
| 34 |
} |
| 35 |
if b[0] != 10*time.Millisecond { |
| 36 |
t.Error("incorrect value") |
| 37 |
} |
| 38 |
|
| 39 |
b = ExponentialBackoff(4, 1*time.Minute) |
| 40 |
if len(b) != 4 { |
| 41 |
t.Error("incorrect length") |
| 42 |
} |
| 43 |
if b[0] != 1*time.Minute { |
| 44 |
t.Error("incorrect value") |
| 45 |
} |
| 46 |
if b[1] != 2*time.Minute { |
| 47 |
t.Error("incorrect value") |
| 48 |
} |
| 49 |
if b[2] != 4*time.Minute { |
| 50 |
t.Error("incorrect value") |
| 51 |
} |
| 52 |
if b[3] != 8*time.Minute { |
| 53 |
t.Error("incorrect value") |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
func TestLimitedExponentialBackoff(t *testing.T) { |
| 58 |
b := LimitedExponentialBackoff(1, 10*time.Millisecond, 11*time.Millisecond) |
| 59 |
if len(b) != 1 { |
| 60 |
t.Error("incorrect length") |
| 61 |
} |
| 62 |
if b[0] != 10*time.Millisecond { |
| 63 |
t.Error("incorrect value") |
| 64 |
} |
| 65 |
|
| 66 |
b = LimitedExponentialBackoff(5, 1*time.Minute, 4*time.Minute) |
| 67 |
if len(b) != 5 { |
| 68 |
t.Error("incorrect length") |
| 69 |
} |
| 70 |
if b[0] != 1*time.Minute { |
| 71 |
t.Error("incorrect value") |
| 72 |
} |
| 73 |
if b[1] != 2*time.Minute { |
| 74 |
t.Error("incorrect value") |
| 75 |
} |
| 76 |
if b[2] != 4*time.Minute { |
| 77 |
t.Error("incorrect value") |
| 78 |
} |
| 79 |
if b[3] != 4*time.Minute { |
| 80 |
t.Error("incorrect value") |
| 81 |
} |
| 82 |
if b[4] != 4*time.Minute { |
| 83 |
t.Error("incorrect value") |
| 84 |
} |
| 85 |
} |