mirror of
https://github.com/nodejs/node.git
synced 2024-12-01 16:10:02 +01:00
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
This commit is contained in:
parent
5c0f039c9c
commit
a0119af0e4
@ -21,12 +21,13 @@
|
||||
|
||||
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'),
|
||||
@ -34,21 +35,47 @@ var common = require('../common'),
|
||||
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');
|
||||
|
||||
|
@ -21,12 +21,13 @@
|
||||
|
||||
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'),
|
||||
@ -34,21 +35,47 @@ var common = require('../common'),
|
||||
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');
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user