|
| 1 | +import { default as IORedis } from 'ioredis'; |
| 2 | +import { v4 } from 'uuid'; |
| 3 | +import { describe, it, expect, vi, beforeAll, afterAll } from 'vitest'; |
| 4 | +import { Queue } from '../src'; |
| 5 | +import { removeAllQueueData } from '../src/utils'; |
| 6 | + |
| 7 | +describe('Repeat/JobScheduler error forwarding', () => { |
| 8 | + const redisHost = process.env.REDIS_HOST || 'localhost'; |
| 9 | + const prefix = process.env.BULLMQ_TEST_PREFIX || 'bull'; |
| 10 | + let connection: IORedis; |
| 11 | + |
| 12 | + beforeAll(async () => { |
| 13 | + connection = new IORedis(redisHost, { maxRetriesPerRequest: null }); |
| 14 | + }); |
| 15 | + |
| 16 | + afterAll(async () => { |
| 17 | + await connection.quit(); |
| 18 | + }); |
| 19 | + |
| 20 | + it('Queue should forward Repeat "error" to queue.on("error")', async () => { |
| 21 | + const queueName = `test-forward-repeat-${v4()}`; |
| 22 | + const queue = new Queue(queueName, { connection, prefix }); |
| 23 | + |
| 24 | + // Force repeat instance creation so the buggy listener is attached |
| 25 | + const repeat = await queue.repeat; |
| 26 | + await repeat.waitUntilReady(); |
| 27 | + |
| 28 | + const spy = vi.fn(); |
| 29 | + queue.on('error', spy); |
| 30 | + |
| 31 | + const err = new Error('repeat boom'); |
| 32 | + |
| 33 | + // Trigger the child error event directly |
| 34 | + (queue as any)._repeat.emit('error', err); |
| 35 | + |
| 36 | + // This EXPECTATION FAILS on current buggy code (spy called 0 times) |
| 37 | + expect(spy).toHaveBeenCalledTimes(1); |
| 38 | + expect(spy).toHaveBeenCalledWith(err); |
| 39 | + |
| 40 | + await queue.close(); |
| 41 | + await removeAllQueueData(new IORedis(redisHost), queueName); |
| 42 | + }); |
| 43 | + |
| 44 | + it('Queue should forward JobScheduler "error" to queue.on("error")', async () => { |
| 45 | + const queueName = `test-forward-scheduler-${v4()}`; |
| 46 | + const queue = new Queue(queueName, { connection, prefix }); |
| 47 | + |
| 48 | + // Force jobScheduler instance creation so the buggy listener is attached |
| 49 | + const jobScheduler = await queue.jobScheduler; |
| 50 | + await jobScheduler.waitUntilReady(); |
| 51 | + |
| 52 | + const spy = vi.fn(); |
| 53 | + queue.on('error', spy); |
| 54 | + |
| 55 | + const err = new Error('scheduler boom'); |
| 56 | + |
| 57 | + // Trigger the child error event directly |
| 58 | + (queue as any)._jobScheduler.emit('error', err); |
| 59 | + |
| 60 | + // This EXPECTATION FAILS on current buggy code (spy called 0 times) |
| 61 | + expect(spy).toHaveBeenCalledTimes(1); |
| 62 | + expect(spy).toHaveBeenCalledWith(err); |
| 63 | + |
| 64 | + await queue.close(); |
| 65 | + await removeAllQueueData(new IORedis(redisHost), queueName); |
| 66 | + }); |
| 67 | +}); |
0 commit comments