-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
368 lines (286 loc) · 10.6 KB
/
main_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
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
package jackd_test
import (
"crypto/rand"
"testing"
"time"
"github.com/goccy/go-yaml"
"github.com/stretchr/testify/assert"
"github.com/getjackd/go-jackd"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
)
type JackdSuite struct {
suite.Suite
beanstalkd *jackd.Client
beanstalkd2 *jackd.Client
}
func TestJackdSuite(t *testing.T) {
suite.Run(t, new(JackdSuite))
}
func (suite *JackdSuite) SetupTest() {
beanstalkd, err := jackd.Dial("localhost:11300")
require.NoError(suite.T(), err)
suite.beanstalkd = beanstalkd
beanstalkd2, err := jackd.Dial("localhost:11300")
require.NoError(suite.T(), err)
suite.beanstalkd2 = beanstalkd2
}
func (suite *JackdSuite) TearDownTest() {
err := suite.beanstalkd.Quit()
require.NoError(suite.T(), err)
}
func TestConnects(t *testing.T) {
beanstalkd, err := jackd.Dial("localhost:11300")
defer beanstalkd.Quit()
require.NoError(t, err)
}
func TestConnectsAndDisconnects(t *testing.T) {
beanstalkd, err := jackd.Dial("localhost:11300")
require.NoError(t, err)
err = beanstalkd.Quit()
require.NoError(t, err)
}
func (suite *JackdSuite) TestPutJob() {
id, err := suite.beanstalkd.Put([]byte("test job"), jackd.DefaultPutOpts())
defer suite.beanstalkd.Delete(id)
require.NoError(suite.T(), err)
assert.IsType(suite.T(), uint32(0), id)
assert.True(suite.T(), id > 0)
}
func (suite *JackdSuite) TestReserve() {
payload := []byte("test job")
id, err := suite.beanstalkd.Put(payload, jackd.DefaultPutOpts())
defer suite.beanstalkd.Delete(id)
reservedID, reservedPayload, err := suite.beanstalkd.Reserve()
require.NoError(suite.T(), err)
assert.Equal(suite.T(), id, reservedID)
assert.Equal(suite.T(), payload, reservedPayload)
}
func (suite *JackdSuite) TestRelease() {
payload := []byte("test job")
id, err := suite.beanstalkd.Put(payload, jackd.DefaultPutOpts())
defer suite.beanstalkd.Delete(id)
reservedID, reservedPayload, err := suite.beanstalkd.Reserve()
require.NoError(suite.T(), err)
err = suite.beanstalkd.Release(reservedID, jackd.DefaultReleaseOpts())
require.NoError(suite.T(), err)
peekedId, peekedPayload, err := suite.beanstalkd.PeekReady()
require.NoError(suite.T(), err)
assert.Equal(suite.T(), id, reservedID)
assert.Equal(suite.T(), payload, reservedPayload)
assert.Equal(suite.T(), id, peekedId)
assert.Equal(suite.T(), payload, peekedPayload)
}
func (suite *JackdSuite) TestPeek() {
payload := []byte("test job")
id, err := suite.beanstalkd.Put(payload, jackd.DefaultPutOpts())
defer suite.beanstalkd.Delete(id)
peekedId, peekedPayload, err := suite.beanstalkd.Peek(id)
require.NoError(suite.T(), err)
assert.Equal(suite.T(), id, peekedId)
assert.Equal(suite.T(), payload, peekedPayload)
}
func (suite *JackdSuite) TestPeekReady() {
payload := []byte("test job")
id, err := suite.beanstalkd.Put(payload, jackd.DefaultPutOpts())
defer suite.beanstalkd.Delete(id)
peekedId, peekedPayload, err := suite.beanstalkd.PeekReady()
require.NoError(suite.T(), err)
assert.Equal(suite.T(), id, peekedId)
assert.Equal(suite.T(), payload, peekedPayload)
}
func (suite *JackdSuite) TestPeekDelayed() {
opts := jackd.DefaultPutOpts()
opts.Delay = 1 * time.Second
payload := []byte("test delayed job")
id, err := suite.beanstalkd.Put(payload, opts)
defer suite.beanstalkd.Delete(id)
peekedId, peekedPayload, err := suite.beanstalkd.PeekDelayed()
require.NoError(suite.T(), err)
assert.Equal(suite.T(), id, peekedId)
assert.Equal(suite.T(), payload, peekedPayload)
}
func (suite *JackdSuite) TestBury() {
payload := []byte("test job")
id, err := suite.beanstalkd.Put(payload, jackd.DefaultPutOpts())
defer suite.beanstalkd.Delete(id)
require.NoError(suite.T(), err)
// In order to bury a job, it must be reserved first
_, _, err = suite.beanstalkd.ReserveJob(id)
require.NoError(suite.T(), err)
err = suite.beanstalkd.Bury(id, 0)
require.NoError(suite.T(), err)
peekedId, peekedPayload, err := suite.beanstalkd.PeekBuried()
require.NoError(suite.T(), err)
assert.Equal(suite.T(), id, peekedId)
assert.Equal(suite.T(), payload, peekedPayload)
}
func (suite *JackdSuite) TestKickBuried() {
payload := []byte("test job")
id, err := suite.beanstalkd.Put(payload, jackd.DefaultPutOpts())
defer suite.beanstalkd.Delete(id)
require.NoError(suite.T(), err)
// In order to bury a job, it must be reserved first
_, _, err = suite.beanstalkd.ReserveJob(id)
require.NoError(suite.T(), err)
err = suite.beanstalkd.Bury(id, 0)
require.NoError(suite.T(), err)
kicked, err := suite.beanstalkd.Kick(1)
require.NoError(suite.T(), err)
secondReservedID, secondReservedPayload, err := suite.beanstalkd.Reserve()
require.NoError(suite.T(), err)
assert.Equal(suite.T(), uint32(1), kicked)
assert.Equal(suite.T(), id, secondReservedID)
assert.Equal(suite.T(), payload, secondReservedPayload)
}
func (suite *JackdSuite) TestReserveJob() {
payload := []byte("test job")
id, err := suite.beanstalkd.Put(payload, jackd.DefaultPutOpts())
defer suite.beanstalkd.Delete(id)
reservedID, reservedPayload, err := suite.beanstalkd.ReserveJob(id)
require.NoError(suite.T(), err)
assert.Equal(suite.T(), id, reservedID)
assert.Equal(suite.T(), payload, reservedPayload)
}
func (suite *JackdSuite) TestReserveDelayedJob() {
opts := jackd.DefaultPutOpts()
opts.Delay = 1 * time.Second
payload := []byte("test delayed job")
id, err := suite.beanstalkd.Put(payload, opts)
defer suite.beanstalkd.Delete(id)
reservedID, reservedPayload, err := suite.beanstalkd.Reserve()
require.NoError(suite.T(), err)
assert.Equal(suite.T(), id, reservedID)
assert.Equal(suite.T(), payload, reservedPayload)
}
func (suite *JackdSuite) TestHandlesJobsWithNewLines() {
payload := []byte("test job\r\nwith line breaks")
id, err := suite.beanstalkd.Put(payload, jackd.DefaultPutOpts())
defer suite.beanstalkd.Delete(id)
reservedID, reservedPayload, err := suite.beanstalkd.Reserve()
require.NoError(suite.T(), err)
assert.Equal(suite.T(), id, reservedID)
assert.Equal(suite.T(), payload, reservedPayload)
}
func (suite *JackdSuite) TestHandlesMassiveJobs() {
payload := make([]byte, 50000)
_, err := rand.Read(payload)
require.NoError(suite.T(), err)
id, err := suite.beanstalkd.Put(payload, jackd.DefaultPutOpts())
defer suite.beanstalkd.Delete(id)
require.NoError(suite.T(), err)
reservedID, reservedPayload, err := suite.beanstalkd.Reserve()
require.NoError(suite.T(), err)
assert.Equal(suite.T(), id, reservedID)
assert.Equal(suite.T(), payload, reservedPayload)
}
func (suite *JackdSuite) TestUseTube() {
tube := "some-other-tube"
returnedTube, err := suite.beanstalkd.Use(tube)
require.NoError(suite.T(), err)
assert.Equal(suite.T(), tube, returnedTube)
}
func (suite *JackdSuite) TestWatchTube() {
tube := "some-other-tube"
count, err := suite.beanstalkd.Watch(tube)
require.NoError(suite.T(), err)
assert.Equal(suite.T(), uint32(2), count)
}
func (suite *JackdSuite) TestListTubes() {
tube := "some-other-tube"
_, err := suite.beanstalkd.Watch(tube)
require.NoError(suite.T(), err)
resp, err := suite.beanstalkd2.ListTubes()
require.NoError(suite.T(), err)
var tubes []string
err = yaml.Unmarshal(resp, &tubes)
require.NoError(suite.T(), err)
assert.Len(suite.T(), tubes, 2)
}
func (suite *JackdSuite) TestListTubeUsed() {
tube, err := suite.beanstalkd.ListTubeUsed()
require.NoError(suite.T(), err)
assert.Equal(suite.T(), "default", tube)
}
func (suite *JackdSuite) TestListTubesWatched() {
tube := "some-other-tube"
_, err := suite.beanstalkd.Watch(tube)
require.NoError(suite.T(), err)
_, err = suite.beanstalkd.Ignore("default")
require.NoError(suite.T(), err)
resp, err := suite.beanstalkd2.ListTubesWatched()
require.NoError(suite.T(), err)
var tubes []string
err = yaml.Unmarshal(resp, &tubes)
require.NoError(suite.T(), err)
assert.Len(suite.T(), tubes, 1)
}
func (suite *JackdSuite) TestPutReserveJobDifferentTube() {
tube := "some-other-tube"
_, err := suite.beanstalkd.Use(tube)
require.NoError(suite.T(), err)
payload := []byte("my awesome other tube job")
job, err := suite.beanstalkd.Put(payload, jackd.DefaultPutOpts())
defer suite.beanstalkd.Delete(job)
require.NoError(suite.T(), err)
_, err = suite.beanstalkd2.Watch(tube)
require.NoError(suite.T(), err)
reservedJob, reservedPayload, err := suite.beanstalkd2.Reserve()
defer suite.beanstalkd2.Delete(reservedJob)
require.NoError(suite.T(), err)
assert.Equal(suite.T(), job, reservedJob)
assert.Equal(suite.T(), payload, reservedPayload)
}
func (suite *JackdSuite) TestIgnoresTube() {
var defaultTubeJob, job uint32
defaultTubeJobPayload := []byte("my default job")
payload := []byte("my awesome other tube job")
tube := "some-other-tube"
defaultTubeJob, err := suite.beanstalkd.Put(defaultTubeJobPayload, jackd.DefaultPutOpts())
defer suite.beanstalkd.Delete(defaultTubeJob)
require.NoError(suite.T(), err)
_, err = suite.beanstalkd.Use(tube)
require.NoError(suite.T(), err)
job, err = suite.beanstalkd.Put(payload, jackd.DefaultPutOpts())
defer suite.beanstalkd.Delete(job)
require.NoError(suite.T(), err)
_, err = suite.beanstalkd2.Watch(tube)
require.NoError(suite.T(), err)
_, err = suite.beanstalkd2.Ignore("default")
require.NoError(suite.T(), err)
reservedJob, reservedPayload, err := suite.beanstalkd2.Reserve()
defer suite.beanstalkd2.Delete(reservedJob)
require.NoError(suite.T(), err)
assert.Equal(suite.T(), job, reservedJob)
assert.Equal(suite.T(), payload, reservedPayload)
}
func (suite *JackdSuite) TestPauseTube() {
payload := []byte("my default job")
pausedPayload := []byte("my awesome other tube job")
tube := "some-other-tube"
// Pause the other tube for five seconds
err := suite.beanstalkd.PauseTube(tube, 5*time.Second)
require.NoError(suite.T(), err)
// Ask the second client to watch this paused tube
_, err = suite.beanstalkd2.Watch(tube)
require.NoError(suite.T(), err)
// Put in a delayed job for one second in the default tube
opts := jackd.DefaultPutOpts()
opts.Delay = 1 * time.Second
job, err := suite.beanstalkd.Put(payload, opts)
defer suite.beanstalkd.Delete(job)
// Put in a job with no delay in the paused tube. Jobs going into the paused
// tube should have a delay now.
_, err = suite.beanstalkd.Use(tube)
require.NoError(suite.T(), err)
pausedJob, err := suite.beanstalkd.Put(pausedPayload, jackd.DefaultPutOpts())
defer suite.beanstalkd.Delete(pausedJob)
require.NoError(suite.T(), err)
// The reserved job should be the one from the default payload and not the paused
// tube job, even though that job has a 0 delay.
reservedJob, reservedPayload, err := suite.beanstalkd2.Reserve()
defer suite.beanstalkd2.Delete(reservedJob)
require.NoError(suite.T(), err)
assert.Equal(suite.T(), job, reservedJob)
assert.Equal(suite.T(), payload, reservedPayload)
}