-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathexecutors_test.go
More file actions
435 lines (386 loc) · 11.8 KB
/
Copy pathexecutors_test.go
File metadata and controls
435 lines (386 loc) · 11.8 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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
package routine
import (
"bytes"
"context"
"errors"
"strings"
"sync/atomic"
"testing"
"time"
)
// --- Guarantee ---
func TestGuaranteeSuccess(t *testing.T) {
called := false
exec := Guarantee(ExecutorFunc(func(ctx context.Context) error {
called = true
return nil
}))
if err := exec.Execute(context.Background()); err != nil {
t.Fatal(err)
}
if !called {
t.Fatal("inner executor not called")
}
}
func TestGuaranteeRecoversPanic(t *testing.T) {
exec := Guarantee(ExecutorFunc(func(ctx context.Context) error {
panic("boom")
}))
// Guarantee swallows the panic and returns nil
if err := exec.Execute(context.Background()); err != nil {
t.Fatalf("expected nil, got %v", err)
}
}
func TestGuaranteeRecoversPanicError(t *testing.T) {
exec := Guarantee(ExecutorFunc(func(ctx context.Context) error {
panic(errors.New("panic-error"))
}))
if err := exec.Execute(context.Background()); err != nil {
t.Fatalf("expected nil (swallowed), got %v", err)
}
}
func TestGuaranteeInnerError(t *testing.T) {
exec := Guarantee(ExecutorFunc(func(ctx context.Context) error {
return errors.New("inner error")
}))
// Guarantee logs but returns nil
if err := exec.Execute(context.Background()); err != nil {
t.Fatalf("expected nil, got %v", err)
}
}
// --- Retry ---
func TestRetrySuccess(t *testing.T) {
var calls int
exec := Retry(3, ExecutorFunc(func(ctx context.Context) error {
calls++
return nil
}))
if err := exec.Execute(context.Background()); err != nil {
t.Fatal(err)
}
if calls != 1 {
t.Fatalf("expected 1 call, got %d", calls)
}
}
func TestRetryExhausted(t *testing.T) {
want := errors.New("fail")
exec := Retry(3, ExecutorFunc(func(ctx context.Context) error {
return want
}))
if err := exec.Execute(context.Background()); err == nil {
t.Fatal("expected error")
}
}
func TestRetrySucceedsOnSecondAttempt(t *testing.T) {
var calls int
exec := Retry(3, ExecutorFunc(func(ctx context.Context) error {
calls++
if calls < 2 {
return errors.New("not yet")
}
return nil
}))
if err := exec.Execute(context.Background()); err != nil {
t.Fatal(err)
}
if calls != 2 {
t.Fatalf("expected 2 calls, got %d", calls)
}
}
func TestFromRetry(t *testing.T) {
var seen []int
exec := Retry(3, ExecutorFunc(func(ctx context.Context) error {
seen = append(seen, FromRetry(ctx))
return errors.New("keep retrying")
}))
exec.Execute(context.Background()) //nolint
if len(seen) != 3 || seen[0] != 1 || seen[1] != 2 || seen[2] != 3 {
t.Fatalf("unexpected retry counts: %v", seen)
}
}
func TestFromRetryNoContext(t *testing.T) {
if got := FromRetry(nil); got != 0 {
t.Fatalf("expected 0, got %d", got)
}
if got := FromRetry(context.Background()); got != 0 {
t.Fatalf("expected 0, got %d", got)
}
}
// --- Repeat ---
func TestRepeatFixed(t *testing.T) {
var calls int32
exec := Repeat(3, 0, ExecutorFunc(func(ctx context.Context) error {
atomic.AddInt32(&calls, 1)
return nil
}))
if err := exec.Execute(context.Background()); err != nil {
t.Fatal(err)
}
if atomic.LoadInt32(&calls) != 3 {
t.Fatalf("expected 3 calls, got %d", calls)
}
}
func TestRepeatError(t *testing.T) {
exec := Repeat(5, 0, ExecutorFunc(func(ctx context.Context) error {
return errors.New("fail")
}))
if err := exec.Execute(context.Background()); err == nil {
t.Fatal("expected error")
}
}
func TestFromRepeat(t *testing.T) {
var seen []int
exec := Repeat(3, 0, ExecutorFunc(func(ctx context.Context) error {
seen = append(seen, FromRepeat(ctx))
return nil
}))
exec.Execute(context.Background()) //nolint
if len(seen) != 3 || seen[0] != 1 || seen[1] != 2 || seen[2] != 3 {
t.Fatalf("unexpected repeat counts: %v", seen)
}
}
func TestFromRepeatNoContext(t *testing.T) {
if got := FromRepeat(nil); got != 0 {
t.Fatalf("expected 0, got %d", got)
}
}
func TestRepeatWithInterval(t *testing.T) {
var calls int32
exec := Repeat(2, 10*time.Millisecond, ExecutorFunc(func(ctx context.Context) error {
atomic.AddInt32(&calls, 1)
return nil
}))
start := time.Now()
exec.Execute(context.Background()) //nolint
elapsed := time.Since(start)
if atomic.LoadInt32(&calls) != 2 {
t.Fatalf("expected 2 calls, got %d", calls)
}
// Should have waited at least one interval (10ms)
if elapsed < 10*time.Millisecond {
t.Fatalf("expected at least 10ms elapsed, got %v", elapsed)
}
}
// --- Crontab ---
func TestFromCrontabNoContext(t *testing.T) {
if got := FromCrontab(nil); !got.IsZero() {
t.Fatal("expected zero time")
}
if got := FromCrontab(context.Background()); !got.IsZero() {
t.Fatal("expected zero time")
}
}
func TestCrontabInvalidPlan(t *testing.T) {
exec := Crontab("invalid-cron", ExecutorFunc(func(ctx context.Context) error { return nil }))
if err := exec.Execute(context.Background()); err == nil {
t.Fatal("expected error for invalid cron expression")
}
}
func TestCrontabContextCancel(t *testing.T) {
exec := Crontab("* * * * * * *", ExecutorFunc(func(ctx context.Context) error { return nil }))
ctx, cancel := context.WithTimeout(context.Background(), 50*time.Millisecond)
defer cancel()
err := exec.Execute(ctx)
if err == nil {
t.Fatal("expected context error")
}
}
func TestCrontabIsTimeMuted(t *testing.T) {
exec := Crontab("* * * * *", ExecutorFunc(func(ctx context.Context) error { return nil }))
now := time.Now()
exec.Mute(now.Add(-time.Hour), now.Add(time.Hour))
if !exec.IsTimeMuted(now) {
t.Fatal("expected time to be muted")
}
exec2 := Crontab("* * * * *", ExecutorFunc(func(ctx context.Context) error { return nil }))
exec2.Workday(true) // mute weekends, only fire on workdays
weekday := time.Date(2024, 1, 2, 12, 0, 0, 0, time.UTC) // Tuesday
weekend := time.Date(2024, 1, 6, 12, 0, 0, 0, time.UTC) // Saturday
if !exec2.IsTimeMuted(weekend) {
t.Fatal("weekend should be muted when Workday(true)")
}
if exec2.IsTimeMuted(weekday) {
t.Fatal("weekday should not be muted when Workday(true)")
}
exec3 := Crontab("* * * * *", ExecutorFunc(func(ctx context.Context) error { return nil }))
exec3.Weekend(true) // mute weekdays, only fire on weekends
if !exec3.IsTimeMuted(weekday) {
t.Fatal("weekday should be muted when Weekend(true)")
}
if exec3.IsTimeMuted(weekend) {
t.Fatal("weekend should not be muted when Weekend(true)")
}
}
func TestCrontabEveryday(t *testing.T) {
exec := Crontab("* * * * *", ExecutorFunc(func(ctx context.Context) error { return nil }))
exec.Workday(true)
exec.Everyday(true)
weekday := time.Date(2024, 1, 2, 12, 0, 0, 0, time.UTC)
weekend := time.Date(2024, 1, 6, 12, 0, 0, 0, time.UTC)
// Everyday(true) clears both flags
if exec.IsTimeMuted(weekday) || exec.IsTimeMuted(weekend) {
t.Fatal("expected no mute after Everyday(true)")
}
}
// --- Command ---
func TestCommandEcho(t *testing.T) {
var buf bytes.Buffer
exec := Command("echo", ARG("hello"), Stdout(&buf))
if err := exec.Execute(context.Background()); err != nil {
t.Fatal(err)
}
if !strings.Contains(buf.String(), "hello") {
t.Fatalf("unexpected output: %q", buf.String())
}
}
func TestCommandNotFound(t *testing.T) {
exec := Command("this-command-does-not-exist-at-all")
if err := exec.Execute(context.Background()); err == nil {
t.Fatal("expected error for missing command")
}
}
func TestCommandEnv(t *testing.T) {
var buf bytes.Buffer
exec := Command("sh", ARG("-c"), ARG("echo $TEST_VAR"), ENV("TEST_VAR=hello_env"), Stdout(&buf))
if err := exec.Execute(context.Background()); err != nil {
t.Fatal(err)
}
if !strings.Contains(buf.String(), "hello_env") {
t.Fatalf("unexpected output: %q", buf.String())
}
}
func TestCommandStdin(t *testing.T) {
var buf bytes.Buffer
exec := Command("cat", Stdin(strings.NewReader("from-stdin")), Stdout(&buf))
if err := exec.Execute(context.Background()); err != nil {
t.Fatal(err)
}
if buf.String() != "from-stdin" {
t.Fatalf("unexpected output: %q", buf.String())
}
}
func TestCommandContextCancel(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 50*time.Millisecond)
defer cancel()
exec := Command("sleep", ARG("10"))
if err := exec.Execute(ctx); err == nil {
t.Fatal("expected error from context cancellation")
}
}
// --- Timeout ---
func TestTimeoutSuccess(t *testing.T) {
exec := Timeout(time.Second, ExecutorFunc(func(ctx context.Context) error { return nil }))
if err := exec.Execute(context.Background()); err != nil {
t.Fatal(err)
}
}
func TestTimeoutExpires(t *testing.T) {
exec := Timeout(50*time.Millisecond, ExecutorFunc(func(ctx context.Context) error {
time.Sleep(time.Second)
return nil
}))
if err := exec.Execute(context.Background()); err == nil {
t.Fatal("expected timeout error")
}
}
// --- Deadline ---
func TestDeadlineSuccess(t *testing.T) {
exec := Deadline(time.Now().Add(time.Second), ExecutorFunc(func(ctx context.Context) error { return nil }))
if err := exec.Execute(context.Background()); err != nil {
t.Fatal(err)
}
}
func TestDeadlineExpired(t *testing.T) {
exec := Deadline(time.Now().Add(50*time.Millisecond), ExecutorFunc(func(ctx context.Context) error {
time.Sleep(time.Second)
return nil
}))
if err := exec.Execute(context.Background()); err == nil {
t.Fatal("expected deadline error")
}
}
// --- Concurrent ---
func TestConcurrent(t *testing.T) {
var calls int32
exec := Concurrent(5, ExecutorFunc(func(ctx context.Context) error {
atomic.AddInt32(&calls, 1)
return nil
}))
if err := exec.Execute(context.Background()); err != nil {
t.Fatal(err)
}
if atomic.LoadInt32(&calls) != 5 {
t.Fatalf("expected 5 calls, got %d", calls)
}
}
func TestConcurrentWithError(t *testing.T) {
// ConcurrentExecutor logs errors but always returns nil
exec := Concurrent(3, ExecutorFunc(func(ctx context.Context) error {
return errors.New("concurrent fail")
}))
if err := exec.Execute(context.Background()); err != nil {
t.Fatalf("expected nil (errors logged), got %v", err)
}
}
// --- Parallel ---
func TestParallel(t *testing.T) {
var calls int32
inc := ExecutorFunc(func(ctx context.Context) error {
atomic.AddInt32(&calls, 1)
return nil
})
exec := Parallel(inc, inc, inc)
if err := exec.Execute(context.Background()); err != nil {
t.Fatal(err)
}
if atomic.LoadInt32(&calls) != 3 {
t.Fatalf("expected 3 calls, got %d", calls)
}
}
func TestParallelWithError(t *testing.T) {
// ParallelExecutor logs errors but returns nil
fail := ExecutorFunc(func(ctx context.Context) error { return errors.New("parallel fail") })
exec := Parallel(fail, fail)
if err := exec.Execute(context.Background()); err != nil {
t.Fatalf("expected nil (errors logged), got %v", err)
}
}
// --- Append ---
func TestAppendSequential(t *testing.T) {
var order []int
exec := Append(
ExecutorFunc(func(ctx context.Context) error { order = append(order, 1); return nil }),
ExecutorFunc(func(ctx context.Context) error { order = append(order, 2); return nil }),
ExecutorFunc(func(ctx context.Context) error { order = append(order, 3); return nil }),
)
if err := exec.Execute(context.Background()); err != nil {
t.Fatal(err)
}
if len(order) != 3 || order[0] != 1 || order[1] != 2 || order[2] != 3 {
t.Fatalf("unexpected order: %v", order)
}
}
func TestAppendStopsOnError(t *testing.T) {
var calls int
exec := Append(
ExecutorFunc(func(ctx context.Context) error { calls++; return nil }),
ExecutorFunc(func(ctx context.Context) error { calls++; return errors.New("stop") }),
ExecutorFunc(func(ctx context.Context) error { calls++; return nil }),
)
if err := exec.Execute(context.Background()); err == nil {
t.Fatal("expected error")
}
if calls != 2 {
t.Fatalf("expected 2 calls before stop, got %d", calls)
}
}
func TestAppendDynamic(t *testing.T) {
exec := Append()
var calls int
exec.Append(ExecutorFunc(func(ctx context.Context) error { calls++; return nil }))
exec.Append(ExecutorFunc(func(ctx context.Context) error { calls++; return nil }))
exec.Execute(context.Background()) //nolint
if calls != 2 {
t.Fatalf("expected 2, got %d", calls)
}
}