-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpolicies_test.go
More file actions
152 lines (132 loc) · 4.35 KB
/
policies_test.go
File metadata and controls
152 lines (132 loc) · 4.35 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package goretry
import (
"testing"
"time"
)
func TestFixedDelayPolicy(t *testing.T) {
policy := NewFixedDelayPolicy(100 * time.Millisecond)
for attempt := 1; attempt <= 5; attempt++ {
delay, shouldContinue := policy.NextDelay(attempt)
if !shouldContinue {
t.Errorf("Expected to continue at attempt %d", attempt)
}
if delay != 100*time.Millisecond {
t.Errorf("Expected 100ms delay, got %v", delay)
}
}
}
func TestExponentialBackoffPolicy(t *testing.T) {
policy := NewExponentialBackoffPolicy(100*time.Millisecond, 1*time.Second).WithJitter(false)
expectedDelays := []time.Duration{
100 * time.Millisecond,
200 * time.Millisecond,
400 * time.Millisecond,
800 * time.Millisecond,
1000 * time.Millisecond, // capped at max
}
for attempt := 1; attempt <= len(expectedDelays); attempt++ {
delay, shouldContinue := policy.NextDelay(attempt)
if !shouldContinue {
t.Errorf("Expected to continue at attempt %d", attempt)
}
if delay != expectedDelays[attempt-1] {
t.Errorf("Attempt %d: expected %v, got %v", attempt, expectedDelays[attempt-1], delay)
}
}
}
func TestExponentialBackoffPolicy_WithCustomMultiplier(t *testing.T) {
policy := NewExponentialBackoffPolicy(100*time.Millisecond, 1*time.Second).
WithMultiplier(3.0).
WithJitter(false)
expectedDelays := []time.Duration{
100 * time.Millisecond,
300 * time.Millisecond,
900 * time.Millisecond,
1000 * time.Millisecond, // capped at max
}
for attempt := 1; attempt <= len(expectedDelays); attempt++ {
delay, shouldContinue := policy.NextDelay(attempt)
if !shouldContinue {
t.Errorf("Expected to continue at attempt %d", attempt)
}
if delay != expectedDelays[attempt-1] {
t.Errorf("Attempt %d: expected %v, got %v", attempt, expectedDelays[attempt-1], delay)
}
}
}
func TestLinearBackoffPolicy(t *testing.T) {
policy := NewLinearBackoffPolicy(100*time.Millisecond, 50*time.Millisecond, 300*time.Millisecond)
expectedDelays := []time.Duration{
100 * time.Millisecond,
150 * time.Millisecond,
200 * time.Millisecond,
250 * time.Millisecond,
300 * time.Millisecond, // capped at max
300 * time.Millisecond, // still capped
}
for attempt := 1; attempt <= len(expectedDelays); attempt++ {
delay, shouldContinue := policy.NextDelay(attempt)
if !shouldContinue {
t.Errorf("Expected to continue at attempt %d", attempt)
}
if delay != expectedDelays[attempt-1] {
t.Errorf("Attempt %d: expected %v, got %v", attempt, expectedDelays[attempt-1], delay)
}
}
}
func TestNoDelayPolicy(t *testing.T) {
policy := NewNoDelayPolicy()
for attempt := 1; attempt <= 5; attempt++ {
delay, shouldContinue := policy.NextDelay(attempt)
if !shouldContinue {
t.Errorf("Expected to continue at attempt %d", attempt)
}
if delay != 0 {
t.Errorf("Expected 0 delay, got %v", delay)
}
}
}
func TestStopPolicy_MaxAttempts(t *testing.T) {
basePolicy := NewFixedDelayPolicy(10 * time.Millisecond)
policy := NewStopPolicy(basePolicy).WithMaxAttempts(3)
for attempt := 1; attempt <= 3; attempt++ {
delay, shouldContinue := policy.NextDelay(attempt)
if attempt < 3 {
if !shouldContinue {
t.Errorf("Expected to continue at attempt %d", attempt)
}
if delay != 10*time.Millisecond {
t.Errorf("Expected 10ms delay, got %v", delay)
}
} else {
if shouldContinue {
t.Errorf("Expected to stop at attempt %d", attempt)
}
}
}
}
func TestStopPolicy_MaxDuration(t *testing.T) {
basePolicy := NewFixedDelayPolicy(10 * time.Millisecond)
policy := NewStopPolicy(basePolicy).WithMaxDuration(50 * time.Millisecond)
// Test that policy correctly reports max duration
if policy.GetMaxDuration() != 50*time.Millisecond {
t.Errorf("Expected max duration 50ms, got %v", policy.GetMaxDuration())
}
// Duration checking is now handled by the retrier, so test NextDelay behavior
delay, shouldContinue := policy.NextDelay(1)
if !shouldContinue {
t.Error("Expected to continue at first attempt")
}
if delay != 10*time.Millisecond {
t.Errorf("Expected 10ms delay, got %v", delay)
}
// Policy no longer tracks time internally - that's handled by retrier
// This ensures thread safety and proper timing per retry operation
delay, shouldContinue = policy.NextDelay(2)
if !shouldContinue {
t.Error("Expected policy to continue (duration checking moved to retrier)")
}
if delay != 10*time.Millisecond {
t.Errorf("Expected 10ms delay, got %v", delay)
}
}