|
| 1 | +import { RabbitMqGateway } from '../../../src/rabbitmq/RabbitMqGateway.ts'; |
| 2 | +import { EventEmitter } from 'stream'; |
| 3 | + |
| 4 | +function createMockChannel() { |
| 5 | + const assertQueueCalls: any[] = []; |
| 6 | + return { |
| 7 | + assertQueueCalls, |
| 8 | + assertExchange: jest.fn().mockResolvedValue(undefined), |
| 9 | + assertQueue: jest.fn().mockImplementation((_name, options) => { |
| 10 | + assertQueueCalls.push(options); |
| 11 | + return Promise.resolve({ queue: 'test-queue', messageCount: 0, consumerCount: 0 }); |
| 12 | + }), |
| 13 | + bindQueue: jest.fn().mockResolvedValue(undefined), |
| 14 | + prefetch: jest.fn().mockResolvedValue(undefined), |
| 15 | + consume: jest.fn().mockResolvedValue({ consumerTag: 'tag-1' }), |
| 16 | + on: jest.fn() |
| 17 | + }; |
| 18 | +} |
| 19 | + |
| 20 | +function createMockConnection(channel: ReturnType<typeof createMockChannel>) { |
| 21 | + return { |
| 22 | + createChannel: jest.fn().mockResolvedValue(channel), |
| 23 | + createConfirmChannel: jest.fn().mockResolvedValue({ |
| 24 | + on: jest.fn(), |
| 25 | + publish: jest.fn() |
| 26 | + }), |
| 27 | + on: jest.fn(), |
| 28 | + close: jest.fn().mockResolvedValue(undefined) |
| 29 | + }; |
| 30 | +} |
| 31 | + |
| 32 | +describe('RabbitMqGateway', () => { |
| 33 | + |
| 34 | + describe('x-consumer-timeout', () => { |
| 35 | + |
| 36 | + let channel: ReturnType<typeof createMockChannel>; |
| 37 | + let connection: ReturnType<typeof createMockConnection>; |
| 38 | + let gateway: RabbitMqGateway; |
| 39 | + |
| 40 | + beforeEach(() => { |
| 41 | + channel = createMockChannel(); |
| 42 | + connection = createMockConnection(channel); |
| 43 | + |
| 44 | + gateway = new RabbitMqGateway({ |
| 45 | + rabbitMqConnectionFactory: () => Promise.resolve(connection as any), |
| 46 | + process: new EventEmitter() as any |
| 47 | + }); |
| 48 | + }); |
| 49 | + |
| 50 | + afterEach(async () => { |
| 51 | + await gateway.disconnect(); |
| 52 | + }); |
| 53 | + |
| 54 | + it('sets x-consumer-timeout to handlerProcessTimeout + 1000 on durable queue', async () => { |
| 55 | + await gateway.subscribe({ |
| 56 | + exchange: 'test-exchange', |
| 57 | + queueName: 'test-queue', |
| 58 | + eventType: 'test.event', |
| 59 | + handler: jest.fn(), |
| 60 | + handlerProcessTimeout: 30_000 |
| 61 | + }); |
| 62 | + |
| 63 | + // assertQueue is called twice for durable queues: first for the dead letter queue, then for the main queue. |
| 64 | + // The main queue call has x-queue-type: 'quorum' and x-dead-letter-exchange. |
| 65 | + const durableQueueCall = channel.assertQueueCalls.find( |
| 66 | + (opts: any) => opts.arguments?.['x-dead-letter-exchange'] |
| 67 | + ); |
| 68 | + |
| 69 | + expect(durableQueueCall).toBeDefined(); |
| 70 | + expect(durableQueueCall.arguments['x-consumer-timeout']).toBe(31_000); |
| 71 | + }); |
| 72 | + |
| 73 | + it('sets x-consumer-timeout to default HANDLER_PROCESS_TIMEOUT + 1000 when not specified', async () => { |
| 74 | + await gateway.subscribe({ |
| 75 | + exchange: 'test-exchange', |
| 76 | + queueName: 'test-queue', |
| 77 | + eventType: 'test.event', |
| 78 | + handler: jest.fn() |
| 79 | + }); |
| 80 | + |
| 81 | + const durableQueueCall = channel.assertQueueCalls.find( |
| 82 | + (opts: any) => opts.arguments?.['x-dead-letter-exchange'] |
| 83 | + ); |
| 84 | + |
| 85 | + expect(durableQueueCall).toBeDefined(); |
| 86 | + expect(durableQueueCall.arguments['x-consumer-timeout']).toBe( |
| 87 | + RabbitMqGateway.HANDLER_PROCESS_TIMEOUT + 1_000 |
| 88 | + ); |
| 89 | + }); |
| 90 | + |
| 91 | + it('does not set x-consumer-timeout when handlerProcessTimeout is 0', async () => { |
| 92 | + await gateway.subscribe({ |
| 93 | + exchange: 'test-exchange', |
| 94 | + queueName: 'test-queue', |
| 95 | + eventType: 'test.event', |
| 96 | + handler: jest.fn(), |
| 97 | + handlerProcessTimeout: 0 |
| 98 | + }); |
| 99 | + |
| 100 | + const durableQueueCall = channel.assertQueueCalls.find( |
| 101 | + (opts: any) => opts.arguments?.['x-dead-letter-exchange'] |
| 102 | + ); |
| 103 | + |
| 104 | + expect(durableQueueCall).toBeDefined(); |
| 105 | + expect(durableQueueCall.arguments['x-consumer-timeout']).toBeUndefined(); |
| 106 | + }); |
| 107 | + |
| 108 | + it('sets default x-consumer-timeout on exclusive (fanout) queues', async () => { |
| 109 | + await gateway.subscribe({ |
| 110 | + exchange: 'test-exchange', |
| 111 | + handler: jest.fn() |
| 112 | + }); |
| 113 | + |
| 114 | + const exclusiveQueueCall = channel.assertQueueCalls.find( |
| 115 | + (opts: any) => opts.exclusive === true |
| 116 | + ); |
| 117 | + |
| 118 | + expect(exclusiveQueueCall).toBeDefined(); |
| 119 | + expect(exclusiveQueueCall.arguments['x-consumer-timeout']).toBe( |
| 120 | + RabbitMqGateway.HANDLER_PROCESS_TIMEOUT + 1_000 |
| 121 | + ); |
| 122 | + }); |
| 123 | + }); |
| 124 | +}); |
0 commit comments