Skip to content

Commit 7f85eb6

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

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*!
2+
* Additional RedisQueue tests for processCleanup branches
3+
*/
4+
import './mocks';
5+
import { expect } from 'chai';
6+
import { RedisQueue, uuid } from '../src';
7+
import { RedisClientMock } from './mocks';
8+
9+
describe('RedisQueue.processCleanup extra branches', function() {
10+
this.timeout(5000);
11+
12+
it('should remove scanned keys that do not match any connectedKey (different prefix)', async () => {
13+
const name = uuid();
14+
const rq: any = new RedisQueue(name, {
15+
logger: console,
16+
cleanup: true,
17+
prefix: 'imqX',
18+
cleanupFilter: '*',
19+
});
20+
21+
// start to create reader/writer/watcher with connection names
22+
await rq.start();
23+
24+
// Create an orphan worker key with a different prefix so it won't include any connectedKey
25+
const orphanKey = 'imqY:orphan:worker:someuuid:123456';
26+
(RedisClientMock as any).__queues__[orphanKey] = ['payload'];
27+
28+
// Sanity: ensure the key is present before cleanup
29+
expect((RedisClientMock as any).__queues__[orphanKey]).to.be.ok;
30+
31+
await rq.processCleanup();
32+
33+
// The orphan key should be deleted by cleanup (true branch of keysToRemove filter)
34+
expect((RedisClientMock as any).__queues__[orphanKey]).to.be.undefined;
35+
36+
await rq.destroy();
37+
});
38+
});
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*!
2+
* UDPClusterManager.selectNetworkInterface() branch coverage tests
3+
*/
4+
import './mocks';
5+
import { expect } from 'chai';
6+
import * as mock from 'mock-require';
7+
8+
describe('UDPClusterManager.selectNetworkInterface()', () => {
9+
it('should return default when broadcastAddress is undefined', async () => {
10+
const { UDPClusterManager } = await import('../src');
11+
const select = (UDPClusterManager as any).selectNetworkInterface as Function;
12+
const res = select({});
13+
expect(res).to.equal('0.0.0.0');
14+
});
15+
16+
it('should return default when broadcastAddress equals limitedBroadcastAddress', async () => {
17+
const { UDPClusterManager } = await import('../src');
18+
const select = (UDPClusterManager as any).selectNetworkInterface as Function;
19+
const res = select({ broadcastAddress: '127.0.0.255', limitedBroadcastAddress: '127.0.0.255' });
20+
expect(res).to.equal('0.0.0.0');
21+
});
22+
23+
it('should continue on undefined interface entry and still select matching address', () => {
24+
// Re-mock os.networkInterfaces to include an undefined entry
25+
const os = require('node:os');
26+
const networkInterfaces = () => ({ bad: undefined, lo: [{ address: '127.0.0.1', family: 'IPv4' }] });
27+
mock.stop('os');
28+
mock('os', Object.assign({}, os, { networkInterfaces }));
29+
// Re-require the module to capture new binding
30+
const { UDPClusterManager } = mock.reRequire('../src/UDPClusterManager');
31+
const res = (UDPClusterManager as any).selectNetworkInterface({ broadcastAddress: '127.0.0.255', limitedBroadcastAddress: '255.255.255.255' });
32+
expect(res).to.equal('127.0.0.1');
33+
34+
// restore to base mocks for other tests
35+
mock.stop('os');
36+
mock.reRequire('./mocks/os');
37+
mock.reRequire('../src/UDPClusterManager');
38+
});
39+
40+
it('should select matching interface address when not equal to limited broadcast', async () => {
41+
const { UDPClusterManager } = await import('../src');
42+
const select = (UDPClusterManager as any).selectNetworkInterface as Function;
43+
const res = select({ broadcastAddress: '127.0.0.255', limitedBroadcastAddress: '255.255.255.255' });
44+
expect(res).to.equal('127.0.0.1');
45+
});
46+
});

0 commit comments

Comments
 (0)