Skip to content

Commit 7df499d

Browse files
committed
Updated tests
1 parent 6158abd commit 7df499d

File tree

2 files changed

+97
-21
lines changed

2 files changed

+97
-21
lines changed

src/__tests__/Net.test.ts

Lines changed: 88 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
import { isPortInUse, getNetworkInterfaces, stopNetworkUtilizationComputation } from '../net';
1+
import { start } from 'repl';
2+
import { isPortInUse, getNetworkInterfaces, stopNetworkUtilizationComputation, canConnect, isPortListendedOn } from '../net';
23
import net from 'net';
34

45

6+
57
test('getNetworkInterfaces', async () => {
68
const nics = await getNetworkInterfaces();
79
//console.log(nics); // TODO REMOVE
@@ -29,22 +31,94 @@ test('isPortInUse(12345)', async () => {
2931
server.close();
3032
});
3133

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+
32113
/*
33114
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', '::' ];
46117
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+
}));
48122
});
49123
*/
50124

src/net.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -224,13 +224,15 @@ export async function isPortListendedOn(port: number, bindAddress: string = '0.0
224224
* @param bindAddress Address of the interface to bind to (default '0.0.0.0').
225225
*/
226226
export async function isPortInUse(port: number, bindAddress: string = '0.0.0.0'): Promise<boolean> {
227-
let canConn = false;
228-
let isListened = false;
229-
await Promise.allSettled([
230-
canConnect(port, bindAddress).then(res => canConn = res),
231-
isPortListendedOn(port, bindAddress).then(res => isListened = res)
232-
])
233-
return canConn || isListened;
227+
// check first if port can be listened on (way faster if port is really used because just a kernel check needed).
228+
const isListenedOn = await isPortListendedOn(port, bindAddress);
229+
if(isListenedOn) return true;
230+
231+
// as backup check if can connect
232+
const canConn = await canConnect(port, bindAddress);
233+
if(canConn) return true;
234+
235+
return false;
234236
}
235237

236238

0 commit comments

Comments
 (0)