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

Commit

Permalink
test: handle unhandled dgram scenarios
Browse files Browse the repository at this point in the history
	- watch for the death of child processes and fail the test if they all die
	- use setTimeout to fail the test if responses are not received and processed in 5000ms
  • Loading branch information
wankdanker authored and isaacs committed Jan 27, 2012
1 parent 5c0f039 commit a0119af
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 11 deletions.
41 changes: 36 additions & 5 deletions test/simple/test-dgram-broadcast-multi-process.js
Expand Up @@ -21,34 +21,61 @@

var common = require('../common'),
assert = require('assert'),
cluster = require('cluster'),
dgram = require('dgram'),
util = require('util'),
assert = require('assert'),
Buffer = require('buffer').Buffer,
fork = require('child_process').fork,
LOCAL_BROADCAST_HOST = '255.255.255.255',
TIMEOUT = 5000,
messages = [
new Buffer('First message to send'),
new Buffer('Second message to send'),
new Buffer('Third message to send'),
new Buffer('Fourth message to send')
];

if (cluster.isMaster) {
if (process.argv[2] !== 'child') {
var workers = {},
listeners = 3,
listening = 0,
dead = 0,
i = 0,
done = 0;
done = 0,
timer = null;

//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');
process.exit(1);
}, TIMEOUT);

//launch child processes
for (var x = 0; x < listeners; x++) {
(function () {
var worker = cluster.fork();
var worker = fork(process.argv[1], ['child']);
workers[worker.pid] = worker;

worker.messagesReceived = [];

//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) {
return;
}

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

if (dead === listeners) {
console.error('All workers have died.');
console.error('Fail');
process.exit(1);
}
});

worker.on('message', function (msg) {
if (msg.listening) {
listening += 1;
Expand All @@ -63,6 +90,7 @@ if (cluster.isMaster) {

if (worker.messagesReceived.length === messages.length) {
done += 1;
worker.isDone = true;
console.error('%d received %d messages total.', worker.pid,
worker.messagesReceived.length);
}
Expand Down Expand Up @@ -91,6 +119,9 @@ if (cluster.isMaster) {
assert.equal(count, messages.length
,'A worker received an invalid multicast message');
});

clearTimeout(timer);
console.error('Success');
}
}
});
Expand Down Expand Up @@ -127,7 +158,7 @@ if (cluster.isMaster) {
};
}

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

Expand Down
43 changes: 37 additions & 6 deletions test/simple/test-dgram-multicast-multi-process.js
Expand Up @@ -21,34 +21,61 @@

var common = require('../common'),
assert = require('assert'),
cluster = require('cluster'),
dgram = require('dgram'),
util = require('util'),
assert = require('assert'),
Buffer = require('buffer').Buffer,
fork = require('child_process').fork,
LOCAL_BROADCAST_HOST = '224.0.0.1',
TIMEOUT = 5000,
messages = [
new Buffer('First message to send'),
new Buffer('Second message to send'),
new Buffer('Third message to send'),
new Buffer('Fourth message to send')
];

if (cluster.isMaster) {
if (process.argv[2] !== 'child') {
var workers = {},
listeners = 3,
listening = 0,
dead = 0,
i = 0,
done = 0;
done = 0,
timer = null;

//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');
process.exit(1);
}, TIMEOUT);

//launch child processes
for (var x = 0; x < listeners; x++) {
(function () {
var worker = cluster.fork();
var worker = fork(process.argv[1], ['child']);
workers[worker.pid] = worker;

worker.messagesReceived = [];

//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) {
return;
}

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

if (dead === listeners) {
console.error('All workers have died.');
console.error('Fail');
process.exit(1);
}
});

worker.on('message', function (msg) {
if (msg.listening) {
listening += 1;
Expand All @@ -63,12 +90,13 @@ if (cluster.isMaster) {

if (worker.messagesReceived.length === messages.length) {
done += 1;
worker.isDone = true;
console.error('%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('All workers have received the required number of '
+ 'messages. Will now compare.');

Object.keys(workers).forEach(function (pid) {
Expand All @@ -91,6 +119,9 @@ if (cluster.isMaster) {
assert.equal(count, messages.length
,'A worker received an invalid multicast message');
});

clearTimeout(timer);
console.error('Success');
}
}
});
Expand Down Expand Up @@ -129,7 +160,7 @@ if (cluster.isMaster) {
};
}

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

Expand Down

0 comments on commit a0119af

Please sign in to comment.