Skip to content

Commit 7926886

Browse files
author
Julien Gilli
committed
test: fix test-debug-signal-cluster.js flakyness
Do not assume any order and buffering/atomicity of output from child processes' debugger agents. Fixes: #3796 PR-URL: #8568 Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Ilkka Myller <ilkka.myller@nodefield.com>
1 parent d3834a1 commit 7926886

File tree

2 files changed

+39
-32
lines changed

2 files changed

+39
-32
lines changed

test/parallel/parallel.status

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ test-tick-processor-cpp-core : PASS,FLAKY
1919
test-tick-processor-unknown : PASS,FLAKY
2020

2121
[$system==solaris] # Also applies to SmartOS
22-
test-debug-signal-cluster : PASS,FLAKY
2322

2423
[$system==freebsd]
2524

@@ -34,9 +33,5 @@ test-fs-watch-encoding : FAIL, PASS
3433
#being worked under https://github.com/nodejs/node/issues/7973
3534
test-stdio-closed : PASS, FLAKY
3635

37-
#covered by https://github.com/nodejs/node/issues/3796
38-
# but more frequent on AIX ?
39-
test-debug-signal-cluster : PASS, FLAKY
40-
4136
#covered by https://github.com/nodejs/node/issues/8271
4237
test-child-process-fork-dgram : PASS, FLAKY

test/parallel/test-debug-signal-cluster.js

Lines changed: 39 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
const common = require('../common');
44
const assert = require('assert');
55
const spawn = require('child_process').spawn;
6+
const os = require('os');
67
const path = require('path');
78

89
const port = common.PORT;
@@ -11,13 +12,24 @@ const args = [`--debug-port=${port}`, serverPath];
1112
const options = { stdio: ['inherit', 'inherit', 'pipe', 'ipc'] };
1213
const child = spawn(process.execPath, args, options);
1314

14-
const outputLines = [];
15-
var waitingForDebuggers = false;
15+
var expectedContent = [
16+
'Starting debugger agent.',
17+
'Debugger listening on 127.0.0.1:' + (port + 0),
18+
'Starting debugger agent.',
19+
'Debugger listening on 127.0.0.1:' + (port + 1),
20+
'Starting debugger agent.',
21+
'Debugger listening on 127.0.0.1:' + (port + 2),
22+
].join(os.EOL);
23+
expectedContent += os.EOL; // the last line also contains an EOL character
24+
25+
var debuggerAgentsOutput = '';
26+
var debuggerAgentsStarted = false;
1627

1728
var pids;
1829

1930
child.stderr.on('data', function(data) {
20-
const lines = data.toString().replace(/\r/g, '').trim().split('\n');
31+
const childStderrOutputString = data.toString();
32+
const lines = childStderrOutputString.replace(/\r/g, '').trim().split('\n');
2133

2234
lines.forEach(function(line) {
2335
console.log('> ' + line);
@@ -30,24 +42,26 @@ child.stderr.on('data', function(data) {
3042
pids = msg.pids;
3143
console.error('got pids %j', pids);
3244

33-
waitingForDebuggers = true;
3445
process._debugProcess(child.pid);
46+
debuggerAgentsStarted = true;
3547
});
3648

3749
child.send({
3850
type: 'getpids'
3951
});
40-
} else if (waitingForDebuggers) {
41-
outputLines.push(line);
4252
}
43-
4453
});
45-
if (outputLines.length === expectedLines.length)
46-
onNoMoreLines();
54+
55+
if (debuggerAgentsStarted) {
56+
debuggerAgentsOutput += childStderrOutputString;
57+
if (debuggerAgentsOutput.length === expectedContent.length) {
58+
onNoMoreDebuggerAgentsOutput();
59+
}
60+
}
4761
});
4862

49-
function onNoMoreLines() {
50-
assertOutputLines();
63+
function onNoMoreDebuggerAgentsOutput() {
64+
assertDebuggerAgentsOutput();
5165
process.exit();
5266
}
5367

@@ -63,21 +77,19 @@ process.on('exit', function onExit() {
6377
});
6478
});
6579

66-
const expectedLines = [
67-
'Starting debugger agent.',
68-
'Debugger listening on 127.0.0.1:' + (port + 0),
69-
'Starting debugger agent.',
70-
'Debugger listening on 127.0.0.1:' + (port + 1),
71-
'Starting debugger agent.',
72-
'Debugger listening on 127.0.0.1:' + (port + 2),
73-
];
74-
75-
function assertOutputLines() {
76-
// Do not assume any particular order of output messages,
77-
// since workers can take different amout of time to
78-
// start up
79-
outputLines.sort();
80-
expectedLines.sort();
80+
function assertDebuggerAgentsOutput() {
81+
// Workers can take different amout of time to start up, and child processes'
82+
// output may be interleaved arbitrarily. Moreover, child processes' output
83+
// may be written using an arbitrary number of system calls, and no assumption
84+
// on buffering or atomicity of output should be made. Thus, we process the
85+
// output of all child processes' debugger agents character by character, and
86+
// remove each character from the set of expected characters. Once all the
87+
// output from all debugger agents has been processed, we consider that we got
88+
// the content we expected if there's no character left in the initial
89+
// expected content.
90+
debuggerAgentsOutput.split('').forEach(function gotChar(char) {
91+
expectedContent = expectedContent.replace(char, '');
92+
});
8193

82-
assert.deepStrictEqual(outputLines, expectedLines);
94+
assert.strictEqual(expectedContent, '');
8395
}

0 commit comments

Comments
 (0)