|
| 1 | +/*! |
| 2 | + * UDPClusterManager missing branches coverage |
| 3 | + */ |
| 4 | +import './mocks'; |
| 5 | +import { expect } from 'chai'; |
| 6 | +import * as sinon from 'sinon'; |
| 7 | +import { UDPClusterManager } from '../src'; |
| 8 | + |
| 9 | +describe('UDPClusterManager - cover remaining branches', () => { |
| 10 | + it('serverAliveWait should handle existing.timeout falsy (|| 0) path and remove on timeout', async () => { |
| 11 | + // Arrange cluster stub |
| 12 | + const server: any = { host: 'h', port: 1, timer: undefined, timeout: undefined, timestamp: undefined }; |
| 13 | + const cluster: any = { |
| 14 | + find: sinon.stub().callsFake((_s: any, _strict?: boolean) => server), |
| 15 | + remove: sinon.stub(), |
| 16 | + }; |
| 17 | + |
| 18 | + // Use fake timers to control setTimeout and Date |
| 19 | + const clock = sinon.useFakeTimers(); |
| 20 | + try { |
| 21 | + // make timestamp truthy |
| 22 | + clock.tick(1); |
| 23 | + // Alive correction > 0 ensures timer is scheduled even if timeout is falsy |
| 24 | + (UDPClusterManager as any).serverAliveWait(cluster, server, 1); |
| 25 | + // Advance time to trigger setTimeout callback and make delta >= currentTimeout (1ms) |
| 26 | + clock.tick(2); |
| 27 | + |
| 28 | + expect(cluster.remove.called).to.equal(true); |
| 29 | + } finally { |
| 30 | + clock.restore(); |
| 31 | + } |
| 32 | + }); |
| 33 | + |
| 34 | + it('destroySocket should call socket.unref() when socket is present', async () => { |
| 35 | + // Prepare fake socket with unref |
| 36 | + const unref = sinon.spy(); |
| 37 | + const removeAll = sinon.spy(); |
| 38 | + const sock: any = { |
| 39 | + removeAllListeners: removeAll, |
| 40 | + close: (cb: (err?: any) => void) => cb(), |
| 41 | + unref, |
| 42 | + }; |
| 43 | + const key = 'test-key'; |
| 44 | + (UDPClusterManager as any).sockets[key] = sock; |
| 45 | + await (UDPClusterManager as any).destroySocket(key, sock); |
| 46 | + expect(unref.called).to.equal(true); |
| 47 | + expect((UDPClusterManager as any).sockets[key]).to.equal(undefined); |
| 48 | + }); |
| 49 | + |
| 50 | + it('destroySocket should work when socket.unref() is absent (optional chaining negative branch)', async () => { |
| 51 | + const removeAll = sinon.spy(); |
| 52 | + const sock: any = { |
| 53 | + removeAllListeners: removeAll, |
| 54 | + close: (cb: (err?: any) => void) => cb(), |
| 55 | + // no unref method |
| 56 | + }; |
| 57 | + const key = 'test-key-2'; |
| 58 | + (UDPClusterManager as any).sockets[key] = sock; |
| 59 | + await (UDPClusterManager as any).destroySocket(key, sock); |
| 60 | + // should not throw, sockets map cleaned |
| 61 | + expect((UDPClusterManager as any).sockets[key]).to.equal(undefined); |
| 62 | + }); |
| 63 | +}); |
0 commit comments