Skip to content

Commit b2c5479

Browse files
Trottrvagg
authored andcommitted
test: refactor to eliminate flaky test
This retains the key elements of test-child-process-fork-getconnections (forks a child process, sends a bunch of sockets, uses getConnections() to enumerate them) but contains some code to work around an apparent intermittent bug that occurs on OS X where a socket seems to close itself unexpectedly. #2610 was opened for the bug that was causing the problem in the first place. PR-URL: #2609 Fixes: #1100 Reviewed-By: jbergstroem - Johan Bergström <bugs@bergstroem.nu> Reviewed-By: Brendan Ashworth <brendan.ashworth@me.com>
1 parent 8929445 commit b2c5479

File tree

2 files changed

+30
-30
lines changed

2 files changed

+30
-30
lines changed

test/sequential/sequential.status

-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ prefix sequential
55
# sample-test : PASS,FLAKY
66

77
[true] # This section applies to all platforms
8-
test-child-process-fork-getconnections : PASS,FLAKY
98
test-repl-persistent-history : PASS,FLAKY
109

1110
[$system==win32]

test/sequential/test-child-process-fork-getconnections.js

+30-29
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,71 @@
11
'use strict';
2-
var assert = require('assert');
3-
var common = require('../common');
4-
var fork = require('child_process').fork;
5-
var net = require('net');
6-
var count = 12;
2+
const assert = require('assert');
3+
const common = require('../common');
4+
const fork = require('child_process').fork;
5+
const net = require('net');
6+
const count = 12;
77

88
if (process.argv[2] === 'child') {
9-
var sockets = [];
10-
var id = process.argv[3];
9+
let sockets = [];
1110

1211
process.on('message', function(m, socket) {
12+
function sendClosed(id) {
13+
process.send({ id: id, status: 'closed'});
14+
};
15+
1316
if (m.cmd === 'new') {
1417
assert(socket);
1518
assert(socket instanceof net.Socket, 'should be a net.Socket');
1619
sockets.push(socket);
17-
socket.on('end', function() {
18-
if (!this.closingOnPurpose)
19-
throw new Error('[c] closing by accident!');
20-
});
2120
}
2221

2322
if (m.cmd === 'close') {
2423
assert.equal(socket, undefined);
25-
sockets[m.id].once('close', function() {
26-
process.send({ id: m.id, status: 'closed' });
27-
});
28-
sockets[m.id].destroy();
24+
if (sockets[m.id].destroyed) {
25+
// Workaround for https://github.com/nodejs/node/issues/2610
26+
sendClosed(m.id);
27+
// End of workaround. When bug is fixed, this code can be used instead:
28+
// throw new Error('socket destroyed unexpectedly!');
29+
} else {
30+
sockets[m.id].once('close', sendClosed.bind(null, m.id));
31+
sockets[m.id].destroy();
32+
}
2933
}
3034
});
3135

3236
} else {
33-
var child = fork(process.argv[1], ['child']);
37+
const child = fork(process.argv[1], ['child']);
3438

3539
child.on('exit', function(code, signal) {
3640
if (!childKilled)
3741
throw new Error('child died unexpectedly!');
3842
});
3943

40-
var server = net.createServer();
41-
var sockets = [];
42-
var sent = 0;
44+
const server = net.createServer();
45+
let sockets = [];
46+
let sent = 0;
4347

4448
server.on('connection', function(socket) {
45-
child.send({ cmd: 'new' }, socket, { track: false });
49+
child.send({ cmd: 'new' }, socket);
4650
sockets.push(socket);
4751

4852
if (sockets.length === count) {
4953
closeSockets(0);
5054
}
5155
});
5256

53-
var disconnected = 0;
54-
var clients = [];
57+
let disconnected = 0;
5558
server.on('listening', function() {
56-
var j = count, client;
59+
let j = count, client;
5760
while (j--) {
5861
client = net.connect(common.PORT, '127.0.0.1');
59-
client.id = j;
6062
client.on('close', function() {
6163
disconnected += 1;
6264
});
63-
clients.push(client);
6465
}
6566
});
6667

67-
var childKilled = false;
68+
let childKilled = false;
6869
function closeSockets(i) {
6970
if (i === count) {
7071
childKilled = true;
@@ -73,17 +74,17 @@ if (process.argv[2] === 'child') {
7374
return;
7475
}
7576

76-
sent++;
77-
child.send({ id: i, cmd: 'close' });
7877
child.once('message', function(m) {
7978
assert(m.status === 'closed');
8079
server.getConnections(function(err, num) {
8180
closeSockets(i + 1);
8281
});
8382
});
83+
sent++;
84+
child.send({ id: i, cmd: 'close' });
8485
};
8586

86-
var closeEmitted = false;
87+
let closeEmitted = false;
8788
server.on('close', function() {
8889
closeEmitted = true;
8990
});

0 commit comments

Comments
 (0)