-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathclient_test.go
More file actions
45 lines (39 loc) · 1.3 KB
/
client_test.go
File metadata and controls
45 lines (39 loc) · 1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package main
import (
"testing"
"time"
)
func TestCalculateRetryDelay_Jitter(t *testing.T) {
// Run multiple times to verify jitter adds randomness
for attempt := 0; attempt < 3; attempt++ {
baseDelay := baseRetryDelay * (1 << uint(attempt))
if baseDelay > maxRetryDelay {
baseDelay = maxRetryDelay
}
delays := make(map[time.Duration]bool)
for i := 0; i < 100; i++ {
d := calculateRetryDelay(attempt)
delays[d] = true
// Verify delay is within expected range: [baseDelay, baseDelay + baseDelay/2)
if d < baseDelay {
t.Errorf("attempt %d: delay %v is less than base delay %v", attempt, d, baseDelay)
}
maxExpected := baseDelay + baseDelay/2
if d >= maxExpected {
t.Errorf("attempt %d: delay %v exceeds max expected %v", attempt, d, maxExpected)
}
}
// With 100 samples, we should see more than 1 unique value (jitter is random)
if len(delays) < 2 {
t.Errorf("attempt %d: expected multiple unique delay values due to jitter, got %d", attempt, len(delays))
}
}
}
func TestCalculateRetryDelay_Cap(t *testing.T) {
// Very high attempt number should cap at maxRetryDelay + jitter
d := calculateRetryDelay(100)
maxExpected := maxRetryDelay + maxRetryDelay/2
if d >= maxExpected {
t.Errorf("delay %v exceeds max expected %v for high attempt", d, maxExpected)
}
}