-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathqueue_manager.spec.ts
More file actions
324 lines (271 loc) · 8.42 KB
/
queue_manager.spec.ts
File metadata and controls
324 lines (271 loc) · 8.42 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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
import { test } from '@japa/runner'
import * as errors from '../src/exceptions.js'
import { QueueManager } from '../src/queue_manager.js'
import { sync } from '../src/drivers/sync_adapter.js'
import { MemoryLogger } from './_mocks/memory_logger.js'
import type { Adapter } from '../src/contracts/adapter.js'
test.group('QueueManager', () => {
test('should validate adapter presence', async ({ assert }) => {
assert.plan(2)
try {
await QueueManager.init({
default: 'sync',
adapters: {},
})
} catch (error) {
assert.instanceOf(error, errors.E_CONFIGURATION_ERROR)
assert.equal(
error.message,
'Configuration error. Reason: At least one adapter must be configured'
)
}
})
test('should validate default adapter presence', async ({ assert }) => {
assert.plan(2)
try {
await QueueManager.init({
default: '',
adapters: { sync: sync() },
})
} catch (error) {
assert.instanceOf(error, errors.E_CONFIGURATION_ERROR)
assert.equal(error.message, 'Configuration error. Reason: Default adapter must be specified')
}
})
test('should validate that adapter is a function', async ({ assert }) => {
assert.plan(2)
try {
await QueueManager.init({
default: 'sync',
adapters: { sync: 'not-a-function' as any },
})
} catch (error) {
assert.instanceOf(error, errors.E_CONFIGURATION_ERROR)
assert.equal(
error.message,
'Configuration error. Reason: Adapter "sync" must be a factory function'
)
}
})
test('should validate default adapter existence in adapters', async ({ assert }) => {
assert.plan(2)
try {
await QueueManager.init({
default: 'missing',
adapters: { sync: sync() },
})
} catch (error) {
assert.instanceOf(error, errors.E_CONFIGURATION_ERROR)
assert.equal(
error.message,
'Configuration error. Reason: Default adapter "missing" not found in adapters configuration'
)
}
})
test('should expose a config resolver after initialization', async ({ assert }) => {
await QueueManager.init({
default: 'sync',
adapters: { sync: sync() },
})
const resolver = QueueManager.getConfigResolver()
assert.exists(resolver)
})
test('should expose the configured logger', async ({ assert }) => {
const logger = new MemoryLogger()
await QueueManager.init({
default: 'sync',
adapters: { sync: sync() },
logger,
})
assert.strictEqual(QueueManager.getLogger(), logger)
})
test('should throw E_QUEUE_NOT_INITIALIZED when use() called before init()', async ({
assert,
}) => {
assert.plan(2)
await QueueManager.destroy()
try {
QueueManager.use()
} catch (error) {
assert.instanceOf(error, errors.E_QUEUE_NOT_INITIALIZED)
assert.equal(
error.message,
'QueueManager is not initialized. Call QueueManager.init() before using it.'
)
}
})
test('should throw E_ADAPTER_INIT_ERROR when adapter factory throws', async ({ assert }) => {
assert.plan(2)
await QueueManager.init({
default: 'broken',
adapters: {
broken: () => {
throw new Error('Connection failed')
},
},
})
try {
QueueManager.use()
} catch (error) {
assert.instanceOf(error, errors.E_ADAPTER_INIT_ERROR)
assert.equal(
error.message,
'Failed to initialize adapter "broken". Reason: Connection failed'
)
}
})
test('should log warning when locations match no jobs', async ({ assert }) => {
const logger = new MemoryLogger()
await QueueManager.init({
default: 'sync',
adapters: { sync: sync() },
locations: ['./non-existent-path/**/*.ts'],
logger,
})
assert.equal(logger.logs.length, 1)
assert.equal(logger.logs[0].level, 'warn')
assert.include(logger.logs[0].message, 'No jobs found for locations')
})
test('should fake adapters and restore them', async ({ assert }) => {
await QueueManager.init({
default: 'sync',
adapters: { sync: sync() },
})
const original = QueueManager.use()
const fakeAdapter = QueueManager.fake()
assert.strictEqual(QueueManager.use(), fakeAdapter)
QueueManager.restore()
assert.strictEqual(QueueManager.use(), original)
await QueueManager.destroy()
})
test('should restore fake using Symbol.dispose', async ({ assert }) => {
await QueueManager.init({
default: 'sync',
adapters: { sync: sync() },
})
const original = QueueManager.use()
{
using fake = QueueManager.fake()
assert.strictEqual(QueueManager.use(), fake)
}
assert.strictEqual(QueueManager.use(), original)
await QueueManager.destroy()
})
test('should destroy existing adapter instances before reinitializing', async ({
assert,
cleanup,
}) => {
const adapters: Adapter[] = []
let destroyedCount = 0
const createAdapter = (): Adapter => ({
setWorkerId() {},
pop: async () => null,
popFrom: async () => null,
recoverStalledJobs: async () => 0,
completeJob: async () => {},
failJob: async () => {},
retryJob: async () => {},
getJob: async () => null,
push: async () => {},
pushOn: async () => {},
pushLater: async () => {},
pushLaterOn: async () => {},
pushMany: async () => {},
pushManyOn: async () => {},
size: async () => 0,
sizeOf: async () => 0,
destroy: async () => {
destroyedCount++
},
upsertSchedule: async () => 'schedule-id',
createSchedule: async () => 'schedule-id',
getSchedule: async () => null,
listSchedules: async () => [],
updateSchedule: async () => {},
deleteSchedule: async () => {},
claimDueSchedule: async () => null,
})
cleanup(async () => {
await QueueManager.destroy()
})
await QueueManager.init({
default: 'custom',
adapters: {
custom: () => {
const adapter = createAdapter()
adapters.push(adapter)
return adapter
},
},
})
const firstAdapter = QueueManager.use()
await QueueManager.init({
default: 'custom',
adapters: {
custom: () => {
const adapter = createAdapter()
adapters.push(adapter)
return adapter
},
},
})
const secondAdapter = QueueManager.use()
assert.strictEqual(firstAdapter, adapters[0])
assert.strictEqual(secondAdapter, adapters[1])
assert.equal(destroyedCount, 1)
})
test('should reset fake state when reinitializing', async ({ assert, cleanup }) => {
type LabeledAdapter = Adapter & { label: string }
const createAdapter = (label: string): LabeledAdapter => ({
label,
setWorkerId() {},
pop: async () => null,
popFrom: async () => null,
recoverStalledJobs: async () => 0,
completeJob: async () => {},
failJob: async () => {},
retryJob: async () => {},
getJob: async () => null,
push: async () => {},
pushOn: async () => {},
pushLater: async () => {},
pushLaterOn: async () => {},
pushMany: async () => {},
pushManyOn: async () => {},
size: async () => 0,
sizeOf: async () => 0,
destroy: async () => {},
upsertSchedule: async () => 'schedule-id',
createSchedule: async () => 'schedule-id',
getSchedule: async () => null,
listSchedules: async () => [],
updateSchedule: async () => {},
deleteSchedule: async () => {},
claimDueSchedule: async () => null,
})
cleanup(async () => {
await QueueManager.destroy()
})
await QueueManager.init({
default: 'custom',
adapters: {
custom: () => createAdapter('first'),
},
})
const firstAdapter = QueueManager.use() as LabeledAdapter
const firstFakeAdapter = QueueManager.fake()
await QueueManager.init({
default: 'custom',
adapters: {
custom: () => createAdapter('second'),
},
})
const secondFakeAdapter = QueueManager.fake()
assert.notStrictEqual(secondFakeAdapter, firstFakeAdapter)
assert.strictEqual(QueueManager.use(), secondFakeAdapter)
QueueManager.restore()
const restoredAdapter = QueueManager.use() as LabeledAdapter
assert.notStrictEqual(restoredAdapter, firstAdapter)
assert.equal(restoredAdapter.label, 'second')
})
})