|
| 1 | +/*! |
| 2 | + * Additional RedisQueue tests: unsubscribe() cleanup path |
| 3 | + */ |
| 4 | +import './mocks'; |
| 5 | +import { expect } from 'chai'; |
| 6 | +import * as sinon from 'sinon'; |
| 7 | +import { RedisQueue } from '../src'; |
| 8 | + |
| 9 | +function makeLogger() { |
| 10 | + return { |
| 11 | + log: (..._args: any[]) => undefined, |
| 12 | + info: (..._args: any[]) => undefined, |
| 13 | + warn: (..._args: any[]) => undefined, |
| 14 | + error: (..._args: any[]) => undefined, |
| 15 | + } as any; |
| 16 | +} |
| 17 | + |
| 18 | +describe('RedisQueue.unsubscribe()', function() { |
| 19 | + this.timeout(10000); |
| 20 | + |
| 21 | + it('should cleanup subscription channel when present', async () => { |
| 22 | + const logger = makeLogger(); |
| 23 | + const rq: any = new RedisQueue('SubUnsub', { logger }); |
| 24 | + await rq.start(); |
| 25 | + |
| 26 | + const handler = sinon.spy(); |
| 27 | + await rq.subscribe('SubUnsub', handler); |
| 28 | + |
| 29 | + expect(rq.subscription).to.be.ok; |
| 30 | + expect(rq.subscriptionName).to.equal('SubUnsub'); |
| 31 | + |
| 32 | + const unsubSpy = sinon.spy(rq.subscription, 'unsubscribe'); |
| 33 | + const ralSpy = sinon.spy(rq.subscription, 'removeAllListeners'); |
| 34 | + const disconnectSpy = sinon.spy(rq.subscription, 'disconnect'); |
| 35 | + const quitSpy = sinon.spy(rq.subscription, 'quit'); |
| 36 | + |
| 37 | + await rq.unsubscribe(); |
| 38 | + |
| 39 | + expect(unsubSpy.calledOnce).to.equal(true); |
| 40 | + expect(ralSpy.calledOnce).to.equal(true); |
| 41 | + expect(disconnectSpy.calledOnce).to.equal(true); |
| 42 | + expect(quitSpy.calledOnce).to.equal(true); |
| 43 | + expect(rq.subscription).to.equal(undefined); |
| 44 | + expect(rq.subscriptionName).to.equal(undefined); |
| 45 | + |
| 46 | + unsubSpy.restore(); |
| 47 | + ralSpy.restore(); |
| 48 | + disconnectSpy.restore(); |
| 49 | + quitSpy.restore(); |
| 50 | + |
| 51 | + await rq.destroy().catch(() => undefined); |
| 52 | + }); |
| 53 | +}); |
0 commit comments