Browse files

test: handle unhandled dgram scenarios

	- 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...
1 parent 5c0f039 commit a0119af0e477d12b69c730336916b49fe0a46aba @wankdanker wankdanker committed with isaacs Jan 25, 2012
View
41 test/simple/test-dgram-broadcast-multi-process.js
@@ -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;
@@ -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);
}
@@ -91,6 +119,9 @@ if (cluster.isMaster) {
assert.equal(count, messages.length
,'A worker received an invalid multicast message');
});
+
+ clearTimeout(timer);
+ console.error('Success');
}
}
});
@@ -127,7 +158,7 @@ if (cluster.isMaster) {
};
}
-if (!cluster.isMaster) {
+if (process.argv[2] === 'child') {
var receivedMessages = [];
var listenSocket = dgram.createSocket('udp4');
View
43 test/simple/test-dgram-multicast-multi-process.js
@@ -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;
@@ -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) {
@@ -91,6 +119,9 @@ if (cluster.isMaster) {
assert.equal(count, messages.length
,'A worker received an invalid multicast message');
});
+
+ clearTimeout(timer);
+ console.error('Success');
}
}
});
@@ -129,7 +160,7 @@ if (cluster.isMaster) {
};
}
-if (!cluster.isMaster) {
+if (process.argv[2] === 'child') {
var receivedMessages = [];
var listenSocket = dgram.createSocket('udp4');

0 comments on commit a0119af

Please sign in to comment.