Skip to content

Commit b9f6a5a

Browse files
committed
feat: add more tests to improve test coverage
1 parent 7f85eb6 commit b9f6a5a

File tree

3 files changed

+172
-0
lines changed

3 files changed

+172
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*!
2+
* Cover ClusteredRedisQueue.addServerWithQueueInitializing with initializeQueue=false
3+
*/
4+
import './mocks';
5+
import { expect } from 'chai';
6+
import { ClusteredRedisQueue } from '../src';
7+
import { ClusterManager } from '../src/ClusterManager';
8+
9+
const server = { host: '127.0.0.1', port: 6380 };
10+
11+
describe('ClusteredRedisQueue.addServerWithQueueInitializing(false)', () => {
12+
it('should add server without initializing queue and not emit initialized', async () => {
13+
const manager = new (ClusterManager as any)();
14+
const cq: any = new ClusteredRedisQueue('NoInit', { clusterManagers: [manager] });
15+
16+
let initializedCalled = false;
17+
(cq as any).clusterEmitter.on('initialized', () => { initializedCalled = true; });
18+
19+
// call private method via any to cover branch
20+
(cq as any).addServerWithQueueInitializing(server, false);
21+
22+
// should have server and imq added
23+
expect(cq.servers.length).to.be.greaterThan(0);
24+
expect(cq.imqs.length).to.be.greaterThan(0);
25+
// queueLength updated
26+
expect(cq.queueLength).to.equal(cq.imqs.length);
27+
// initialized not emitted
28+
expect(initializedCalled).to.equal(false);
29+
30+
await cq.destroy();
31+
});
32+
});
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*!
2+
* UDPClusterManager.destroySocket() branch coverage tests
3+
*/
4+
import './mocks';
5+
import { expect } from 'chai';
6+
import { UDPClusterManager } from '../src';
7+
8+
describe('UDPClusterManager.destroySocket()', () => {
9+
it('should resolve when socket has no close() function', async () => {
10+
const destroy = (UDPClusterManager as any).destroySocket as Function;
11+
const fakeSocket: any = { /* no close, no removeAllListeners */ };
12+
13+
await destroy('0.0.0.0:63000', fakeSocket);
14+
});
15+
16+
it('should reject when removeAllListeners throws inside try-block', async () => {
17+
const destroy = (UDPClusterManager as any).destroySocket as Function;
18+
const fakeSocket: any = {
19+
removeAllListeners: () => { throw new Error('boom'); },
20+
close: (cb: Function) => cb && cb(),
21+
};
22+
23+
let thrown = null as any;
24+
try {
25+
await destroy('1.1.1.1:63000', fakeSocket);
26+
} catch (e) {
27+
thrown = e;
28+
}
29+
expect(thrown).to.be.instanceOf(Error);
30+
expect((thrown as Error).message).to.equal('boom');
31+
});
32+
33+
it('should remove socket entry and unref after successful close()', async () => {
34+
const destroy = (UDPClusterManager as any).destroySocket as Function;
35+
const sockets = (UDPClusterManager as any).sockets as Record<string, any>;
36+
const key = '9.9.9.9:65000';
37+
38+
let unrefCalled = false;
39+
const fakeSocket: any = {
40+
removeAllListeners: () => {},
41+
close: (cb: Function) => cb && cb(),
42+
unref: () => { unrefCalled = true; },
43+
};
44+
45+
sockets[key] = fakeSocket;
46+
await destroy(key, fakeSocket);
47+
48+
expect(unrefCalled).to.equal(true);
49+
expect(sockets[key]).to.be.undefined;
50+
});
51+
});
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*!
2+
* Additional branch coverage for UDPClusterManager
3+
*/
4+
import './mocks';
5+
import { expect } from 'chai';
6+
import { UDPClusterManager } from '../src';
7+
8+
describe('UDPClusterManager additional branches', () => {
9+
describe('processMessageOnCluster added-path', () => {
10+
const processMessageOnCluster = (UDPClusterManager as any).processMessageOnCluster as any;
11+
12+
it('should call serverAliveWait when server is added and found (added truthy)', async () => {
13+
const calls: any[] = [];
14+
const addedServer: any = { id: 'id', host: '127.0.0.1', port: 6379 };
15+
const cluster: any = {
16+
add: (message: any) => { calls.push(['add', message]); },
17+
find: (message: any, strict?: boolean) => strict ? addedServer : undefined,
18+
};
19+
const original = (UDPClusterManager as any).serverAliveWait;
20+
let waited = false;
21+
(UDPClusterManager as any).serverAliveWait = (...args: any[]) => {
22+
waited = true;
23+
};
24+
25+
processMessageOnCluster(cluster, { id: 'id', name: 'n', type: 'up', host: 'h', port: 1, timeout: 0 }, 5);
26+
27+
// allow microtask queue
28+
await new Promise(res => setTimeout(res, 0));
29+
30+
expect(waited).to.equal(true);
31+
// restore
32+
(UDPClusterManager as any).serverAliveWait = original;
33+
});
34+
35+
it('should not call serverAliveWait when added not found', async () => {
36+
const cluster: any = {
37+
add: (_: any) => undefined,
38+
find: (_: any, __?: boolean) => undefined,
39+
};
40+
const original = (UDPClusterManager as any).serverAliveWait;
41+
let waited = false;
42+
(UDPClusterManager as any).serverAliveWait = () => { waited = true; };
43+
44+
processMessageOnCluster(cluster, { id: 'id', name: 'n', type: 'up', host: 'h', port: 1, timeout: 0 }, 5);
45+
await new Promise(res => setTimeout(res, 0));
46+
47+
expect(waited).to.equal(false);
48+
(UDPClusterManager as any).serverAliveWait = original;
49+
});
50+
});
51+
52+
describe('serverAliveWait branches', () => {
53+
const serverAliveWait = (UDPClusterManager as any).serverAliveWait as any;
54+
55+
it('should return early when computed timeout is <= 0', () => {
56+
const cluster: any = { find: () => ({}) };
57+
const server: any = {};
58+
59+
serverAliveWait(cluster, server, 0); // no message and correction 0 => timeout 0
60+
61+
expect(server.timer).to.equal(undefined);
62+
});
63+
64+
it('should remove when no timestamp is present on existing (timer callback path)', (done) => {
65+
const removed: any[] = [];
66+
const server: any = { timeout: 0 };
67+
const cluster: any = {
68+
find: () => server,
69+
remove: (s: any) => { removed.push(s); },
70+
};
71+
72+
// use small correction to trigger timeout quickly
73+
serverAliveWait(cluster, server, 1);
74+
75+
// wipe timestamp before timeout fires to force the branch
76+
server.timestamp = undefined;
77+
78+
setTimeout(() => {
79+
try {
80+
expect(removed.length).to.equal(1);
81+
expect(server.timer).to.equal(undefined);
82+
done();
83+
} catch (e) {
84+
done(e as any);
85+
}
86+
}, 10);
87+
});
88+
});
89+
});

0 commit comments

Comments
 (0)