Browse Source

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.

https://github.com/nodejs/node/issues/2610 was opened for the bug that
was causing the problem in the first place.

PR-URL: https://github.com/nodejs/node/pull/2609
Fixes: https://github.com/nodejs/node/issues/1100
Reviewed-By: jbergstroem - Johan Bergström <bugs@bergstroem.nu>
Reviewed-By: Brendan Ashworth <brendan.ashworth@me.com>
v5.x
Rich Trott 9 years ago
committed by Rod Vagg
parent
commit
10a32aee84
  1. 1
      test/sequential/sequential.status
  2. 59
      test/sequential/test-child-process-fork-getconnections.js

1
test/sequential/sequential.status

@ -5,7 +5,6 @@ prefix sequential
# sample-test : PASS,FLAKY # sample-test : PASS,FLAKY
[true] # This section applies to all platforms [true] # This section applies to all platforms
test-child-process-fork-getconnections : PASS,FLAKY
test-repl-persistent-history : PASS,FLAKY test-repl-persistent-history : PASS,FLAKY
[$system==win32] [$system==win32]

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

@ -1,48 +1,52 @@
'use strict'; 'use strict';
var assert = require('assert'); const assert = require('assert');
var common = require('../common'); const common = require('../common');
var fork = require('child_process').fork; const fork = require('child_process').fork;
var net = require('net'); const net = require('net');
var count = 12; const count = 12;
if (process.argv[2] === 'child') { if (process.argv[2] === 'child') {
var sockets = []; let sockets = [];
var id = process.argv[3];
process.on('message', function(m, socket) { process.on('message', function(m, socket) {
function sendClosed(id) {
process.send({ id: id, status: 'closed'});
};
if (m.cmd === 'new') { if (m.cmd === 'new') {
assert(socket); assert(socket);
assert(socket instanceof net.Socket, 'should be a net.Socket'); assert(socket instanceof net.Socket, 'should be a net.Socket');
sockets.push(socket); sockets.push(socket);
socket.on('end', function() {
if (!this.closingOnPurpose)
throw new Error('[c] closing by accident!');
});
} }
if (m.cmd === 'close') { if (m.cmd === 'close') {
assert.equal(socket, undefined); assert.equal(socket, undefined);
sockets[m.id].once('close', function() { if (sockets[m.id].destroyed) {
process.send({ id: m.id, status: 'closed' }); // Workaround for https://github.com/nodejs/node/issues/2610
}); sendClosed(m.id);
sockets[m.id].destroy(); // End of workaround. When bug is fixed, this code can be used instead:
// throw new Error('socket destroyed unexpectedly!');
} else {
sockets[m.id].once('close', sendClosed.bind(null, m.id));
sockets[m.id].destroy();
}
} }
}); });
} else { } else {
var child = fork(process.argv[1], ['child']); const child = fork(process.argv[1], ['child']);
child.on('exit', function(code, signal) { child.on('exit', function(code, signal) {
if (!childKilled) if (!childKilled)
throw new Error('child died unexpectedly!'); throw new Error('child died unexpectedly!');
}); });
var server = net.createServer(); const server = net.createServer();
var sockets = []; let sockets = [];
var sent = 0; let sent = 0;
server.on('connection', function(socket) { server.on('connection', function(socket) {
child.send({ cmd: 'new' }, socket, { track: false }); child.send({ cmd: 'new' }, socket);
sockets.push(socket); sockets.push(socket);
if (sockets.length === count) { if (sockets.length === count) {
@ -50,21 +54,18 @@ if (process.argv[2] === 'child') {
} }
}); });
var disconnected = 0; let disconnected = 0;
var clients = [];
server.on('listening', function() { server.on('listening', function() {
var j = count, client; let j = count, client;
while (j--) { while (j--) {
client = net.connect(common.PORT, '127.0.0.1'); client = net.connect(common.PORT, '127.0.0.1');
client.id = j;
client.on('close', function() { client.on('close', function() {
disconnected += 1; disconnected += 1;
}); });
clients.push(client);
} }
}); });
var childKilled = false; let childKilled = false;
function closeSockets(i) { function closeSockets(i) {
if (i === count) { if (i === count) {
childKilled = true; childKilled = true;
@ -73,17 +74,17 @@ if (process.argv[2] === 'child') {
return; return;
} }
sent++;
child.send({ id: i, cmd: 'close' });
child.once('message', function(m) { child.once('message', function(m) {
assert(m.status === 'closed'); assert(m.status === 'closed');
server.getConnections(function(err, num) { server.getConnections(function(err, num) {
closeSockets(i + 1); closeSockets(i + 1);
}); });
}); });
sent++;
child.send({ id: i, cmd: 'close' });
}; };
var closeEmitted = false; let closeEmitted = false;
server.on('close', function() { server.on('close', function() {
closeEmitted = true; closeEmitted = true;
}); });

Loading…
Cancel
Save