-
-
Notifications
You must be signed in to change notification settings - Fork 271
Expand file tree
/
Copy pathworkTest.ts
More file actions
310 lines (211 loc) · 8.7 KB
/
Copy pathworkTest.ts
File metadata and controls
310 lines (211 loc) · 8.7 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
import { delay } from '../src/tools.ts'
import assert from 'node:assert'
import * as helper from './testHelper.ts'
import { JobWithMetadata, type PgBoss } from '../src/index.ts'
describe('work', function () {
it('should fail with no arguments', async function () {
assert.rejects(async () => {
this.boss = await helper.start(this.bossConfig) as PgBoss
await this.boss.work()
})
})
it('should fail if no callback provided', async function () {
assert.rejects(async () => {
this.boss = await helper.start(this.bossConfig) as PgBoss
await this.boss.work('foo')
})
})
it('should fail if options is not an object', async function () {
assert.rejects(async () => {
this.boss = await helper.start(this.bossConfig) as PgBoss
await this.boss.work('foo', async () => {}, 'nope')
})
})
it('offWork should fail without a name', async function () {
assert.rejects(async () => {
this.boss = await helper.start(this.bossConfig) as PgBoss
await this.boss.offWork()
})
})
it('should honor a custom polling interval', async function () {
this.boss = await helper.start(this.bossConfig) as PgBoss
const pollingIntervalSeconds = 1
const timeout = 5000
let processCount = 0
const jobCount = 10
for (let i = 0; i < jobCount; i++) {
await this.boss.send(this.schema)
}
await this.boss.work(this.schema, { pollingIntervalSeconds }, async () => {
processCount++
})
await delay(timeout)
assert.strictEqual(processCount, timeout / 1000 / pollingIntervalSeconds)
})
it('should honor when a worker is notified', async function () {
this.boss = await helper.start(this.bossConfig) as PgBoss
let processCount = 0
await this.boss.send(this.schema)
const workerId = await this.boss.work(this.schema, { pollingIntervalSeconds: 5 }, async () => processCount++)
await delay(500)
assert.strictEqual(processCount, 1)
await this.boss.send(this.schema)
this.boss.notifyWorker(workerId)
await delay(500)
assert.strictEqual(processCount, 2)
})
it('should remove a worker', async function () {
this.boss = await helper.start(this.bossConfig) as PgBoss
let receivedCount = 0
this.boss.work(this.schema, async () => {
receivedCount++
await this.boss!.offWork(this.schema)
})
await this.boss.send(this.schema)
await this.boss.send(this.schema)
await delay(5000)
assert.strictEqual(receivedCount, 1)
})
it('should remove a worker by id', async function () {
this.boss = await helper.start(this.bossConfig) as PgBoss
let receivedCount = 0
await this.boss.send(this.schema)
await this.boss.send(this.schema)
const id = await this.boss.work(this.schema, { pollingIntervalSeconds: 0.5 }, async () => {
receivedCount++
await this.boss!.offWork({ id })
})
await delay(2000)
assert.strictEqual(receivedCount, 1)
})
it('should handle a batch of jobs via batchSize', async function () {
this.boss = await helper.start(this.bossConfig) as PgBoss
const batchSize = 4
for (let i = 0; i < batchSize; i++) {
await this.boss.send(this.schema)
}
return new Promise((resolve) => {
this.boss!.work(this.schema, { batchSize }, async jobs => {
assert.strictEqual(jobs.length, batchSize)
resolve()
})
})
})
it('batchSize should auto-complete the jobs', async function () {
this.boss = await helper.start(this.bossConfig) as PgBoss
const jobId = await this.boss.send(this.schema)
await new Promise((resolve) => {
this.boss!.work(this.schema, { batchSize: 1 }, async jobs => {
assert.strictEqual(jobs.length, 1)
resolve()
})
})
await delay(500)
const job = await this.boss.getJobById(this.schema, jobId!)
assert.strictEqual(job!.state, 'completed')
})
it('returning promise applies backpressure', async function () {
this.boss = await helper.start(this.bossConfig) as PgBoss
const jobCount = 4
let processCount = 0
for (let i = 0; i < jobCount; i++) {
await this.boss.send(this.schema)
}
await this.boss.work(this.schema, async () => {
// delay slows down process fetch
await delay(2000)
processCount++
})
await delay(7000)
assert(processCount < jobCount)
})
it('completion should pass string wrapped in value prop', async function () {
this.boss = await helper.start(this.bossConfig) as PgBoss
const result = 'success'
const jobId = await this.boss.send(this.schema)
await this.boss.work(this.schema, async () => result)
await delay(1000)
const job = await this.boss.getJobById(this.schema, jobId!)
assert.strictEqual(job!.state, 'completed')
assert.strictEqual(job!.output.value, result)
})
it('handler result should be stored in output', async function () {
this.boss = await helper.start(this.bossConfig) as PgBoss
const something = 'clever'
const jobId = await this.boss.send(this.schema)
await this.boss.work(this.schema, async () => ({ something }))
await delay(1000)
const job = await this.boss.getJobById(this.schema, jobId!)
assert.strictEqual(job!.state, 'completed')
assert.strictEqual(job!.output.something, something)
})
it('job cab be deleted in handler', async function () {
this.boss = await helper.start(this.bossConfig) as PgBoss
const jobId = await this.boss.send(this.schema)
await this.boss.work(this.schema, async ([job]) => this.boss.deleteJob(this.schema, job.id))
await delay(1000)
const job = await this.boss.getJobById(this.schema, jobId)
assert(!job)
})
it('should allow multiple workers to the same this.schema per instance', async function () {
this.boss = await helper.start(this.bossConfig) as PgBoss
await this.boss.work(this.schema, async () => {})
await this.boss.work(this.schema, async () => {})
})
it('should honor the includeMetadata option', async function () {
this.boss = await helper.start(this.bossConfig) as PgBoss
await this.boss.send(this.schema)
return new Promise((resolve) => {
this.boss!.work(this.schema, { includeMetadata: true }, async ([job]) => {
assert(job.startedOn !== undefined)
resolve()
})
})
})
it('should fail job at expiration in worker', async function () {
this.boss = await helper.start({ ...this.bossConfig, supervise: false })
const jobId = await this.boss.send(this.schema, null, { retryLimit: 0, expireInSeconds: 1 })
await this.boss.work(this.schema, () => delay(2000))
await delay(2000)
const job = await this.boss.getJobById(this.schema, jobId!)
assert.strictEqual(job!.state, 'failed')
assert(job!.output!.message!.includes('handler execution exceeded'))
})
it('should fail a batch of jobs at expiration in worker', async function () {
this.boss = await helper.start({ ...this.bossConfig, supervise: false })
const jobId1 = await this.boss.send(this.schema, null, { retryLimit: 0, expireInSeconds: 1 })
const jobId2 = await this.boss.send(this.schema, null, { retryLimit: 0, expireInSeconds: 1 })
await this.boss.work(this.schema, { batchSize: 2 }, () => delay(2000))
await delay(2000)
const job1 = await this.boss.getJobById(this.schema, jobId1!) as JobWithMetadata
const job2 = await this.boss.getJobById(this.schema, jobId2!) as JobWithMetadata
assert.strictEqual(job1.state, 'failed')
assert(job1.output.message.includes('handler execution exceeded'))
assert.strictEqual(job2.state, 'failed')
assert(job2.output.message.includes('handler execution exceeded'))
})
it('should emit wip event every 2s for workers', async function () {
this.boss = await helper.start(this.bossConfig) as PgBoss
const firstWipEvent = new Promise(resolve => this.boss!.once('wip', resolve))
await this.boss.send(this.schema)
await this.boss.work(this.schema, { pollingIntervalSeconds: 1 }, () => delay(2000))
const wip1 = await firstWipEvent
await this.boss.send(this.schema)
assert.strictEqual(wip1.length, 1)
const secondWipEvent = new Promise(resolve => this.boss!.once('wip', resolve))
const wip2 = await secondWipEvent
assert.strictEqual(wip2.length, 1)
})
it('should reject work() after stopping', async function () {
this.boss = await helper.start(this.bossConfig) as PgBoss
await this.boss.stop({ wait: true })
assert.rejects(async () => {
await this.boss!.work(this.schema, async () => {})
})
})
it('should allow send() after stopping', async function () {
this.boss = await helper.start(this.bossConfig) as PgBoss
this.boss.stop({ wait: true, close: false })
await this.boss.send(this.schema)
})
})