Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions tests/portCheck.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void>((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<void>((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<void>((r) => sock.close(() => r()))
}
}
throw new Error('every bound udp port also read as taken on tcp')
})
})
Loading