53 lines
1.6 KiB
Go
53 lines
1.6 KiB
Go
package outboxrelay
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestBackoffDoublesFromInitialToTheCap(t *testing.T) {
|
|
noJitter := func(time.Duration) time.Duration { return 0 }
|
|
fullJitter := func(limit time.Duration) time.Duration { return limit }
|
|
backoff := Backoff{Initial: 500 * time.Millisecond, Max: 5 * time.Second}
|
|
|
|
ceilings := []time.Duration{
|
|
500 * time.Millisecond, // failure 1
|
|
time.Second,
|
|
2 * time.Second,
|
|
4 * time.Second,
|
|
5 * time.Second, // capped
|
|
5 * time.Second,
|
|
}
|
|
for index, ceiling := range ceilings {
|
|
failures := index + 1
|
|
backoff.jitter = noJitter
|
|
if got := backoff.Delay(failures); got != ceiling/2 {
|
|
t.Fatalf("Delay(%d) without jitter = %s, want %s", failures, got, ceiling/2)
|
|
}
|
|
backoff.jitter = fullJitter
|
|
if got := backoff.Delay(failures); got != ceiling {
|
|
t.Fatalf("Delay(%d) with full jitter = %s, want %s", failures, got, ceiling)
|
|
}
|
|
}
|
|
backoff.jitter = fullJitter
|
|
if got := backoff.Delay(0); got != 500*time.Millisecond {
|
|
t.Fatalf("Delay(0) = %s, want the first delay", got)
|
|
}
|
|
if got := backoff.Delay(1 << 20); got != 5*time.Second {
|
|
t.Fatalf("Delay(many) = %s, want the cap", got)
|
|
}
|
|
}
|
|
|
|
func TestBackoffRandomJitterStaysWithinTheCeiling(t *testing.T) {
|
|
backoff := Backoff{Initial: 100 * time.Millisecond, Max: time.Second}
|
|
for failures := 1; failures <= 8; failures++ {
|
|
ceiling := min(100*time.Millisecond<<(failures-1), time.Second)
|
|
for range 50 {
|
|
delay := backoff.Delay(failures)
|
|
if delay < ceiling/2 || delay > ceiling {
|
|
t.Fatalf("Delay(%d) = %s, want between %s and %s", failures, delay, ceiling/2, ceiling)
|
|
}
|
|
}
|
|
}
|
|
}
|