Skip to content
This repository was archived by the owner on Apr 22, 2023. It is now read-only.

Commit b0277f3

Browse files
Julien Gillitjfontaine
authored andcommitted
tests: fix child-process-fork-dgram on SmartOS.
Send messages until both the parent and the child process have received at least one message. If at least one of them doesn't receive any message, the test runner will make the test timeout. Fixes #8046.
1 parent d6b4766 commit b0277f3

1 file changed

Lines changed: 38 additions & 24 deletions

File tree

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

Lines changed: 38 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,20 @@
1919
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
2020
// USE OR OTHER DEALINGS IN THE SOFTWARE.
2121

22+
/*
23+
* The purpose of this test is to make sure that when forking a process,
24+
* sending a fd representing a UDP socket to the child and sending messages
25+
* to this endpoint, these messages are distributed to the parent and the
26+
* child process.
27+
*
28+
* Because it's not really possible to predict how the messages will be
29+
* distributed among the parent and the child processes, we keep sending
30+
* messages until both the parent and the child received at least one
31+
* message. The worst case scenario is when either one never receives
32+
* a message. In this case the test runner will timeout after 60 secs
33+
* and the test will fail.
34+
*/
35+
2236
var dgram = require('dgram');
2337
var fork = require('child_process').fork;
2438
var assert = require('assert');
@@ -38,12 +52,11 @@ if (process.argv[2] === 'child') {
3852
server = clusterServer;
3953

4054
server.on('message', function () {
41-
childCollected += 1;
55+
process.send('gotMessage');
4256
});
4357

4458
} else if (msg === 'stop') {
4559
server.close();
46-
process.send(childCollected);
4760
process.removeListener('message', removeMe);
4861
}
4962
});
@@ -52,48 +65,49 @@ if (process.argv[2] === 'child') {
5265
var server = dgram.createSocket('udp4');
5366
var client = dgram.createSocket('udp4');
5467
var child = fork(__filename, ['child']);
68+
5569
var msg = new Buffer('Some bytes');
5670

57-
var parentCollected = 0;
58-
var childCollected = 0;
71+
var childGotMessage = false;
72+
var parentGotMessage = false;
73+
5974
server.on('message', function (msg, rinfo) {
60-
parentCollected += 1;
75+
parentGotMessage = true;
6176
});
6277

6378
server.on('listening', function () {
6479
child.send('server', server);
6580

81+
child.once('message', function (msg) {
82+
if (msg === 'gotMessage') {
83+
childGotMessage = true;
84+
}
85+
});
86+
6687
sendMessages();
6788
});
6889

6990
var sendMessages = function () {
70-
var wait = 0;
71-
var send = 0;
72-
var total = 100;
73-
7491
var timer = setInterval(function () {
75-
send += 1;
76-
if (send === total) {
77-
clearInterval(timer);
78-
}
79-
8092
client.send(msg, 0, msg.length, common.PORT, '127.0.0.1', function(err) {
8193
if (err) throw err;
82-
83-
wait += 1;
84-
if (wait === total) {
85-
shutdown();
86-
}
8794
}
8895
);
96+
97+
/*
98+
* Both the parent and the child got at least one message,
99+
* test passed, clean up everyting.
100+
*/
101+
if (parentGotMessage && childGotMessage) {
102+
clearInterval(timer);
103+
shutdown();
104+
}
105+
89106
}, 1);
90107
};
91108

92109
var shutdown = function () {
93110
child.send('stop');
94-
child.once('message', function (collected) {
95-
childCollected = collected;
96-
});
97111

98112
server.close();
99113
client.close();
@@ -102,7 +116,7 @@ if (process.argv[2] === 'child') {
102116
server.bind(common.PORT, '127.0.0.1');
103117

104118
process.once('exit', function () {
105-
assert(childCollected > 0);
106-
assert(parentCollected > 0);
119+
assert(parentGotMessage);
120+
assert(childGotMessage);
107121
});
108122
}

0 commit comments

Comments
 (0)