Skip to content

Commit fe2caf6

Browse files
committed
test: fix test-inspector-port-zero-cluster
* re-implemented test to parse args instead of post binding (exit 12) * saved failing case as known issue PR-URL: nodejs#13373 Fixes: nodejs#13343 Reviewed-By: James M Snell <[email protected]>
1 parent 9f9e3c0 commit fe2caf6

File tree

3 files changed

+101
-22
lines changed

3 files changed

+101
-22
lines changed

test/inspector/inspector.status

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,5 @@ prefix inspector
55
# sample-test : PASS,FLAKY
66

77
[true] # This section applies to all platforms
8-
test-inspector-port-zero-cluster : PASS,FLAKY
98

109
[$system==win32]

test/inspector/test-inspector-port-zero-cluster.js

Lines changed: 38 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,48 @@ const common = require('../common');
44

55
common.skipIfInspectorDisabled();
66

7+
// Assert that even when started with `--inspect=0` workers are assigned
8+
// consecutive (i.e. deterministically predictable) debug ports
9+
710
const assert = require('assert');
811
const cluster = require('cluster');
912

10-
if (cluster.isMaster) {
11-
const ports = [];
12-
for (const worker of [cluster.fork(),
13-
cluster.fork(),
14-
cluster.fork()]) {
15-
worker.on('message', common.mustCall((message) => {
16-
ports.push(message.debugPort);
17-
worker.kill();
13+
function serialFork() {
14+
return new Promise((res) => {
15+
const worker = cluster.fork();
16+
worker.on('exit', common.mustCall((code, signal) => {
17+
// code 0 is normal
18+
// code 12 can happen if inspector could not bind because of a port clash
19+
if (code !== 0 && code !== 12)
20+
assert.fail(`code: ${code}, signal: ${signal}`);
21+
const port = worker.process.spawnargs
22+
.map((a) => (/=(?:.*:)?(\d{2,5})$/.exec(a) || [])[1])
23+
.filter((p) => p)
24+
.pop();
25+
res(Number(port));
1826
}));
19-
worker.send('debugPort');
20-
}
21-
process.on('exit', () => {
22-
ports.sort();
23-
assert.strictEqual(ports.length, 3);
24-
assert(ports.every((port) => port > 0));
25-
assert(ports.every((port) => port < 65536));
26-
assert.strictEqual(ports[0] + 1, ports[1]); // Ports should be consecutive.
27-
assert.strictEqual(ports[1] + 1, ports[2]);
2827
});
28+
}
29+
30+
if (cluster.isMaster) {
31+
Promise.all([serialFork(), serialFork(), serialFork()])
32+
.then(common.mustCall((ports) => {
33+
ports.push(process.debugPort);
34+
ports.sort();
35+
// 4 = [master, worker1, worker2, worker3].length()
36+
assert.strictEqual(ports.length, 4);
37+
assert(ports.every((port) => port > 0));
38+
assert(ports.every((port) => port < 65536));
39+
// Ports should be consecutive.
40+
assert.strictEqual(ports[0] + 1, ports[1]);
41+
assert.strictEqual(ports[1] + 1, ports[2]);
42+
assert.strictEqual(ports[2] + 1, ports[3]);
43+
}))
44+
.catch(
45+
(err) => {
46+
console.error(err);
47+
process.exit(1);
48+
});
2949
} else {
30-
process.on('message', (message) => {
31-
if (message === 'debugPort')
32-
process.send({ debugPort: process.debugPort });
33-
});
50+
process.exit(0);
3451
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// Flags: --inspect=0
2+
'use strict';
3+
const common = require('../common');
4+
5+
// With the current behavior of Node.js (at least as late as 8.1.0), this
6+
// test fails with the following error:
7+
// `AssertionError [ERR_ASSERTION]: worker 2 failed to bind port`
8+
// Ideally, there would be a way for the user to opt out of sequential port
9+
// assignment.
10+
//
11+
// Refs: https://github.com/nodejs/node/issues/13343
12+
13+
common.skipIfInspectorDisabled();
14+
15+
const assert = require('assert');
16+
const cluster = require('cluster');
17+
const net = require('net');
18+
19+
const ports = [process.debugPort];
20+
const clashPort = process.debugPort + 2;
21+
function serialFork() {
22+
return new Promise((res) => {
23+
const worker = cluster.fork();
24+
worker.on('error', (err) => assert.fail(err));
25+
// no common.mustCall since 1 out of 3 should fail
26+
worker.on('online', () => {
27+
worker.on('message', common.mustCall((message) => {
28+
ports.push(message.debugPort);
29+
}));
30+
});
31+
worker.on('exit', common.mustCall((code, signal) => {
32+
assert.strictEqual(signal, null);
33+
// worker 2 should fail because of port clash with `server`
34+
if (code === 12) {
35+
return assert.fail(`worker ${worker.id} failed to bind port`);
36+
}
37+
assert.strictEqual(0, code);
38+
}));
39+
worker.on('disconnect', common.mustCall(res));
40+
});
41+
}
42+
43+
if (cluster.isMaster) {
44+
cluster.on('online', common.mustCall((worker) => worker.send('dbgport'), 2));
45+
46+
// block one of the ports with a listening socket
47+
const server = net.createServer();
48+
server.listen(clashPort, common.localhostIPv4, common.mustCall(() => {
49+
// try to fork 3 workers No.2 should fail
50+
Promise.all([serialFork(), serialFork(), serialFork()])
51+
.then(common.mustNotCall())
52+
.catch((err) => console.error(err));
53+
}));
54+
server.unref();
55+
} else {
56+
const sentinel = common.mustCall();
57+
process.on('message', (message) => {
58+
if (message !== 'dbgport') return;
59+
process.send({ debugPort: process.debugPort });
60+
sentinel();
61+
process.disconnect();
62+
});
63+
}

0 commit comments

Comments
 (0)