-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.test.js
276 lines (221 loc) · 7.01 KB
/
index.test.js
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
const { expect } = require('chai')
const Jackd = require('../dist')
const YAML = require('yaml')
const crypto = require('crypto')
describe('jackd', function () {
it('can connect to and disconnect from beanstalkd', async function () {
const c = new Jackd({ useLegacyStringPayloads: true })
await c.connect()
await c.close()
})
describe('connectivity', function () {
setupTestSuiteLifecycleWithClient()
it('connected', function () {
expect(this.client.connected).to.be.ok
})
it('disconnected', async function () {
await this.client.disconnect()
expect(this.client.ocnnected).to.not.be.ok
})
})
describe('producers', function () {
setupTestSuiteLifecycleWithClient()
it('can insert jobs', async function () {
let id
try {
id = await this.client.put('some random job')
expect(id).to.be.ok
} finally {
if (id) await this.client.delete(id)
}
})
it('can insert jobs with objects', async function () {
let id
try {
id = await this.client.put({ foo: 'bar' })
expect(id).to.be.ok
const job = await this.client.reserve()
expect(String(job.payload)).to.equal('{"foo":"bar"}')
} finally {
if (id) await this.client.delete(id)
}
})
it('can insert jobs with priority', async function () {
let id
try {
id = await this.client.put({ foo: 'bar' }, { priority: 12342342 })
expect(id).to.be.ok
const job = await this.client.reserve()
expect(String(job.payload)).to.equal('{"foo":"bar"}')
} finally {
if (id) await this.client.delete(id)
}
})
})
describe('consumers', function () {
setupTestSuiteLifecycleWithClient()
it('can reserve jobs', async function () {
let id
try {
id = await this.client.put('some random job')
const job = await this.client.reserve()
expect(job.id).to.equal(id)
expect(String(job.payload)).to.equal('some random job')
} finally {
if (id) await this.client.delete(id)
}
})
it('can reserve delayed jobs', async function () {
let id
try {
id = await this.client.put('some random job', {
delay: 1
})
const job = await this.client.reserve()
expect(job.id).to.equal(id)
expect(String(job.payload)).to.equal('some random job')
} finally {
if (id) await this.client.delete(id)
}
})
it('can reserve jobs by id', async function () {
let id
try {
id = await this.client.put('some random job', {
delay: 1
})
const job = await this.client.reserveJob(id)
expect(String(job.payload)).to.equal('some random job')
} finally {
if (id) await this.client.delete(id)
}
})
it('handles not found', async function () {
try {
await this.client.reserveJob(4)
} catch (err) {
expect(err.message).to.equal('NOT_FOUND')
}
})
it('can insert and process jobs on a different tube', async function () {
let id
try {
await this.client.use('some-other-tube')
id = await this.client.put('some random job on another tube')
await this.client.watch('some-other-tube')
const job = await this.client.reserve()
expect(job.id).to.equal(id)
expect(String(job.payload)).to.equal('some random job on another tube')
} finally {
if (id) await this.client.delete(id)
}
})
it('will ignore jobs from default', async function () {
let id, defaultId
try {
defaultId = await this.client.put('job on default')
await this.client.use('some-other-tube')
id = await this.client.put('some random job on another tube')
await this.client.watch('some-other-tube')
await this.client.ignore('default')
const job = await this.client.reserve()
expect(job.id).to.equal(id)
expect(String(job.payload)).to.equal('some random job on another tube')
} finally {
if (id) await this.client.delete(id)
if (defaultId) await this.client.delete(defaultId)
}
})
it('handles multiple promises fired at once', async function () {
let id1, id2
try {
this.client.use('some-tube')
const firstJobPromise = this.client.put('some-job')
this.client.watch('some-random-tube')
this.client.use('some-another-tube')
const secondJobPromise = this.client.put('some-job')
id1 = await firstJobPromise
id2 = await secondJobPromise
} finally {
if (id1) await this.client.delete(id1)
if (id2) await this.client.delete(id2)
}
})
it('can receive huge jobs', async function () {
let id
try {
// job larger than a socket data frame
const hugeText =
crypto.randomBytes(15000).toString('hex') +
'\r\n' +
crypto.randomBytes(15000).toString('hex')
id = await this.client.put(hugeText)
const job = await this.client.reserve()
expect(job.id).to.equal(id)
expect(String(job.payload)).to.equal(hugeText)
} finally {
if (id) await this.client.delete(id)
}
})
it('can peek buried jobs', async function () {
let id
try {
await this.client.use('some-tube')
id = await this.client.put('some-job')
await this.client.watch('some-tube')
await this.client.reserve()
await this.client.bury(id)
const job = await this.client.peekBuried()
expect(job.id).to.equal(id)
} finally {
if (id) await this.client.delete(id)
}
})
})
describe('stats', function () {
setupTestSuiteLifecycleWithClient()
it('brings back stats', async function () {
const stats = await this.client.stats()
YAML.parse(stats)
})
})
describe('bugfixes', function () {
setupTestSuiteLifecycleWithClient()
it('can receive jobs with new lines jobs', async function () {
let id
try {
// job larger than a socket data frame
const payload = 'this job should not fail!\r\n'
id = await this.client.put(payload)
const job = await this.client.reserve()
expect(job.id).to.equal(id)
expect(String(job.payload)).to.equal(payload)
} finally {
if (id) await this.client.delete(id)
}
})
it('can continue execution after bad command', async function () {
let id
try {
// Bad command
await this.client.delete('nonexistent job')
} catch (err) {
expect(err).to.be.an('error')
}
try {
id = await this.client.put('my awesome job')
} finally {
if (id) await this.client.delete(id)
}
})
})
})
function setupTestSuiteLifecycleWithClient() {
beforeEach(async function () {
this.client = new Jackd()
await this.client.connect()
})
afterEach(async function () {
await this.client.close()
})
}