|
| 1 | +/*! |
| 2 | + * IMQClient methods Unit Tests (subscribe/unsubscribe/broadcast + signals) |
| 3 | + */ |
| 4 | +import './mocks'; |
| 5 | +import { expect } from 'chai'; |
| 6 | +import * as sinon from 'sinon'; |
| 7 | +import { IMQClient, remote } from '..'; |
| 8 | +import { logger } from './mocks'; |
| 9 | + |
| 10 | +class MethodsClient extends IMQClient { |
| 11 | + // ensure there is at least one remote method (not used directly here) |
| 12 | + @remote() |
| 13 | + public async ping(): Promise<string> { |
| 14 | + return this.remoteCall<string>(...arguments); |
| 15 | + } |
| 16 | +} |
| 17 | + |
| 18 | +describe('IMQClient methods', () => { |
| 19 | + let client: MethodsClient; |
| 20 | + |
| 21 | + afterEach(async () => { |
| 22 | + try { await client?.destroy(); } catch { /* ignore */ } |
| 23 | + sinon.restore(); |
| 24 | + }); |
| 25 | + |
| 26 | + it('should delegate subscribe() to subscriptionImq with client name', async () => { |
| 27 | + client = new MethodsClient({ logger }); |
| 28 | + const subImq: any = (client as any).subscriptionImq; |
| 29 | + const spy = sinon.stub(subImq, 'subscribe').resolves(); |
| 30 | + const handler = sinon.spy(); |
| 31 | + await client.subscribe(handler as any); |
| 32 | + expect(spy.calledOnce).to.equal(true); |
| 33 | + expect(spy.firstCall.args[0]).to.equal(client.name); |
| 34 | + expect(spy.firstCall.args[1]).to.equal(handler); |
| 35 | + }); |
| 36 | + |
| 37 | + it('should delegate unsubscribe() to subscriptionImq', async () => { |
| 38 | + client = new MethodsClient({ logger }); |
| 39 | + const subImq: any = (client as any).subscriptionImq; |
| 40 | + const spy = sinon.stub(subImq, 'unsubscribe').resolves(); |
| 41 | + await client.unsubscribe(); |
| 42 | + expect(spy.calledOnce).to.equal(true); |
| 43 | + }); |
| 44 | + |
| 45 | + it('should delegate broadcast() to imq.publish with queueName', async () => { |
| 46 | + client = new MethodsClient({ logger }); |
| 47 | + const imq: any = (client as any).imq; |
| 48 | + const spy = sinon.stub(imq, 'publish').resolves(); |
| 49 | + const payload: any = { hello: 'world' }; |
| 50 | + await client.broadcast(payload); |
| 51 | + expect(spy.calledOnce).to.equal(true); |
| 52 | + expect(spy.firstCall.args[0]).to.equal(payload); |
| 53 | + expect(spy.firstCall.args[1]).to.equal(client.queueName); |
| 54 | + }); |
| 55 | + |
| 56 | + it('should handle process signals by calling destroy and then process.exit(0)', async () => { |
| 57 | + const callbacks: Array<() => any> = []; |
| 58 | + const onStub = sinon.stub(process as any, 'on').callsFake((sig: any, cb: any) => { |
| 59 | + callbacks.push(cb); |
| 60 | + return process as any; |
| 61 | + }); |
| 62 | + const exitStub = sinon.stub(process as any, 'exit'); |
| 63 | + const clock = sinon.useFakeTimers(); |
| 64 | + |
| 65 | + client = new MethodsClient({ logger }); |
| 66 | + const destroyStub = sinon.stub(client, 'destroy').resolves(); |
| 67 | + |
| 68 | + // invoke the first registered signal handler (e.g., SIGTERM) |
| 69 | + await callbacks[0](); |
| 70 | + // fast-forward shutdown timeout |
| 71 | + clock.tick(10000); // IMQ_SHUTDOWN_TIMEOUT default |
| 72 | + |
| 73 | + expect(destroyStub.calledOnce).to.equal(true); |
| 74 | + expect(exitStub.called).to.equal(true); |
| 75 | + expect(exitStub.firstCall.args[0]).to.equal(0); |
| 76 | + |
| 77 | + clock.restore(); |
| 78 | + onStub.restore(); |
| 79 | + exitStub.restore(); |
| 80 | + }); |
| 81 | +}); |
0 commit comments