Skip to content

Commit

Permalink
test: dgram-{broadcast,multicast}-multi-process : prevent false failures
Browse files Browse the repository at this point in the history
* check exit code of child processes
* wait 1000 ms to exit the child process
* prefix log messages with [PARENT] or [CHILD] to help debugging
* kill all child processes before exiting

Conflicts:

	test/simple/test-dgram-multicast-multi-process.js
  • Loading branch information
wankdanker authored and isaacs committed Feb 1, 2012
1 parent fa490f6 commit 35b3d15
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 29 deletions.
50 changes: 35 additions & 15 deletions test/simple/test-dgram-broadcast-multi-process.js
Expand Up @@ -46,8 +46,11 @@ if (process.argv[2] !== 'child') {

//exit the test if it doesn't succeed within TIMEOUT
timer = setTimeout(function () {
console.error('Responses were not received within %d ms.', TIMEOUT);
console.error('Fail');
console.error('[PARENT] Responses were not received within %d ms.', TIMEOUT);
console.error('[PARENT] Fail');

killChildren(workers);

process.exit(1);
}, TIMEOUT);

Expand All @@ -62,16 +65,20 @@ if (process.argv[2] !== 'child') {
//handle the death of workers
worker.on('exit', function (code, signal) {
//don't consider this the true death if the worker has finished successfully
if (worker.isDone) {
//or if the exit code is 0
if (worker.isDone || code == 0) {
return;
}

dead += 1;
console.error('Worker %d died. %d dead of %d', worker.pid, dead, listeners);
console.error('[PARENT] Worker %d died. %d dead of %d', worker.pid, dead, listeners);

if (dead === listeners) {
console.error('All workers have died.');
console.error('Fail');
console.error('[PARENT] All workers have died.');
console.error('[PARENT] Fail');

killChildren(workers);

process.exit(1);
}
});
Expand All @@ -91,12 +98,12 @@ if (process.argv[2] !== 'child') {
if (worker.messagesReceived.length === messages.length) {
done += 1;
worker.isDone = true;
console.error('%d received %d messages total.', worker.pid,
console.error('[PARENT] %d received %d messages total.', worker.pid,
worker.messagesReceived.length);
}

if (done === listeners) {
console.error('All workers have received the required number of '
console.error('[PARENT] All workers have received the required number of '
+ 'messages. Will now compare.');

Object.keys(workers).forEach(function (pid) {
Expand All @@ -113,15 +120,16 @@ if (process.argv[2] !== 'child') {
}
});

console.error('%d received %d matching messges.', worker.pid
console.error('[PARENT] %d received %d matching messges.', worker.pid
, count);

assert.equal(count, messages.length
,'A worker received an invalid multicast message');
});

clearTimeout(timer);
console.error('Success');
console.error('[PARENT] Success');
killChildren(workers);
}
}
});
Expand All @@ -134,7 +142,7 @@ if (process.argv[2] !== 'child') {
sendSocket.setBroadcast(true);

sendSocket.on('close', function() {
console.error('sendSocket closed');
console.error('[PARENT] sendSocket closed');
});

sendSocket.sendNext = function() {
Expand All @@ -150,35 +158,47 @@ if (process.argv[2] !== 'child') {

if (err) throw err;

console.error('sent %s to %s:%s', util.inspect(buf.toString())
console.error('[PARENT] sent %s to %s:%s', util.inspect(buf.toString())
, LOCAL_BROADCAST_HOST, common.PORT);

process.nextTick(sendSocket.sendNext);
});
};

function killChildren(children) {
Object.keys(children).forEach(function(key) {
var child = children[key];
child.kill();
});
}
}

if (process.argv[2] === 'child') {
var receivedMessages = [];
var listenSocket = dgram.createSocket('udp4');

listenSocket.on('message', function(buf, rinfo) {
console.error('%s received %s from %j', process.pid
console.error('[CHILD] %s received %s from %j', process.pid
, util.inspect(buf.toString()), rinfo);

receivedMessages.push(buf);

process.send({ message : buf.toString() });

if (receivedMessages.length == messages.length) {
process.nextTick(function() {
process.nextTick(function () {
listenSocket.close();
});
}
});

listenSocket.on('close', function() {
process.exit();
//HACK: Wait to exit the process to ensure that the parent
//process has had time to receive all messages via process.send()
//This may be indicitave of some other issue.
setTimeout(function() {
process.exit();
}, 1000);
});

listenSocket.on('listening', function() {
Expand Down
50 changes: 36 additions & 14 deletions test/simple/test-dgram-multicast-multi-process.js
Expand Up @@ -46,8 +46,11 @@ if (process.argv[2] !== 'child') {

//exit the test if it doesn't succeed within TIMEOUT
timer = setTimeout(function () {
console.error('Responses were not received within %d ms.', TIMEOUT);
console.error('Fail');
console.error('[PARENT] Responses were not received within %d ms.', TIMEOUT);
console.error('[PARENT] Fail');

killChildren(workers);

process.exit(1);
}, TIMEOUT);

Expand All @@ -62,16 +65,21 @@ if (process.argv[2] !== 'child') {
//handle the death of workers
worker.on('exit', function (code, signal) {
//don't consider this the true death if the worker has finished successfully
if (worker.isDone) {

//or if the exit code is 0
if (worker.isDone || code === 0) {
return;
}

dead += 1;
console.error('Worker %d died. %d dead of %d', worker.pid, dead, listeners);
console.error('[PARENT] Worker %d died. %d dead of %d', worker.pid, dead, listeners);

if (dead === listeners) {
console.error('All workers have died.');
console.error('Fail');
console.error('[PARENT] All workers have died.');
console.error('[PARENT] Fail');

killChildren(workers);

process.exit(1);
}
});
Expand All @@ -91,12 +99,12 @@ if (process.argv[2] !== 'child') {
if (worker.messagesReceived.length === messages.length) {
done += 1;
worker.isDone = true;
console.error('%d received %d messages total.', worker.pid,
console.error('[PARENT] %d received %d messages total.', worker.pid,
worker.messagesReceived.length);
}

if (done === listeners) {
console.error('All workers have received the required number of '
console.error('[PARENT] All workers have received the required number of '
+ 'messages. Will now compare.');

Object.keys(workers).forEach(function (pid) {
Expand All @@ -113,15 +121,16 @@ if (process.argv[2] !== 'child') {
}
});

console.error('%d received %d matching messages.', worker.pid
console.error('[PARENT] %d received %d matching messages.', worker.pid
, count);

assert.equal(count, messages.length
,'A worker received an invalid multicast message');
});

clearTimeout(timer);
console.error('Success');
console.error('[PARENT] Success');
killChildren(workers);
}
}
});
Expand All @@ -139,7 +148,7 @@ if (process.argv[2] !== 'child') {
sendSocket.setMulticastLoopback(true);

sendSocket.on('close', function() {
console.error('sendSocket closed');
console.error('[PARENT] sendSocket closed');
});

sendSocket.sendNext = function() {
Expand All @@ -153,19 +162,26 @@ if (process.argv[2] !== 'child') {
sendSocket.send(buf, 0, buf.length,
common.PORT, LOCAL_BROADCAST_HOST, function(err) {
if (err) throw err;
console.error('sent %s to %s:%s', util.inspect(buf.toString()),
console.error('[PARENT] sent %s to %s:%s', util.inspect(buf.toString()),
LOCAL_BROADCAST_HOST, common.PORT);
process.nextTick(sendSocket.sendNext);
});
};

function killChildren(children) {
Object.keys(children).forEach(function(key) {
var child = children[key];
child.kill();
});
}
}

if (process.argv[2] === 'child') {
var receivedMessages = [];
var listenSocket = dgram.createSocket('udp4');

listenSocket.on('message', function(buf, rinfo) {
console.error('%s received %s from %j', process.pid
console.error('[CHILD] %s received %s from %j', process.pid
,util.inspect(buf.toString()), rinfo);

receivedMessages.push(buf);
Expand All @@ -174,6 +190,7 @@ if (process.argv[2] === 'child') {

if (receivedMessages.length == messages.length) {
listenSocket.dropMembership(LOCAL_BROADCAST_HOST);

process.nextTick(function() { // TODO should be changed to below.
// listenSocket.dropMembership(LOCAL_BROADCAST_HOST, function() {
listenSocket.close();
Expand All @@ -182,7 +199,12 @@ if (process.argv[2] === 'child') {
});

listenSocket.on('close', function() {
process.exit();
//HACK: Wait to exit the process to ensure that the parent
//process has had time to receive all messages via process.send()
//This may be indicitave of some other issue.
setTimeout(function () {
process.exit();
}, 1000);
});

listenSocket.on('listening', function() {
Expand Down

0 comments on commit 35b3d15

Please sign in to comment.