|
| 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