|
| 1 | +/*! |
| 2 | + * UDPClusterManager parseBroadcastedMessage and startListening branch tests |
| 3 | + */ |
| 4 | +import './mocks'; |
| 5 | +import { expect } from 'chai'; |
| 6 | +import { UDPClusterManager } from '../src'; |
| 7 | + |
| 8 | +/** |
| 9 | + * Covers default parameters in startListening(options = {}) and |
| 10 | + * default destructuring for address = '' and timeout = '0' in |
| 11 | + * parseBroadcastedMessage(). |
| 12 | + */ |
| 13 | +describe('UDPClusterManager parse/start branches', () => { |
| 14 | + it('parseBroadcastedMessage: should apply defaults for empty address and timeout', () => { |
| 15 | + const parse = (UDPClusterManager as any).parseBroadcastedMessage as Function; |
| 16 | + const buf = Buffer.from(['name', 'id', 'UP'].join('\t')); |
| 17 | + const msg = parse(buf); |
| 18 | + expect(msg).to.include({ name: 'name', id: 'id', type: 'up' }); |
| 19 | + // address default => '' leads to host '' and port NaN |
| 20 | + expect(msg.host).to.equal(''); |
| 21 | + expect(Number.isNaN(msg.port)).to.equal(true); |
| 22 | + // timeout default '0' => 0 ms |
| 23 | + expect(msg.timeout).to.equal(0); |
| 24 | + }); |
| 25 | + |
| 26 | + it('startListening: should call listenBroadcastedMessages when called without options', () => { |
| 27 | + const mgr: any = new (UDPClusterManager as any)(); |
| 28 | + let called = false; |
| 29 | + const original = mgr.listenBroadcastedMessages; |
| 30 | + mgr.listenBroadcastedMessages = (..._args: any[]) => { called = true; }; |
| 31 | + try { |
| 32 | + (mgr as any).startListening(); |
| 33 | + expect(called).to.equal(true); |
| 34 | + } finally { |
| 35 | + mgr.listenBroadcastedMessages = original; |
| 36 | + } |
| 37 | + }); |
| 38 | +}); |
0 commit comments