-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgoschd_test.go
More file actions
executable file
·338 lines (293 loc) · 7.86 KB
/
Copy pathgoschd_test.go
File metadata and controls
executable file
·338 lines (293 loc) · 7.86 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
package goschd
import (
"context"
"errors"
"sync"
"testing"
"time"
)
type MockLogger struct {
t *testing.T
}
func (m *MockLogger) WithField(key string, value any) Logger {
return m
}
func (m *MockLogger) WithError(err error) Logger {
return m
}
func (m *MockLogger) Debug(args ...any) {
m.t.Log(args...)
}
func (m *MockLogger) Info(args ...any) {
m.t.Log(args...)
}
func TestAddAndExecutionRunOnce(t *testing.T) {
scheduler := NewTaskScheduler(WithLogger(&MockLogger{t: t}))
doneCh := make(chan struct{})
task := &Task{
Interval: "100ms",
RunOnce: true,
FirstRun: true,
TaskFunc: func(ctx context.Context) error {
close(doneCh)
return nil
},
}
err := scheduler.Add("task1", task)
if err != nil {
t.Fatalf("unexpected error adding task: %v", err)
}
// Wait for the task to execute.
select {
case <-doneCh:
// Task executed as expected.
case <-time.After(500 * time.Millisecond):
t.Fatal("task did not execute within expected time")
}
// Allow some time for the task to be deleted if RunOnce.
time.Sleep(100 * time.Millisecond)
_, err = scheduler.Lookup("task1")
if err == nil {
t.Fatal("expected task to be deleted after run once, but it still exists")
}
if !errors.Is(err, ErrTaskNotFound) {
t.Fatalf("expected ErrTaskNotFound, got %v", err)
}
}
func TestAddTaskInvalidFunction(t *testing.T) {
scheduler := NewTaskScheduler(WithLogger(&MockLogger{t: t}))
// No TaskFunc or FuncWithTaskContext provided.
task := &Task{
Interval: "100ms",
}
err := scheduler.Add("invalid", task)
if err == nil {
t.Fatal("expected error when adding task with nil function, got nil")
}
if !errors.Is(err, ErrTaskFuncNil) {
t.Fatalf("expected ErrTaskFuncNil, got %v", err)
}
}
func TestDuplicateTaskAdd(t *testing.T) {
scheduler := NewTaskScheduler(WithLogger(&MockLogger{t: t}))
task := &Task{
Interval: "100ms",
FirstRun: true,
TaskFunc: func(ctx context.Context) error { return nil },
}
err := scheduler.Add("dup", task)
if err != nil {
t.Fatalf("unexpected error adding first task: %v", err)
}
task2 := &Task{
Interval: "100ms",
FirstRun: true,
TaskFunc: func(ctx context.Context) error { return nil },
}
err = scheduler.Add("dup", task2)
if err == nil {
t.Fatal("expected error when adding duplicate task id, got nil")
}
if !errors.Is(err, ErrTaskIDInUse) {
t.Fatalf("expected ErrTaskIDInUse, got %v", err)
}
}
func TestDelTask(t *testing.T) {
scheduler := NewTaskScheduler(WithLogger(&MockLogger{t: t}))
task := &Task{
Interval: "100ms",
FirstRun: true,
TaskFunc: func(ctx context.Context) error { return nil },
}
err := scheduler.Add("taskDel", task)
if err != nil {
t.Fatalf("unexpected error adding task: %v", err)
}
scheduler.Del("taskDel")
_, err = scheduler.Lookup("taskDel")
if err == nil {
t.Fatal("expected error looking up deleted task, got nil")
}
if !errors.Is(err, ErrTaskNotFound) {
t.Fatalf("expected ErrTaskNotFound, got %v", err)
}
}
func TestLookupTask(t *testing.T) {
scheduler := NewTaskScheduler(WithLogger(&MockLogger{t: t}))
task := &Task{
Interval: "100ms",
FirstRun: true,
TaskFunc: func(ctx context.Context) error { return nil },
}
err := scheduler.Add("lookupTask", task)
if err != nil {
t.Fatalf("unexpected error adding task: %v", err)
}
clone, err := scheduler.Lookup("lookupTask")
if err != nil {
t.Fatalf("unexpected error looking up task: %v", err)
}
if clone.id != "lookupTask" {
t.Fatalf("expected task id %q, got %q", "lookupTask", clone.id)
}
}
func TestInvalidInterval(t *testing.T) {
scheduler := NewTaskScheduler(WithLogger(&MockLogger{t: t}))
task := &Task{
Interval: "not-a-duration-or-cron",
FirstRun: true,
TaskFunc: func(ctx context.Context) error { return nil },
}
err := scheduler.Add("invalidInterval", task)
if err == nil {
t.Fatal("expected error when adding task with invalid interval, got nil")
}
}
func TestScheduleTaskWithStartAfter(t *testing.T) {
scheduler := NewTaskScheduler(WithLogger(&MockLogger{t: t}))
doneCh := make(chan struct{})
startAfter := time.Now().Add(200 * time.Millisecond)
task := &Task{
Interval: "50ms",
FirstRun: false,
RunOnce: true,
StartAfter: startAfter,
TaskFunc: func(ctx context.Context) error {
close(doneCh)
return nil
},
}
err := scheduler.Add("startAfterTask", task)
if err != nil {
t.Fatalf("unexpected error adding task: %v", err)
}
// Ensure the task does not run before the scheduled start.
select {
case <-doneCh:
t.Fatal("task executed before startAfter")
case <-time.After(150 * time.Millisecond):
// Expected: not executed yet.
}
// Now wait long enough for the task to fire.
select {
case <-doneCh:
// Task executed as expected.
case <-time.After(150 * time.Millisecond):
t.Fatal("task did not execute after startAfter delay")
}
}
func TestCronIntervalTask(t *testing.T) {
scheduler := NewTaskScheduler(WithLogger(&MockLogger{t: t}))
doneCh := make(chan struct{})
// Use a cron expression that fires every second.
task := &Task{
Interval: "* * * * * *", // cron expression (with seconds field)
RunOnce: true,
FirstRun: true,
TaskFunc: func(ctx context.Context) error {
close(doneCh)
return nil
},
}
err := scheduler.Add("cronTask", task)
if err != nil {
t.Fatalf("unexpected error adding cron task: %v", err)
}
select {
case <-doneCh:
// Task executed as expected.
case <-time.After(2 * time.Second):
t.Fatal("cron task did not execute within expected time")
}
}
func TestErrFuncInvocation(t *testing.T) {
scheduler := NewTaskScheduler(WithLogger(&MockLogger{t: t}))
errCh := make(chan error, 1)
task := &Task{
Interval: "100ms",
RunOnce: true,
FirstRun: true,
TaskFunc: func(ctx context.Context) error {
return errors.New("task error")
},
ErrFunc: func(err error) {
errCh <- err
},
}
err := scheduler.Add("errorTask", task)
if err != nil {
t.Fatalf("unexpected error adding task: %v", err)
}
select {
case e := <-errCh:
if e.Error() != "task error" {
t.Fatalf("expected error 'task error', got %v", e)
}
case <-time.After(500 * time.Millisecond):
t.Fatal("error function was not invoked")
}
}
func TestRunSingleInstance(t *testing.T) {
scheduler := NewTaskScheduler(WithLogger(&MockLogger{t: t}))
var mu sync.Mutex
count := 0
wg := sync.WaitGroup{}
wg.Add(1)
// The task sleeps long enough that if it were run concurrently,
// the counter would increment more than once.
task := &Task{
Interval: "100ms",
RunOnce: true,
RunSingleInstance: true,
FirstRun: true,
TaskFunc: func(ctx context.Context) error {
mu.Lock()
count++
mu.Unlock()
time.Sleep(150 * time.Millisecond)
wg.Done()
return nil
},
}
err := scheduler.Add("singleInstanceTask", task)
if err != nil {
t.Fatalf("unexpected error adding task: %v", err)
}
wg.Wait()
mu.Lock()
if count != 1 {
t.Errorf("expected task to run once due to single instance constraint, got count %d", count)
}
mu.Unlock()
}
func TestClone(t *testing.T) {
original := &Task{
Interval: "100ms",
RunOnce: false,
RunSingleInstance: true,
FirstRun: true,
TaskFunc: func(ctx context.Context) error { return nil },
}
original.TaskContext = TaskContext{
Context: context.Background(),
id: "original",
}
clone := original.Clone()
if clone == original {
t.Fatal("clone should not be the same pointer as original")
}
if clone.Interval != original.Interval ||
clone.RunOnce != original.RunOnce ||
clone.RunSingleInstance != original.RunSingleInstance ||
clone.FirstRun != original.FirstRun ||
clone.TaskContext.id != original.TaskContext.id {
t.Fatal("clone does not match original task properties")
}
}
func TestWithLogger(t *testing.T) {
mockLogger := &MockLogger{t: t}
scheduler := NewTaskScheduler(WithLogger(mockLogger))
if scheduler.logger != mockLogger {
t.Fatal("scheduler logger was not set correctly with WithLogger option")
}
}