-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconman_test.go
More file actions
331 lines (287 loc) · 7.95 KB
/
conman_test.go
File metadata and controls
331 lines (287 loc) · 7.95 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
// Author: Ilyess Bachiri
// Copyright (c) 2021-present Ilyess Bachiri
package conman
import (
"context"
"fmt"
"slices"
"strings"
"testing"
"time"
)
type flakydoubler struct {
operand int
runCount int
}
func (f *flakydoubler) Execute(ctx context.Context) (int, error) {
if f.runCount < 2 {
f.runCount++
err := &RetriableError{Err: fmt.Errorf("Try again")}
return -1, err.WithExponentialBackoff()
}
return f.operand * 2, nil
}
type faultydoubler struct {
operand int
}
func (f *faultydoubler) Execute(ctx context.Context) (int, error) {
err := &RetriableError{Err: fmt.Errorf("Try again")}
return -1, err.WithLinearBackoff()
}
type doubler struct {
operand int
}
func (d *doubler) Execute(ctx context.Context) (int, error) {
return d.operand * 2, nil
}
type errdoubler struct {
operand int
}
func (d *errdoubler) Execute(ctx context.Context) (int, error) {
return -1, fmt.Errorf("Error calculating for %v", d.operand)
}
type slowdoubler struct {
delayInMiliseconds int
operand int
}
func (d *slowdoubler) Execute(ctx context.Context) (int, error) {
delay := d.delayInMiliseconds
if delay == 0 {
delay = 100 // default to 100 ms if not specified
}
time.Sleep(time.Duration(delay) * time.Millisecond)
select {
case <-ctx.Done():
return -1, ctx.Err()
default:
return d.operand * 2, nil
}
}
func TestCaptureOutputs(t *testing.T) {
t.Parallel()
ctx := t.Context()
cm, err := New[int](5)
if err != nil {
t.Fatalf("Failed to create ConMan: %v", err)
}
cm.Run(ctx, &doubler{operand: 299})
cm.Run(ctx, &doubler{operand: 532})
cm.Run(ctx, &doubler{operand: 203})
if err := cm.Wait(ctx); err != nil {
t.Fatalf("ConMan Wait returned an unexpected error: %v", err)
}
for _, o := range []int{598, 1064, 406} {
if !slices.Contains(cm.Outputs(), o) {
t.Errorf("Expected output %v is not part of the captured outputs", o)
}
}
}
func TestCaptureErrors(t *testing.T) {
t.Parallel()
ctx := t.Context()
cm, err := New[int](5)
if err != nil {
t.Fatalf("Failed to create ConMan: %v", err)
}
cm.Run(ctx, &errdoubler{operand: 299})
cm.Run(ctx, &errdoubler{operand: 532})
cm.Run(ctx, &errdoubler{operand: 203})
if err := cm.Wait(ctx); err != nil {
t.Fatalf("ConMan Wait returned an unexpected error: %v", err)
}
for _, i := range []int{299, 532, 203} {
if !containsError(cm.Errors(), fmt.Errorf("Error calculating for %v", i)) {
t.Errorf("Expected error for %v but none was found", i)
}
}
}
func TestConcurrencyLimit(t *testing.T) {
t.Parallel()
ctx := t.Context()
cm, err := New[int](2)
if err != nil {
t.Fatalf("Failed to create ConMan: %v", err)
}
cm.Run(ctx, &slowdoubler{operand: 299})
cm.Run(ctx, &slowdoubler{operand: 532})
cm.Run(ctx, &slowdoubler{operand: 203})
// Wait to make sure the first two tasks have completed
time.Sleep(10 * time.Millisecond)
if slices.Contains(cm.Outputs(), 406) {
t.Errorf("Didn't expect task for 406 to have been executed")
}
if err := cm.Wait(ctx); err != nil {
t.Fatalf("ConMan Wait returned an unexpected error: %v", err)
}
if !slices.Contains(cm.Outputs(), 406) {
t.Errorf("Expected task for 406 to have been exectued")
}
}
func TestRetries(t *testing.T) {
t.Parallel()
ctx := t.Context()
cm, err := New[int](3)
if err != nil {
t.Fatalf("Failed to create ConMan: %v", err)
}
cm.Run(ctx, &flakydoubler{operand: 299})
cm.Run(ctx, &flakydoubler{operand: 532})
cm.Run(ctx, &flakydoubler{operand: 203})
if err := cm.Wait(ctx); err != nil {
t.Fatalf("ConMan Wait returned an unexpected error: %v", err)
}
for _, o := range []int{598, 1064, 406} {
if !slices.Contains(cm.Outputs(), o) {
t.Errorf("Expected output %v is not part of the captured outputs", o)
}
}
}
func TestMaxRetries(t *testing.T) {
t.Parallel()
ctx := t.Context()
cm, err := New[int](3)
if err != nil {
t.Fatalf("Failed to create ConMan: %v", err)
}
cm.Run(ctx, &faultydoubler{operand: 299})
cm.Run(ctx, &faultydoubler{operand: 532})
cm.Run(ctx, &faultydoubler{operand: 203})
if err := cm.Wait(ctx); err != nil {
t.Fatalf("ConMan Wait returned an unexpected error: %v", err)
}
for _, o := range []int{598, 1064, 406} {
if slices.Contains(cm.Outputs(), o) {
t.Errorf("Didn't expect output %v in the captured outputs", o)
}
}
if errCount := len(cm.Errors()); errCount != 3 {
t.Errorf("Expected 3 errors, got %d", errCount)
}
}
func TestDispatchTimeout(t *testing.T) {
t.Parallel()
ctx, cancel := context.WithTimeout(context.Background(), 200*time.Millisecond)
defer cancel()
cm, err := New[int](3)
if err != nil {
t.Fatalf("Failed to create ConMan: %v", err)
}
cm.Run(ctx, &slowdoubler{operand: 299})
time.Sleep(300 * time.Millisecond) // Ensure the context times out before next runs
if err := cm.Run(ctx, &slowdoubler{operand: 532}); err == nil {
t.Errorf("Expected context deadline exceeded error but got nil")
}
if err := cm.Run(ctx, &slowdoubler{operand: 203}); err == nil {
t.Errorf("Expected context deadline exceeded error but got nil")
}
if err := cm.Wait(context.Background()); err != nil {
t.Fatalf("ConMan Wait returned an unexpected error: %v", err)
}
if !slices.Contains(cm.Outputs(), 598) {
t.Errorf("Expected output %v is not part of the captured outputs", 598)
}
}
func TestContextPropagation(t *testing.T) {
t.Parallel()
ctx, cancel := context.WithCancel(context.Background())
cm, err := New[int](3)
if err != nil {
t.Fatalf("Failed to create ConMan: %v", err)
}
cm.Run(ctx, &slowdoubler{operand: 299, delayInMiliseconds: 100})
cm.Run(ctx, &slowdoubler{operand: 532, delayInMiliseconds: 200})
cm.Run(ctx, &slowdoubler{operand: 203, delayInMiliseconds: 400})
go func() {
<-time.After(300 * time.Millisecond)
cancel()
}()
if err := cm.Wait(context.Background()); err != nil {
t.Fatalf("ConMan Wait returned an unexpected error: %v", err)
}
for _, o := range []int{598, 1064} {
if !slices.Contains(cm.Outputs(), o) {
t.Errorf("Expected output %v is not part of the captured outputs", o)
}
}
if slices.Contains(cm.Outputs(), 406) {
t.Errorf("Didn't expect output %v in the captured outputs", 406)
}
if len(cm.Errors()) < 1 {
t.Error("Expected one error, got none")
}
errMsg := cm.Errors()[0].Error()
if errMsg != "context canceled" {
t.Errorf("Expected a 'context canceled' error, got '%s'", errMsg)
}
}
func TestNewValidation(t *testing.T) {
t.Parallel()
tests := []struct {
name string
concurrencyLimit int64
expectError bool
errorContains string
}{
{
name: "Valid concurrency limit 2",
concurrencyLimit: 2,
expectError: false,
},
{
name: "Valid concurrency limit 5",
concurrencyLimit: 5,
expectError: false,
},
{
name: "Invalid concurrency limit 1",
concurrencyLimit: 1,
expectError: true,
errorContains: "concurrencyLimit must be at least 2, got 1",
},
{
name: "Invalid concurrency limit 0",
concurrencyLimit: 0,
expectError: true,
errorContains: "concurrencyLimit must be at least 2, got 0",
},
{
name: "Invalid concurrency limit negative",
concurrencyLimit: -5,
expectError: true,
errorContains: "concurrencyLimit must be at least 2, got -5",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cm, err := New[int](tt.concurrencyLimit)
if tt.expectError {
if err == nil {
t.Errorf("Expected error but got none")
return
}
if tt.errorContains != "" && !strings.Contains(err.Error(), tt.errorContains) {
t.Errorf("Expected error to contain '%s', got '%s'", tt.errorContains, err.Error())
}
if cm != nil {
t.Errorf("Expected nil ConMan on error, got %v", cm)
}
} else {
if err != nil {
t.Errorf("Unexpected error: %v", err)
return
}
if cm == nil {
t.Errorf("Expected non-nil ConMan, got nil")
}
}
})
}
}
func containsError(items []error, item error) bool {
for _, i := range items {
if i.Error() == item.Error() {
return true
}
}
return false
}