-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathqueue_test.go
325 lines (275 loc) Β· 7.44 KB
/
queue_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
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
package goalpost
import (
"context"
"errors"
"fmt"
"log"
"os"
"testing"
"time"
"github.com/stretchr/testify/assert"
bolt "go.etcd.io/bbolt"
)
type testWorker struct{}
type workArgs struct {
ctx context.Context
job Job
}
type longWorker struct{}
var workChan = make(chan workArgs)
func (w *testWorker) DoWork(ctx context.Context, j *Job) error {
fmt.Printf("DoWork called: %+v", *j)
//time.Sleep(1 * time.Second)
jc := *j
args := workArgs{ctx: ctx, job: jc}
workChan <- args
return nil
}
func (w *testWorker) ID() string {
return "testWorker"
}
func (w *longWorker) DoWork(ctx context.Context, j *Job) error {
for i := 0; i < 100; i++ {
select {
case <-ctx.Done():
return nil
default:
time.Sleep(1 * time.Second)
}
}
return nil
}
func (w *longWorker) ID() string {
return "longWorker"
}
func TestQueueInit(t *testing.T) {
os.Remove("test.db")
q, err := Init("test")
assert.Nil(t, err)
defer os.Remove("test.db")
defer q.Close()
q.db.View(func(tx *bolt.Tx) error {
jobBucket := tx.Bucket([]byte(jobsBucketName))
assert.NotNil(t, jobBucket)
completedJobsBucket := tx.Bucket([]byte(completedJobsBucketName))
assert.NotNil(t, completedJobsBucket)
return nil
})
assert.NotNil(t, q.notifier)
assert.Equal(t, make([]*Worker, 0), q.workers)
}
func TestRegisterWorker(t *testing.T) {
os.Remove("test.db")
q, err := Init("test")
assert.Nil(t, err)
defer os.Remove("test.db")
defer q.Close()
worker := &testWorker{}
var workerInterface Worker
q.RegisterWorker(worker)
expectedWorkerList := make([]*Worker, 1)
workerInterface = worker
expectedWorkerList[0] = &workerInterface
assert.Equal(t, expectedWorkerList, q.workers)
}
func TestPushJob(t *testing.T) {
os.Remove("test.db")
q, err := Init("test")
assert.Nil(t, err)
defer os.Remove("test.db")
defer q.Close()
worker := &testWorker{}
q.RegisterWorker(worker)
testData := []byte("abcd")
t.Logf("Pushign test job onto queue")
q.PushBytes(testData)
args := <-workChan
assert.NotNil(t, args)
//A read-write transaction that does nothing, so we can ensure the trasaction for
// marking the job as failed is committed before continuing
q.db.Update(func(tx *bolt.Tx) error {
//Anti-race condition transaction πββ
return nil
})
completedJob, err := q.getJobInBucketByID(args.job.ID, completedJobsBucketName)
assert.Nil(t, err)
assert.NotNil(t, completedJob)
assert.Equal(t, Ack, completedJob.Status)
}
func TestGracefulShutdown(t *testing.T) {
//This test will take over 30s to complete if the early exit doesn't work
os.Remove("test.db")
q, err := Init("test")
assert.Nil(t, err)
defer os.Remove("test.db")
defer q.Close()
worker := &longWorker{}
q.RegisterWorker(worker)
testData := []byte("abcd")
q.PushBytes(testData)
time.Sleep(5 * time.Second)
q.Close()
}
var erroringChan = make(chan Job)
type erroringWorker struct{}
func (w *erroringWorker) DoWork(ctx context.Context, j *Job) error {
log.Printf("Recevied job %d that's been retried %d times", j.ID, j.RetryCount)
if j.RetryCount < 2 {
return NewRecoverableWorkerError("test error")
} else {
var newJob Job
newJob = Job(*j)
erroringChan <- newJob
return errors.New("Fatal error")
}
}
func (w *erroringWorker) ID() string {
return "erroringWorker"
}
func TestErroringJob(t *testing.T) {
//This tests that a recoverable error is retried,
// and that a non-recoverable error is marked as failed
os.Remove("test.db")
q, err := Init("test")
assert.Nil(t, err)
defer os.Remove("test.db")
defer q.Close()
worker := &erroringWorker{}
q.RegisterWorker(worker)
testData := []byte("abcd")
q.PushBytes(testData)
job := <-erroringChan
assert.NotNil(t, job)
assert.Equal(t, 2, job.RetryCount)
//A read-write transaction that does nothing, so we can ensure the trasaction for
// marking the job as failed is committed before continuing
q.db.Update(func(tx *bolt.Tx) error {
//Anti-race condition transaction π
return nil
})
//The job shouldn't exist in the active jobs bucket anymore
oldJob, err := q.getJobInBucketByID(job.ID, jobsBucketName)
assert.Nil(t, oldJob)
completedJob, err := q.getJobInBucketByID(job.ID, completedJobsBucketName)
assert.Nil(t, err)
assert.Equal(t, Failed, completedJob.Status)
}
func TestResumeNackedJobs(t *testing.T) {
os.Remove("test.db")
q, err := Init("test")
assert.Nil(t, err)
defer os.Remove("test.db")
defer q.Close()
worker := &testWorker{}
q.RegisterWorker(worker)
testData := []byte("abcd")
//q.PushBytes(&testData)
var jobID uint64
//Put a Nacked job on the queue
q.db.Update(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte(jobsBucketName))
jobID, _ = b.NextSequence()
job := &Job{
ID: jobID,
Status: Nack,
Data: testData,
RetryCount: 0,
}
err := b.Put(intToByteArray(jobID), job.Bytes())
return err
})
t.Log("Resuming stopped jobs")
q.resumeUnackedJobs()
args := <-workChan
t.Log(args.job.ID)
assert.NotNil(t, args)
//A read-write transaction that does nothing, so we can ensure the trasaction for
// marking the job as failed is committed before continuing
q.db.Update(func(tx *bolt.Tx) error {
//Anti-race condition transaction πββ
return nil
})
completedJob, err := q.getJobInBucketByID(args.job.ID, completedJobsBucketName)
assert.Nil(t, err)
assert.NotNil(t, completedJob)
assert.Equal(t, Ack, completedJob.Status)
}
func TestResumeUackedJobs(t *testing.T) {
os.Remove("test.db")
q, err := Init("test")
assert.Nil(t, err)
defer os.Remove("test.db")
defer q.Close()
worker := &testWorker{}
q.RegisterWorker(worker)
testData := []byte("abcd")
//q.PushBytes(&testData)
var jobID uint64
//Put a Nacked job on the queue
q.db.Update(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte(jobsBucketName))
jobID, _ = b.NextSequence()
job := &Job{
ID: jobID,
Status: Uack,
Data: testData,
RetryCount: 0,
}
err := b.Put(intToByteArray(jobID), job.Bytes())
return err
})
t.Log("Resuming stopped jobs")
q.resumeUnackedJobs()
args := <-workChan
t.Log(args.job.ID)
assert.NotNil(t, args)
//A read-write transaction that does nothing, so we can ensure the trasaction for
// marking the job as failed is committed before continuing
q.db.Update(func(tx *bolt.Tx) error {
//Anti-race condition transaction πββ
return nil
})
completedJob, err := q.getJobInBucketByID(args.job.ID, completedJobsBucketName)
assert.Nil(t, err)
assert.NotNil(t, completedJob)
assert.Equal(t, Ack, completedJob.Status)
}
func TestCleanOldJobs(t *testing.T) {
os.Remove("test.db")
q, err := Init("test")
assert.Nil(t, err)
defer os.Remove("test.db")
defer q.Close()
t.Log("Adding test failed jobs")
// Set up some failed jobs
for _, v := range "abcdefghijklmnopqrstuvwxyz" {
j := &Job{
Status: Failed,
Data: []byte{byte(v)},
RetryCount: 0,
}
err := q.db.Update(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte(completedJobsBucketName))
jobID, _ := b.NextSequence()
j.ID = jobID
return b.Put(intToByteArray(jobID), j.Bytes())
})
assert.Nil(t, err)
}
t.Log("Checking number of failed jobs is 26")
// Check that jobs are actually there
q.db.View(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte(completedJobsBucketName))
stats := b.Stats()
assert.Equal(t, stats.KeyN, 26)
return nil
})
q.CleanOldJobs()
t.Log("Checking all failed/completed jobs are deleted")
q.db.View(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte(completedJobsBucketName))
stats := b.Stats()
assert.Equal(t, stats.KeyN, 0)
return nil
})
}