-
Notifications
You must be signed in to change notification settings - Fork 21
/
jobs_test.go
286 lines (231 loc) · 5.74 KB
/
jobs_test.go
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
package tasqueue
import (
"context"
"encoding/json"
"errors"
"testing"
"time"
)
func TestEnqueue(t *testing.T) {
var (
ctx = context.Background()
srv = newServer(t, taskName, MockHandler)
job = makeJob(t, taskName, false)
)
go srv.Start(ctx)
uuid, err := srv.Enqueue(ctx, job)
if err != nil {
t.Fatal(err)
}
t.Logf("Enqueued job with uuid : %s\n", uuid)
}
func TestJobWithTimeout(t *testing.T) {
var (
ctx = context.Background()
srv = newServer(t, "mock_timeout", MockHandlerWithSleep)
job = makeJob(t, "mock_timeout", false)
)
go srv.Start(ctx)
job.Opts.Timeout = 1 * time.Second
job.Opts.MaxRetries = 0
uuid, err := srv.Enqueue(ctx, job)
if err != nil {
t.Fatal(err)
}
// Wait for task to be consumed & processed.
time.Sleep(3 * time.Second)
// Check if the job has been marked as failed.
msg, err := srv.GetJob(ctx, uuid)
if err != nil {
t.Fatal(err)
}
if !(msg.Status == StatusFailed) {
t.Fatalf("incorrect job status, expected %s, got %s", StatusFailed, msg.Status)
}
}
func TestGetPending(t *testing.T) {
var (
jobs = map[string]Job{
StatusDone: makeJob(t, taskName, false),
StatusFailed: makeJob(t, taskName, true),
}
srv = newServer(t, taskName, MockHandler)
ctx = context.Background()
)
// Enqueue the jobs
for _, job := range jobs {
_, err := srv.Enqueue(ctx, job)
if err != nil {
t.Fatal(err)
}
}
// Check the queue for pending
msg, err := srv.GetPending(ctx, DefaultQueue)
if err != nil {
t.Fatal(err)
}
// Pending jobs must be equal to enqueued as of now
if len(msg) != len(jobs) {
t.Fatalf("pending job not found. resp: %+v, queued:%d", msg, len(jobs))
}
// Start consuming jobs
go srv.Start(ctx)
time.Sleep(2 * time.Second)
// Get pending (again) and check if jobs have been consumed
msg, err = srv.GetPending(ctx, DefaultQueue)
if err != nil {
t.Fatal(err)
}
if len(msg) != 0 {
t.Fatalf("pending job not found. resp: %+v", msg)
}
}
func TestGetJob(t *testing.T) {
var (
jobs = map[string]Job{
StatusDone: makeJob(t, taskName, false),
StatusFailed: makeJob(t, taskName, true),
}
srv = newServer(t, taskName, MockHandler)
ctx = context.Background()
)
go srv.Start(ctx)
for status, job := range jobs {
uuid, err := srv.Enqueue(ctx, job)
if err != nil {
t.Fatal(err)
}
// Wait for task to be consumed & processed.
time.Sleep(time.Second * 2)
msg, err := srv.GetJob(ctx, uuid)
if err != nil {
t.Fatal(err)
}
if msg.Status != status {
t.Fatalf("incorrect job status, expected %s, got %s", status, msg.Status)
}
}
}
func TestSaveJob(t *testing.T) {
var (
// handler simply saves the passed data onto the job's results
handler = func(d []byte, j JobCtx) error {
if err := j.Save(d); err != nil {
t.Fatal(err)
}
return nil
}
savedData = "saved results"
srv = newServer(t, taskName, MockHandler)
ctx = context.Background()
)
// Register the task and handler
err := srv.RegisterTask("validate-save", handler, TaskOpts{})
if err != nil {
t.Fatal(err)
}
// Create a job that passes the data needed to be saved.
job, err := NewJob("validate-save", []byte(savedData), JobOpts{MaxRetries: 1})
if err != nil {
t.Fatal(err)
}
go srv.Start(ctx)
uuid, err := srv.Enqueue(ctx, job)
if err != nil {
t.Fatal(err)
}
// Wait for task to be consumed & processed.
time.Sleep(time.Second * 2)
results, err := srv.GetResult(ctx, uuid)
if err != nil {
t.Fatal(err)
}
if string(results) != savedData {
t.Fatalf("saved results don't match results fetched.\nsaved:%v\nfetched:%v", savedData, results)
}
}
func TestDeleteJob(t *testing.T) {
var (
// handler simply saves the passed data onto the job's results
handler = func(d []byte, j JobCtx) error {
if err := j.Save(d); err != nil {
t.Fatal(err)
}
return nil
}
savedData = "saved results"
srv = newServer(t, taskName, MockHandler)
ctx = context.Background()
)
// Register the task and handler
err := srv.RegisterTask("validate-save", handler, TaskOpts{})
if err != nil {
t.Fatal(err)
}
// Create a job that passes the data needed to be saved.
job, err := NewJob("validate-save", []byte(savedData), JobOpts{MaxRetries: 1})
if err != nil {
t.Fatal(err)
}
go srv.Start(ctx)
uuid, err := srv.Enqueue(ctx, job)
if err != nil {
t.Fatal(err)
}
// Wait for task to be consumed & processed.
time.Sleep(time.Second * 3)
results, err := srv.GetResult(ctx, uuid)
if err != nil {
t.Fatal(err)
}
if string(results) != savedData {
t.Fatalf("saved results don't match results fetched.\nsaved:%v\nfetched:%v", savedData, string(results))
}
if err := srv.DeleteJob(ctx, uuid); err != nil {
t.Fatal(err)
}
_, err = srv.GetResult(ctx, uuid)
if !errors.Is(err, ErrNotFound) {
t.Fatalf("job results not deleted: %v", err)
}
}
func TestJobsOnError(t *testing.T) {
var (
srv = newServer(t, taskName, MockHandler)
)
hasErrored := make(chan bool, 1)
if err := srv.RegisterTask("error", func(b []byte, jc JobCtx) error {
t.Log("error task called")
hasErrored <- true
return nil
}, TaskOpts{
Queue: "error_task",
Concurrency: 1,
}); err != nil {
t.Fatal(err)
}
j := makeJob(t, taskName, true)
errJob, _ := NewJob("error", []byte{}, JobOpts{
Queue: "error_task",
})
j.OnError = append(j.OnError, &errJob)
if _, err := srv.Enqueue(context.Background(), j); err != nil {
t.Fatalf("error enqueuing job: %v", err)
}
go srv.Start(context.Background())
b := <-hasErrored
if !b {
t.Fatalf("error job didn't enqueue")
}
}
func makeJob(t *testing.T, taskName string, doErr bool) Job {
j, err := json.Marshal(MockPayload{ShouldErr: doErr})
if err != nil {
t.Fatal(err)
}
job, err := NewJob(taskName, j, JobOpts{MaxRetries: 1})
if err != nil {
t.Fatal(err)
}
return job
}