forked from mtusi2026/mtusi2026lab6
30 lines
559 B
Go
30 lines
559 B
Go
package jobs
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestRetryBackoffJitterAndCap(t *testing.T) {
|
|
for attempt := 1; attempt <= 12; attempt++ {
|
|
base := time.Second
|
|
for i := 1; i < attempt && base < 60*time.Second; i++ {
|
|
base *= 2
|
|
}
|
|
if base > 60*time.Second {
|
|
base = 60 * time.Second
|
|
}
|
|
values := map[time.Duration]bool{}
|
|
for i := 0; i < 50; i++ {
|
|
d := RetryDelay(attempt)
|
|
if d < base/2 || d > base {
|
|
t.Fatalf("attempt=%d delay=%s", attempt, d)
|
|
}
|
|
values[d] = true
|
|
}
|
|
if len(values) < 2 {
|
|
t.Fatal("jitter absent")
|
|
}
|
|
}
|
|
}
|