Skip to content

Commit c25c87c

Browse files
committed
feat: add more tests to improve coverage
1 parent fd89719 commit c25c87c

File tree

3 files changed

+116
-0
lines changed

3 files changed

+116
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*!
2+
* Additional RedisQueue tests: send() worker-only mode error
3+
*/
4+
import './mocks';
5+
import { expect } from 'chai';
6+
import { RedisQueue, IMQMode } from '../src';
7+
8+
function makeLogger() {
9+
return {
10+
log: (..._args: any[]) => undefined,
11+
info: (..._args: any[]) => undefined,
12+
warn: (..._args: any[]) => undefined,
13+
error: (..._args: any[]) => undefined,
14+
} as any;
15+
}
16+
17+
describe('RedisQueue.send() worker-only mode', function() {
18+
this.timeout(10000);
19+
20+
it('should throw when called in WORKER only mode', async () => {
21+
const logger = makeLogger();
22+
const rq: any = new RedisQueue('WorkerOnly', { logger }, IMQMode.WORKER);
23+
24+
let thrown: any;
25+
try {
26+
await rq.send('AnyQueue', { test: true });
27+
} catch (err) {
28+
thrown = err;
29+
}
30+
31+
expect(thrown).to.be.instanceof(TypeError);
32+
expect(`${thrown}`).to.include('WORKER only mode');
33+
34+
await rq.destroy().catch(() => undefined);
35+
});
36+
});
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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+
});
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*!
2+
* UDPClusterManager.free() coverage test
3+
*/
4+
import './mocks';
5+
import { expect } from 'chai';
6+
7+
describe('UDPClusterManager.free()', () => {
8+
it('should destroy all sockets via destroySocket and clear sockets map', async () => {
9+
const { UDPClusterManager } = await import('../src');
10+
const sockets = (UDPClusterManager as any).sockets as Record<string, any>;
11+
// prepare two mock sockets
12+
sockets['0.0.0.0:5555'] = {
13+
removeAllListeners: () => {},
14+
close: (cb: Function) => cb(),
15+
unref: () => {},
16+
};
17+
sockets['0.0.0.0:6666'] = {
18+
removeAllListeners: () => {},
19+
close: (cb: Function) => cb(),
20+
unref: () => {},
21+
};
22+
23+
await (UDPClusterManager as any).free();
24+
25+
expect(Object.keys((UDPClusterManager as any).sockets)).to.have.length(0);
26+
});
27+
});

0 commit comments

Comments
 (0)