diff --git a/tests/portCheck.test.ts b/tests/portCheck.test.ts index 87a786d..9075eca 100644 --- a/tests/portCheck.test.ts +++ b/tests/portCheck.test.ts @@ -22,11 +22,25 @@ describe('isPortFree', () => { const port = sock.address().port try { expect(await isPortFree(port, 'udp')).toBe(false) - // tcp and udp ports are separate, the tcp side of it is still free - expect(await isPortFree(port, 'tcp')).toBe(true) } finally { await new Promise((r) => sock.close(() => r())) } expect(await isPortFree(port, 'udp')).toBe(true) }) + + it('checks tcp and udp separately', async () => { + // The OS only promises the udp port it hands out is free on udp. Another + // program can hold the same number on tcp, so look for one where it doesn't. + for (let attempt = 0; attempt < 20; attempt++) { + const sock = createSocket('udp4') + await new Promise((r) => sock.bind(0, '127.0.0.1', () => r())) + const port = sock.address().port + try { + if (await isPortFree(port, 'tcp')) return + } finally { + await new Promise((r) => sock.close(() => r())) + } + } + throw new Error('every bound udp port also read as taken on tcp') + }) })