|
1 | | -import { isPortInUse, getNetworkInterfaces, stopNetworkUtilizationComputation } from '../net'; |
| 1 | +import { start } from 'repl'; |
| 2 | +import { isPortInUse, getNetworkInterfaces, stopNetworkUtilizationComputation, canConnect, isPortListendedOn } from '../net'; |
2 | 3 | import net from 'net'; |
3 | 4 |
|
4 | 5 |
|
| 6 | + |
5 | 7 | test('getNetworkInterfaces', async () => { |
6 | 8 | const nics = await getNetworkInterfaces(); |
7 | 9 | //console.log(nics); // TODO REMOVE |
@@ -29,22 +31,94 @@ test('isPortInUse(12345)', async () => { |
29 | 31 | server.close(); |
30 | 32 | }); |
31 | 33 |
|
| 34 | + |
| 35 | + |
| 36 | +test('isPortListenedOn not affecting canConnect', async () => { |
| 37 | + for(let i=0; i < 10; i++){ |
| 38 | + |
| 39 | + const canConn = await canConnect(12345); |
| 40 | + expect(canConn).toBe(false); |
| 41 | + |
| 42 | + const isListened = await isPortListendedOn(12345); |
| 43 | + expect(isListened).toBe(false); |
| 44 | + } |
| 45 | +}); |
| 46 | + |
| 47 | + |
| 48 | +/* |
| 49 | +test('canConnect vs isPortListenedOn', async () => { |
| 50 | + const iterations = 50; |
| 51 | + const freePort = 12345; |
| 52 | + const usedPort = 12346; |
| 53 | +
|
| 54 | +
|
| 55 | + // start server |
| 56 | + const server = net.createServer(); |
| 57 | + server.unref(); |
| 58 | + await new Promise<void>((resolve) => server.listen(usedPort, '0.0.0.0', resolve)); |
| 59 | +
|
| 60 | +
|
| 61 | + console.log('-----'); |
| 62 | +
|
| 63 | +
|
| 64 | + // measure canConnect on free port |
| 65 | + let start = Date.now(); |
| 66 | + for(let i=0; i < iterations; i++){ |
| 67 | + await canConnect(freePort); |
| 68 | + } |
| 69 | + const durationConnectFree = Date.now() - start; |
| 70 | + console.log(`canConnect -> false: took ${durationConnectFree}ms`); |
| 71 | + |
| 72 | + // measure isPortListendedOn on free port |
| 73 | + start = Date.now(); |
| 74 | + for(let i=0; i < iterations; i++){ |
| 75 | + await isPortListendedOn(freePort); |
| 76 | + } |
| 77 | + const durationListenFree = Date.now() - start; |
| 78 | + console.log(`isPortListendedOn -> false: took ${durationListenFree}ms`); |
| 79 | +
|
| 80 | +
|
| 81 | + console.log('-----'); |
| 82 | +
|
| 83 | +
|
| 84 | + // measure canConnect on used port |
| 85 | + start = Date.now(); |
| 86 | + for(let i=0; i < iterations; i++){ |
| 87 | + await canConnect(usedPort); |
| 88 | + } |
| 89 | + const durationConnectUsed = Date.now() - start; |
| 90 | + console.log(`canConnect -> true: took ${durationConnectUsed}ms`); |
| 91 | +
|
| 92 | + // measure isPortListendedOn on used port |
| 93 | + start = Date.now(); |
| 94 | + for(let i=0; i < iterations; i++){ |
| 95 | + await isPortListendedOn(usedPort); |
| 96 | + } |
| 97 | + const durationListenUsed = Date.now() - start; |
| 98 | + console.log(`isPortListendedOn -> true: took ${durationListenUsed}ms`); |
| 99 | +
|
| 100 | +
|
| 101 | + console.log('-----'); |
| 102 | +
|
| 103 | + const durationConnAvg = (durationConnectFree + durationConnectUsed) / 2; |
| 104 | + const durationListenAvg = (durationListenFree + durationListenUsed) / 2; |
| 105 | +
|
| 106 | + console.log(`canConnect avg: ${durationConnAvg}ms`); |
| 107 | + console.log(`isPortListendedOn avg: ${durationListenAvg}ms`); |
| 108 | +
|
| 109 | + server.close(); |
| 110 | +}); |
| 111 | +*/ |
| 112 | + |
32 | 113 | /* |
33 | 114 | test('DEBUG', async () => { |
34 | | - const ports = { |
35 | | - 5050: false, |
36 | | - 5432: false, |
37 | | - 6379: false, |
38 | | - 27017: false, |
39 | | - }; |
40 | | -
|
41 | | - await Promise.allSettled(Object.keys(ports).map(async p => { |
42 | | - const inUse1 = await isPortInUse(parseInt(p), '0.0.0.0'); |
43 | | - const inUse2 = await isPortInUse(parseInt(p), '127.0.0.1'); |
44 | | - ports[p] = inUse1 || inUse2; |
45 | | - })); |
| 115 | + const ports = [ 5050, 5432, 6379, 27017 ]; |
| 116 | + const addresses = [ '0.0.0.0', '127.0.0.1', '::' ]; |
46 | 117 |
|
47 | | - console.log(JSON.stringify(ports, null, ' ')); |
| 118 | + await Promise.allSettled(ports.flatMap(p => addresses.map<[number, string]>(a => [p, a])).map(async ([port, addr]) => { |
| 119 | + const inUse = await isPortInUse(port, addr); |
| 120 | + console.log(port, addr, inUse); // TODO REMOVE |
| 121 | + })); |
48 | 122 | }); |
49 | 123 | */ |
50 | 124 |
|
|
0 commit comments