Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: refactor to make flaky test robust #2609

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion test/sequential/sequential.status
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ prefix sequential
# sample-test : PASS,FLAKY

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

[$system==win32]
Expand Down
59 changes: 30 additions & 29 deletions test/sequential/test-child-process-fork-getconnections.js
Original file line number Diff line number Diff line change
@@ -1,70 +1,71 @@
'use strict';
var assert = require('assert');
var common = require('../common');
var fork = require('child_process').fork;
var net = require('net');
var count = 12;
const assert = require('assert');
const common = require('../common');
const fork = require('child_process').fork;
const net = require('net');
const count = 12;

if (process.argv[2] === 'child') {
var sockets = [];
var id = process.argv[3];
let sockets = [];

process.on('message', function(m, socket) {
function sendClosed(id) {
process.send({ id: id, status: 'closed'});
};

if (m.cmd === 'new') {
assert(socket);
assert(socket instanceof net.Socket, 'should be a net.Socket');
sockets.push(socket);
socket.on('end', function() {
if (!this.closingOnPurpose)
throw new Error('[c] closing by accident!');
});
}

if (m.cmd === 'close') {
assert.equal(socket, undefined);
sockets[m.id].once('close', function() {
process.send({ id: m.id, status: 'closed' });
});
sockets[m.id].destroy();
if (sockets[m.id].destroyed) {
// Workaround for https://github.com/nodejs/node/issues/2610
sendClosed(m.id);
// 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 {
var child = fork(process.argv[1], ['child']);
const child = fork(process.argv[1], ['child']);

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

var server = net.createServer();
var sockets = [];
var sent = 0;
const server = net.createServer();
let sockets = [];
let sent = 0;

server.on('connection', function(socket) {
child.send({ cmd: 'new' }, socket, { track: false });
child.send({ cmd: 'new' }, socket);
sockets.push(socket);

if (sockets.length === count) {
closeSockets(0);
}
});

var disconnected = 0;
var clients = [];
let disconnected = 0;
server.on('listening', function() {
var j = count, client;
let j = count, client;
while (j--) {
client = net.connect(common.PORT, '127.0.0.1');
client.id = j;
client.on('close', function() {
disconnected += 1;
});
clients.push(client);
}
});

var childKilled = false;
let childKilled = false;
function closeSockets(i) {
if (i === count) {
childKilled = true;
Expand All @@ -73,17 +74,17 @@ if (process.argv[2] === 'child') {
return;
}

sent++;
child.send({ id: i, cmd: 'close' });
child.once('message', function(m) {
assert(m.status === 'closed');
server.getConnections(function(err, num) {
closeSockets(i + 1);
});
});
sent++;
child.send({ id: i, cmd: 'close' });
};

var closeEmitted = false;
let closeEmitted = false;
server.on('close', function() {
closeEmitted = true;
});
Expand Down