|
| 1 | +import { expect } from 'aegir/chai' |
| 2 | +import net from 'node:net' |
| 3 | +import { promisify } from 'node:util' |
| 4 | +import { mockUpgrader } from '@libp2p/interface-mocks' |
| 5 | +import { multiaddr } from '@multiformats/multiaddr' |
| 6 | +import { TCP } from '../src/index.js' |
| 7 | + |
| 8 | +describe('maxConnections', () => { |
| 9 | + const afterEachCallbacks: Array<() => Promise<any> | any> = [] |
| 10 | + afterEach(async () => { |
| 11 | + await Promise.all(afterEachCallbacks.map(fn => fn())) |
| 12 | + afterEachCallbacks.length = 0 |
| 13 | + }) |
| 14 | + |
| 15 | + it('reject dial of connection above maxConnections', async () => { |
| 16 | + const maxConnections = 2 |
| 17 | + const socketCount = 4 |
| 18 | + const port = 9900 |
| 19 | + |
| 20 | + const seenRemoteConnections = new Set<string>() |
| 21 | + const tcp = new TCP({ maxConnections }) |
| 22 | + |
| 23 | + const upgrader = mockUpgrader() |
| 24 | + const listener = tcp.createListener({ upgrader }) |
| 25 | + // eslint-disable-next-line @typescript-eslint/promise-function-async |
| 26 | + afterEachCallbacks.push(() => listener.close()) |
| 27 | + await listener.listen(multiaddr(`/ip4/127.0.0.1/tcp/${port}`)) |
| 28 | + |
| 29 | + listener.addEventListener('connection', (conn) => { |
| 30 | + seenRemoteConnections.add(conn.detail.remoteAddr.toString()) |
| 31 | + }) |
| 32 | + |
| 33 | + const sockets: net.Socket[] = [] |
| 34 | + |
| 35 | + for (let i = 0; i < socketCount; i++) { |
| 36 | + const socket = net.connect({ port }) |
| 37 | + sockets.push(socket) |
| 38 | + |
| 39 | + // eslint-disable-next-line @typescript-eslint/promise-function-async |
| 40 | + afterEachCallbacks.unshift(async () => { |
| 41 | + if (!socket.destroyed) { |
| 42 | + socket.destroy() |
| 43 | + await new Promise((resolve) => socket.on('close', resolve)) |
| 44 | + } |
| 45 | + }) |
| 46 | + |
| 47 | + // Wait for connection so the order of sockets is stable, sockets expected to be alive are always [0,1] |
| 48 | + await new Promise<void>((resolve, reject) => { |
| 49 | + socket.on('connect', () => { |
| 50 | + resolve() |
| 51 | + }) |
| 52 | + socket.on('error', (err) => { |
| 53 | + reject(err) |
| 54 | + }) |
| 55 | + }) |
| 56 | + } |
| 57 | + |
| 58 | + // With server.maxConnections the TCP socket is created and the initial handshake is completed |
| 59 | + // Then in the server handler NodeJS javascript code will call socket.emit('drop') if over the limit |
| 60 | + // https://github.com/nodejs/node/blob/fddc701d3c0eb4520f2af570876cc987ae6b4ba2/lib/net.js#L1706 |
| 61 | + |
| 62 | + // Wait for some time for server to drop all sockets above limit |
| 63 | + await promisify(setTimeout)(250) |
| 64 | + |
| 65 | + expect(seenRemoteConnections.size).equals(maxConnections, 'wrong serverConnections') |
| 66 | + |
| 67 | + for (let i = 0; i < socketCount; i++) { |
| 68 | + const socket = sockets[i] |
| 69 | + |
| 70 | + if (i < maxConnections) { |
| 71 | + // Assert socket connected |
| 72 | + expect(socket.destroyed).equals(false, `socket ${i} under limit must not be destroyed`) |
| 73 | + } else { |
| 74 | + // Assert socket ended |
| 75 | + expect(socket.destroyed).equals(true, `socket ${i} above limit must be destroyed`) |
| 76 | + } |
| 77 | + } |
| 78 | + }) |
| 79 | +}) |
0 commit comments