Skip to content

Commit 4a6bd86

Browse files
TrottMyles Borins
authored and
Myles Borins
committed
test: fix flaky test-child-process-fork-dgram
`test-child-process-fork-dgram` is unreliable on some platforms, especially FreeBSD and AIX within the project's continuous integration testing. It has also been observed to be flaky on macos. * Confirm child has received the server before sending packets * Close the server instance on the parent or child after receiving a Refs: #8697 Fixes: #8949 Fixes: #8271 PR-URL: #9098 Reviewed-By: Santiago Gimeno <[email protected]>
1 parent c8dccf2 commit 4a6bd86

File tree

2 files changed

+43
-61
lines changed

2 files changed

+43
-61
lines changed

test/parallel/parallel.status

-3
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,3 @@ test-regress-GH-1899 : FAIL, PASS
3535
# localIPv6Hosts list from test/common.js.
3636
test-https-connect-address-family : PASS,FLAKY
3737
test-tls-connect-address-family : PASS,FLAKY
38-
39-
#covered by https://github.com/nodejs/node/issues/8271
40-
test-child-process-fork-dgram : PASS, FLAKY

test/parallel/test-child-process-fork-dgram.js

+43-58
Original file line numberDiff line numberDiff line change
@@ -4,101 +4,86 @@
44
* sending a fd representing a UDP socket to the child and sending messages
55
* to this endpoint, these messages are distributed to the parent and the
66
* child process.
7-
*
8-
* Because it's not really possible to predict how the messages will be
9-
* distributed among the parent and the child processes, we keep sending
10-
* messages until both the parent and the child received at least one
11-
* message. The worst case scenario is when either one never receives
12-
* a message. In this case the test runner will timeout after 60 secs
13-
* and the test will fail.
147
*/
158

16-
var dgram = require('dgram');
17-
var fork = require('child_process').fork;
18-
var assert = require('assert');
19-
var common = require('../common');
9+
const common = require('../common');
10+
const dgram = require('dgram');
11+
const fork = require('child_process').fork;
12+
const assert = require('assert');
2013

2114
if (common.isWindows) {
22-
common.skip('Sending dgram sockets to child processes is ' +
23-
'not supported');
15+
common.skip('Sending dgram sockets to child processes is not supported');
2416
return;
2517
}
2618

27-
var server;
2819
if (process.argv[2] === 'child') {
29-
process.on('message', function removeMe(msg, clusterServer) {
30-
if (msg === 'server') {
31-
server = clusterServer;
32-
33-
server.on('message', function() {
34-
process.send('gotMessage');
35-
});
36-
37-
} else if (msg === 'stop') {
38-
server.close();
39-
process.removeListener('message', removeMe);
40-
}
20+
let childServer;
21+
22+
process.once('message', function(msg, clusterServer) {
23+
childServer = clusterServer;
24+
25+
childServer.once('message', function() {
26+
process.send('gotMessage');
27+
childServer.close();
28+
});
29+
30+
process.send('handleReceived');
4131
});
4232

4333
} else {
44-
server = dgram.createSocket('udp4');
45-
var client = dgram.createSocket('udp4');
46-
var child = fork(__filename, ['child']);
34+
const parentServer = dgram.createSocket('udp4');
35+
const client = dgram.createSocket('udp4');
36+
const child = fork(__filename, ['child']);
4737

48-
var msg = new Buffer('Some bytes');
38+
const msg = Buffer.from('Some bytes');
4939

5040
var childGotMessage = false;
5141
var parentGotMessage = false;
5242

53-
server.on('message', function(msg, rinfo) {
43+
parentServer.once('message', function(msg, rinfo) {
5444
parentGotMessage = true;
45+
parentServer.close();
5546
});
5647

57-
server.on('listening', function() {
58-
child.send('server', server);
48+
parentServer.on('listening', function() {
49+
child.send('server', parentServer);
5950

60-
child.once('message', function(msg) {
51+
child.on('message', function(msg) {
6152
if (msg === 'gotMessage') {
6253
childGotMessage = true;
54+
} else if (msg = 'handlReceived') {
55+
sendMessages();
6356
}
6457
});
65-
66-
sendMessages();
6758
});
6859

69-
var sendMessages = function() {
70-
var timer = setInterval(function() {
71-
client.send(
72-
msg,
73-
0,
74-
msg.length,
75-
server.address().port,
76-
'127.0.0.1',
77-
function(err) {
78-
if (err) throw err;
79-
}
80-
);
60+
const sendMessages = function() {
61+
const serverPort = parentServer.address().port;
8162

63+
const timer = setInterval(function() {
8264
/*
8365
* Both the parent and the child got at least one message,
8466
* test passed, clean up everyting.
8567
*/
8668
if (parentGotMessage && childGotMessage) {
8769
clearInterval(timer);
88-
shutdown();
70+
client.close();
71+
} else {
72+
client.send(
73+
msg,
74+
0,
75+
msg.length,
76+
serverPort,
77+
'127.0.0.1',
78+
function(err) {
79+
if (err) throw err;
80+
}
81+
);
8982
}
90-
9183
}, 1);
9284
};
9385

94-
var shutdown = function() {
95-
child.send('stop');
96-
97-
server.close();
98-
client.close();
99-
};
100-
101-
server.bind(0, '127.0.0.1');
86+
parentServer.bind(0, '127.0.0.1');
10287

10388
process.once('exit', function() {
10489
assert(parentGotMessage);

0 commit comments

Comments
 (0)