|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const { isObject } = require('lodash') |
| 4 | +const assert = require('assert').strict |
| 5 | +const { Server } = require('net') |
| 6 | +const DHT = require('bittorrent-dht') |
| 7 | + |
| 8 | +const { |
| 9 | + getFreePort, |
| 10 | + isDHTPort, |
| 11 | + getMaxPort, |
| 12 | + getDefaultPorts, |
| 13 | + getPortRangesForLookingUp |
| 14 | +} = require('../ports') |
| 15 | +const { getServerPromise } = require('../utils') |
| 16 | + |
| 17 | +const checkAssertions = (res) => { |
| 18 | + assert.ok(isObject(res)) |
| 19 | + assert.ok( |
| 20 | + [ |
| 21 | + 'grape1DhtPort', |
| 22 | + 'grape1ApiPort', |
| 23 | + 'grape2DhtPort', |
| 24 | + 'grape2ApiPort', |
| 25 | + 'workerApiPort', |
| 26 | + 'workerWsPort', |
| 27 | + 'expressApiPort' |
| 28 | + ].every((key) => Number.isFinite(res[key])) |
| 29 | + ) |
| 30 | + |
| 31 | + const maxPort = getMaxPort() |
| 32 | + const defaultPorts = getDefaultPorts() |
| 33 | + const ranges = getPortRangesForLookingUp() |
| 34 | + |
| 35 | + for (const [name, port] of Object.entries(res)) { |
| 36 | + assert.ok(port >= defaultPorts[name]) |
| 37 | + assert.ok(port <= maxPort) |
| 38 | + |
| 39 | + const errors = [] |
| 40 | + |
| 41 | + for (const { from, to } of ranges) { |
| 42 | + try { |
| 43 | + assert.ok(port >= from) |
| 44 | + assert.ok(port <= to) |
| 45 | + } catch (err) { |
| 46 | + errors.push(err) |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + if (errors.length >= ranges.length) { |
| 51 | + throw errors[0] |
| 52 | + } |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +describe('getFreePort helper', () => { |
| 57 | + it('it should lookup free ports', async function () { |
| 58 | + const res = await getFreePort() |
| 59 | + |
| 60 | + checkAssertions(res) |
| 61 | + }) |
| 62 | + |
| 63 | + it('it should lookup free ports while the previous 3 lookups have been used', async function () { |
| 64 | + const dhts = [] |
| 65 | + const srvs = [] |
| 66 | + |
| 67 | + for (let i = 0; i < 3; i += 1) { |
| 68 | + const newPorts = await getFreePort() |
| 69 | + |
| 70 | + for (const [newName, newPort] of Object.entries(newPorts)) { |
| 71 | + if (isDHTPort(newName)) { |
| 72 | + const dht = new DHT() |
| 73 | + dhts.push(dht) |
| 74 | + |
| 75 | + await getServerPromise(dht, newPort) |
| 76 | + |
| 77 | + continue |
| 78 | + } |
| 79 | + |
| 80 | + const srv = new Server() |
| 81 | + srv.unref() |
| 82 | + srvs.push(srv) |
| 83 | + |
| 84 | + await getServerPromise(srv, newPort) |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + const res = await getFreePort() |
| 89 | + |
| 90 | + checkAssertions(res) |
| 91 | + |
| 92 | + const promises = [] |
| 93 | + |
| 94 | + for (const dht of dhts) { |
| 95 | + promises.push(new Promise((resolve) => dht.destroy(resolve))) |
| 96 | + } |
| 97 | + for (const srv of srvs) { |
| 98 | + promises.push(new Promise((resolve) => srv.close(resolve))) |
| 99 | + } |
| 100 | + |
| 101 | + await Promise.all(promises) |
| 102 | + }) |
| 103 | +}) |
0 commit comments