-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconvenience_test.go
More file actions
134 lines (109 loc) · 2.87 KB
/
Copy pathconvenience_test.go
File metadata and controls
134 lines (109 loc) · 2.87 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
package goretry
import (
"context"
"errors"
"testing"
"time"
)
func TestIfNeeded_Success(t *testing.T) {
callCount := 0
err := IfNeeded(func() error {
callCount++
if callCount < 3 {
return &testError{message: "transient error", timeout: true}
}
return nil
})
if err != nil {
t.Errorf("Expected success, got error: %v", err)
}
if callCount != 3 {
t.Errorf("Expected 3 calls, got %d", callCount)
}
}
func TestIfNeeded_WithCustomOptions(t *testing.T) {
callCount := 0
err := IfNeeded(func() error {
callCount++
return &testError{message: "transient error", timeout: true}
}, WithMaxAttempts(2))
var outOfRetriesErr *OutOfRetriesError
if !errors.As(err, &outOfRetriesErr) {
t.Errorf("Expected OutOfRetriesError, got: %T", err)
}
if callCount != 2 {
t.Errorf("Expected 2 calls, got %d", callCount)
}
}
func TestIfNeededWithContext_Success(t *testing.T) {
ctx := context.Background()
callCount := 0
err := IfNeededWithContext(ctx, func(ctx context.Context) error {
callCount++
if callCount < 2 {
return &testError{message: "transient error", timeout: true}
}
return nil
})
if err != nil {
t.Errorf("Expected success, got error: %v", err)
}
if callCount != 2 {
t.Errorf("Expected 2 calls, got %d", callCount)
}
}
func TestIfNeededWithContext_Cancellation(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
go func() {
time.Sleep(10 * time.Millisecond)
cancel()
}()
err := IfNeededWithContext(ctx, func(ctx context.Context) error {
time.Sleep(20 * time.Millisecond) // This will be interrupted
return &testError{message: "transient error", timeout: true}
})
if !errors.Is(err, context.Canceled) {
t.Errorf("Expected context.Canceled, got: %v", err)
}
}
func TestIfNeededWithPolicy_FixedDelay(t *testing.T) {
policy := NewFixedDelayPolicy(1 * time.Millisecond)
callCount := 0
start := time.Now()
err := IfNeededWithPolicy(policy, func() error {
callCount++
if callCount < 3 {
return &testError{message: "transient error", timeout: true}
}
return nil
}, WithMaxAttempts(5))
elapsed := time.Since(start)
if err != nil {
t.Errorf("Expected success, got error: %v", err)
}
if callCount != 3 {
t.Errorf("Expected 3 calls, got %d", callCount)
}
// Should have at least 2ms delay (2 retries * 1ms each)
if elapsed < 2*time.Millisecond {
t.Errorf("Expected at least 2ms elapsed time, got %v", elapsed)
}
}
func TestIfNeededWithPolicyAndContext_Success(t *testing.T) {
ctx := context.Background()
policy := NewNoDelayPolicy()
callCount := 0
err := IfNeededWithPolicyAndContext(ctx, policy, func(ctx context.Context) error {
callCount++
if callCount < 2 {
return &testError{message: "transient error", timeout: true}
}
return nil
})
if err != nil {
t.Errorf("Expected success, got error: %v", err)
}
if callCount != 2 {
t.Errorf("Expected 2 calls, got %d", callCount)
}
}