Skip to content

Commit 4dd044b

Browse files
committed
test: improve test-child-process-fork-dgram
Previous implementation was subject to timing out as there was no guarantee that the parent or child would ever receive a message. Modified test so that once parent or child receives a message it closes its connection to the socket so we can guarantee the next messsage will be recieved by the other node. The test also left lingering processes when it timed out so pulled the timeout into the test itself so that it could properly cleanup in case of failure.
1 parent 7b7d50a commit 4dd044b

File tree

2 files changed

+25
-10
lines changed

2 files changed

+25
-10
lines changed

test/parallel/parallel.status

-3
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,3 @@ test-fs-watch-encoding : FAIL, PASS
3232

3333
#being worked under https://github.com/nodejs/node/issues/7973
3434
test-stdio-closed : PASS, FLAKY
35-
36-
#covered by https://github.com/nodejs/node/issues/8271
37-
test-child-process-fork-dgram : PASS, FLAKY

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

+25-7
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
* Because it's not really possible to predict how the messages will be
99
* distributed among the parent and the child processes, we keep sending
1010
* 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.
11+
* message. When either the parent or child receives a message we close
12+
* the server on that side so the next message is guaranteed to be
13+
* received by the other.
1414
*/
1515

1616
const common = require('../common');
@@ -26,22 +26,30 @@ if (common.isWindows) {
2626

2727
var server;
2828
if (process.argv[2] === 'child') {
29+
var serverClosed = false;
2930
process.on('message', function removeMe(msg, clusterServer) {
3031
if (msg === 'server') {
3132
server = clusterServer;
3233

3334
server.on('message', function() {
3435
process.send('gotMessage');
36+
// got a message so close the server to make sure
37+
// the parent also gets a message
38+
serverClosed = true;
39+
server.close();
3540
});
3641

3742
} else if (msg === 'stop') {
38-
server.close();
3943
process.removeListener('message', removeMe);
44+
if (!serverClosed) {
45+
server.close();
46+
}
4047
}
4148
});
4249

4350
} else {
4451
server = dgram.createSocket('udp4');
52+
var serverPort = null;
4553
var client = dgram.createSocket('udp4');
4654
var child = fork(__filename, ['child']);
4755

@@ -52,9 +60,13 @@ if (process.argv[2] === 'child') {
5260

5361
server.on('message', function(msg, rinfo) {
5462
parentGotMessage = true;
63+
// got a message so close the server to make sure
64+
// the child also gets a message
65+
server.close();
5566
});
5667

5768
server.on('listening', function() {
69+
serverPort = server.address().port;
5870
child.send('server', server);
5971

6072
child.once('message', function(msg) {
@@ -66,13 +78,16 @@ if (process.argv[2] === 'child') {
6678
sendMessages();
6779
});
6880

81+
var iterations = 0;
6982
var sendMessages = function() {
7083
var timer = setInterval(function() {
84+
iterations++;
85+
7186
client.send(
7287
msg,
7388
0,
7489
msg.length,
75-
server.address().port,
90+
serverPort,
7691
'127.0.0.1',
7792
function(err) {
7893
if (err) throw err;
@@ -83,7 +98,8 @@ if (process.argv[2] === 'child') {
8398
* Both the parent and the child got at least one message,
8499
* test passed, clean up everyting.
85100
*/
86-
if (parentGotMessage && childGotMessage) {
101+
if ((parentGotMessage && childGotMessage) ||
102+
((iterations / 1000) > 45)) {
87103
clearInterval(timer);
88104
shutdown();
89105
}
@@ -94,7 +110,9 @@ if (process.argv[2] === 'child') {
94110
var shutdown = function() {
95111
child.send('stop');
96112

97-
server.close();
113+
if (!parentGotMessage) {
114+
server.close();
115+
}
98116
client.close();
99117
};
100118

0 commit comments

Comments
 (0)