2017-01-03 13:16:48 -08:00
|
|
|
// Copyright Joyent, Inc. and other Node contributors.
|
|
|
|
|
//
|
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
|
// copy of this software and associated documentation files (the
|
|
|
|
|
// "Software"), to deal in the Software without restriction, including
|
|
|
|
|
// without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
|
// distribute, sublicense, and/or sell copies of the Software, and to permit
|
|
|
|
|
// persons to whom the Software is furnished to do so, subject to the
|
|
|
|
|
// following conditions:
|
|
|
|
|
//
|
|
|
|
|
// The above copyright notice and this permission notice shall be included
|
|
|
|
|
// in all copies or substantial portions of the Software.
|
|
|
|
|
//
|
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
|
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
|
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
|
|
|
|
|
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
|
|
|
|
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
|
|
|
|
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
|
|
|
|
// USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
|
|
2015-05-19 13:00:06 +02:00
|
|
|
'use strict';
|
2016-01-13 21:42:45 +01:00
|
|
|
const common = require('../common');
|
2017-07-01 02:29:09 +03:00
|
|
|
if (common.inFreeBSDJail)
|
|
|
|
|
common.skip('in a FreeBSD jail');
|
|
|
|
|
|
2016-01-13 21:42:45 +01:00
|
|
|
const assert = require('assert');
|
|
|
|
|
const dgram = require('dgram');
|
|
|
|
|
const util = require('util');
|
|
|
|
|
const networkInterfaces = require('os').networkInterfaces();
|
2019-03-21 16:19:05 -07:00
|
|
|
const { fork } = require('child_process');
|
2016-01-13 21:42:45 +01:00
|
|
|
const LOCAL_BROADCAST_HOST = '255.255.255.255';
|
|
|
|
|
const TIMEOUT = common.platformTimeout(5000);
|
|
|
|
|
const messages = [
|
2016-01-25 15:00:06 -08:00
|
|
|
Buffer.from('First message to send'),
|
|
|
|
|
Buffer.from('Second message to send'),
|
|
|
|
|
Buffer.from('Third message to send'),
|
2019-03-21 16:19:05 -07:00
|
|
|
Buffer.from('Fourth message to send'),
|
2016-01-13 21:42:45 +01:00
|
|
|
];
|
2011-10-27 10:48:38 -04:00
|
|
|
|
2016-10-26 19:59:32 -07:00
|
|
|
let bindAddress = null;
|
|
|
|
|
|
2016-05-16 13:24:08 -07:00
|
|
|
// Take the first non-internal interface as the address for binding.
|
|
|
|
|
// Ideally, this should check for whether or not an interface is set up for
|
|
|
|
|
// BROADCAST and favor internal/private interfaces.
|
2016-10-26 19:59:32 -07:00
|
|
|
get_bindAddress: for (const name in networkInterfaces) {
|
|
|
|
|
const interfaces = networkInterfaces[name];
|
|
|
|
|
for (let i = 0; i < interfaces.length; i++) {
|
|
|
|
|
const localInterface = interfaces[i];
|
2022-05-10 11:10:03 +02:00
|
|
|
if (!localInterface.internal && localInterface.family === 'IPv4') {
|
2016-10-26 19:59:32 -07:00
|
|
|
bindAddress = localInterface.address;
|
2015-01-08 11:42:34 +01:00
|
|
|
break get_bindAddress;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
assert.ok(bindAddress);
|
|
|
|
|
|
2012-01-25 12:46:45 -05:00
|
|
|
if (process.argv[2] !== 'child') {
|
2016-01-13 21:42:45 +01:00
|
|
|
const workers = {};
|
|
|
|
|
const listeners = 3;
|
|
|
|
|
let listening = 0;
|
|
|
|
|
let dead = 0;
|
|
|
|
|
let i = 0;
|
|
|
|
|
let done = 0;
|
|
|
|
|
let timer = null;
|
2012-01-25 12:46:45 -05:00
|
|
|
|
2018-03-25 22:27:38 +08:00
|
|
|
// Exit the test if it doesn't succeed within TIMEOUT
|
2019-03-21 16:19:05 -07:00
|
|
|
timer = setTimeout(() => {
|
2012-02-18 15:01:35 -08:00
|
|
|
console.error('[PARENT] Responses were not received within %d ms.',
|
|
|
|
|
TIMEOUT);
|
2012-01-30 10:04:20 -05:00
|
|
|
console.error('[PARENT] Fail');
|
|
|
|
|
|
2018-08-14 09:38:51 +03:00
|
|
|
killSubprocesses(workers);
|
2012-01-30 10:04:20 -05:00
|
|
|
|
2012-01-25 12:46:45 -05:00
|
|
|
process.exit(1);
|
|
|
|
|
}, TIMEOUT);
|
2011-10-27 10:48:38 -04:00
|
|
|
|
2018-03-25 22:27:38 +08:00
|
|
|
// Launch child processes
|
2016-10-26 19:59:32 -07:00
|
|
|
for (let x = 0; x < listeners; x++) {
|
2012-02-18 15:01:35 -08:00
|
|
|
(function() {
|
2016-10-26 19:59:32 -07:00
|
|
|
const worker = fork(process.argv[1], ['child']);
|
2011-10-27 10:48:38 -04:00
|
|
|
workers[worker.pid] = worker;
|
|
|
|
|
|
|
|
|
|
worker.messagesReceived = [];
|
|
|
|
|
|
2018-03-25 22:27:38 +08:00
|
|
|
// Handle the death of workers
|
2025-11-02 15:35:09 +01:00
|
|
|
worker.on('exit', common.mustCall((code, signal) => {
|
2019-01-21 01:22:27 +01:00
|
|
|
// Don't consider this the true death if the worker
|
2012-02-18 15:01:35 -08:00
|
|
|
// has finished successfully
|
|
|
|
|
// or if the exit code is 0
|
2016-10-26 19:59:32 -07:00
|
|
|
if (worker.isDone || code === 0) {
|
2012-01-25 12:46:45 -05:00
|
|
|
return;
|
|
|
|
|
}
|
2012-02-18 15:01:35 -08:00
|
|
|
|
2012-01-25 12:46:45 -05:00
|
|
|
dead += 1;
|
2012-02-18 15:01:35 -08:00
|
|
|
console.error('[PARENT] Worker %d died. %d dead of %d',
|
|
|
|
|
worker.pid,
|
|
|
|
|
dead,
|
|
|
|
|
listeners);
|
2012-01-25 12:46:45 -05:00
|
|
|
|
2019-03-21 16:19:05 -07:00
|
|
|
assert.notStrictEqual(signal, null);
|
|
|
|
|
|
2012-01-25 12:46:45 -05:00
|
|
|
if (dead === listeners) {
|
2012-01-30 10:04:20 -05:00
|
|
|
console.error('[PARENT] All workers have died.');
|
|
|
|
|
console.error('[PARENT] Fail');
|
|
|
|
|
|
2018-08-14 09:38:51 +03:00
|
|
|
killSubprocesses(workers);
|
2012-01-30 10:04:20 -05:00
|
|
|
|
2012-01-25 12:46:45 -05:00
|
|
|
process.exit(1);
|
|
|
|
|
}
|
2025-11-02 15:35:09 +01:00
|
|
|
}));
|
2012-01-25 12:46:45 -05:00
|
|
|
|
2025-11-02 15:35:09 +01:00
|
|
|
worker.on('message', common.mustCallAtLeast((msg) => {
|
2011-10-27 10:48:38 -04:00
|
|
|
if (msg.listening) {
|
|
|
|
|
listening += 1;
|
|
|
|
|
|
|
|
|
|
if (listening === listeners) {
|
2018-03-25 22:27:38 +08:00
|
|
|
// All child process are listening, so start sending
|
2011-10-27 10:48:38 -04:00
|
|
|
sendSocket.sendNext();
|
|
|
|
|
}
|
2016-07-08 17:17:47 -07:00
|
|
|
} else if (msg.message) {
|
2011-10-27 10:48:38 -04:00
|
|
|
worker.messagesReceived.push(msg.message);
|
|
|
|
|
|
|
|
|
|
if (worker.messagesReceived.length === messages.length) {
|
|
|
|
|
done += 1;
|
2012-01-25 12:46:45 -05:00
|
|
|
worker.isDone = true;
|
2012-02-18 15:01:35 -08:00
|
|
|
console.error('[PARENT] %d received %d messages total.',
|
|
|
|
|
worker.pid,
|
|
|
|
|
worker.messagesReceived.length);
|
2011-10-27 10:48:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (done === listeners) {
|
2012-02-18 15:01:35 -08:00
|
|
|
console.error('[PARENT] All workers have received the ' +
|
|
|
|
|
'required number of ' +
|
|
|
|
|
'messages. Will now compare.');
|
2011-10-27 10:48:38 -04:00
|
|
|
|
2025-11-02 15:35:09 +01:00
|
|
|
for (const pid of Object.keys(workers)) {
|
2016-10-26 19:59:32 -07:00
|
|
|
const worker = workers[pid];
|
2011-10-27 10:48:38 -04:00
|
|
|
|
2016-10-26 19:59:32 -07:00
|
|
|
let count = 0;
|
2011-10-27 10:48:38 -04:00
|
|
|
|
2019-03-21 16:19:05 -07:00
|
|
|
worker.messagesReceived.forEach((buf) => {
|
2016-10-26 19:59:32 -07:00
|
|
|
for (let i = 0; i < messages.length; ++i) {
|
2011-10-27 10:48:38 -04:00
|
|
|
if (buf.toString() === messages[i].toString()) {
|
|
|
|
|
count++;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2018-10-06 21:09:29 -04:00
|
|
|
console.error('[PARENT] %d received %d matching messages.',
|
2012-02-18 15:01:35 -08:00
|
|
|
worker.pid,
|
|
|
|
|
count);
|
2011-10-27 10:48:38 -04:00
|
|
|
|
2017-10-06 12:04:00 -06:00
|
|
|
assert.strictEqual(count, messages.length);
|
2025-11-02 15:35:09 +01:00
|
|
|
}
|
2012-01-25 12:46:45 -05:00
|
|
|
|
|
|
|
|
clearTimeout(timer);
|
2012-01-30 10:04:20 -05:00
|
|
|
console.error('[PARENT] Success');
|
2018-08-14 09:38:51 +03:00
|
|
|
killSubprocesses(workers);
|
2011-10-27 10:48:38 -04:00
|
|
|
}
|
|
|
|
|
}
|
2025-11-02 15:35:09 +01:00
|
|
|
}));
|
2011-10-27 10:48:38 -04:00
|
|
|
})(x);
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-26 19:59:32 -07:00
|
|
|
const sendSocket = dgram.createSocket({
|
2014-04-09 03:04:53 +04:00
|
|
|
type: 'udp4',
|
2023-02-01 10:46:06 +01:00
|
|
|
reuseAddr: true,
|
2014-04-09 03:04:53 +04:00
|
|
|
});
|
2011-10-27 10:48:38 -04:00
|
|
|
|
2019-03-07 01:03:53 +01:00
|
|
|
// Bind the address explicitly for sending
|
2012-06-18 14:16:16 +09:00
|
|
|
// INADDR_BROADCAST to only one interface
|
2015-01-08 11:42:34 +01:00
|
|
|
sendSocket.bind(common.PORT, bindAddress);
|
2019-03-21 16:19:05 -07:00
|
|
|
sendSocket.on('listening', () => {
|
2012-07-09 18:05:46 +02:00
|
|
|
sendSocket.setBroadcast(true);
|
|
|
|
|
});
|
2011-10-27 10:48:38 -04:00
|
|
|
|
2019-03-21 16:19:05 -07:00
|
|
|
sendSocket.on('close', () => {
|
2012-01-30 10:04:20 -05:00
|
|
|
console.error('[PARENT] sendSocket closed');
|
2011-10-27 10:48:38 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
sendSocket.sendNext = function() {
|
2016-10-26 19:59:32 -07:00
|
|
|
const buf = messages[i++];
|
2011-10-27 10:48:38 -04:00
|
|
|
|
|
|
|
|
if (!buf) {
|
2022-02-02 22:35:32 -08:00
|
|
|
try { sendSocket.close(); } catch {
|
|
|
|
|
// Continue regardless of error.
|
|
|
|
|
}
|
2011-10-27 10:48:38 -04:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-13 21:42:45 +01:00
|
|
|
sendSocket.send(
|
|
|
|
|
buf,
|
|
|
|
|
0,
|
|
|
|
|
buf.length,
|
|
|
|
|
common.PORT,
|
|
|
|
|
LOCAL_BROADCAST_HOST,
|
2025-11-02 15:35:09 +01:00
|
|
|
common.mustSucceed(() => {
|
2016-01-13 21:42:45 +01:00
|
|
|
console.error('[PARENT] sent %s to %s:%s',
|
|
|
|
|
util.inspect(buf.toString()),
|
|
|
|
|
LOCAL_BROADCAST_HOST, common.PORT);
|
|
|
|
|
|
|
|
|
|
process.nextTick(sendSocket.sendNext);
|
2025-11-02 15:35:09 +01:00
|
|
|
}),
|
2016-01-13 21:42:45 +01:00
|
|
|
);
|
2011-10-27 10:48:38 -04:00
|
|
|
};
|
2012-02-18 15:01:35 -08:00
|
|
|
|
2018-08-14 09:38:51 +03:00
|
|
|
function killSubprocesses(subprocesses) {
|
2019-03-21 16:19:05 -07:00
|
|
|
Object.keys(subprocesses).forEach((key) => {
|
2018-08-14 09:38:51 +03:00
|
|
|
const subprocess = subprocesses[key];
|
|
|
|
|
subprocess.kill();
|
2012-01-30 10:04:20 -05:00
|
|
|
});
|
|
|
|
|
}
|
2011-10-27 10:48:38 -04:00
|
|
|
}
|
|
|
|
|
|
2012-01-25 12:46:45 -05:00
|
|
|
if (process.argv[2] === 'child') {
|
2016-10-26 19:59:32 -07:00
|
|
|
const receivedMessages = [];
|
|
|
|
|
const listenSocket = dgram.createSocket({
|
2014-04-09 03:04:53 +04:00
|
|
|
type: 'udp4',
|
2023-02-01 10:46:06 +01:00
|
|
|
reuseAddr: true,
|
2014-04-09 03:04:53 +04:00
|
|
|
});
|
2011-10-27 10:48:38 -04:00
|
|
|
|
2019-03-21 16:19:05 -07:00
|
|
|
listenSocket.on('message', (buf, rinfo) => {
|
2019-01-21 01:22:27 +01:00
|
|
|
// Receive udp messages only sent from parent
|
2015-01-08 11:42:34 +01:00
|
|
|
if (rinfo.address !== bindAddress) return;
|
2012-06-18 14:16:16 +09:00
|
|
|
|
2012-02-18 15:01:35 -08:00
|
|
|
console.error('[CHILD] %s received %s from %j',
|
|
|
|
|
process.pid,
|
|
|
|
|
util.inspect(buf.toString()),
|
|
|
|
|
rinfo);
|
2011-10-27 10:48:38 -04:00
|
|
|
|
|
|
|
|
receivedMessages.push(buf);
|
|
|
|
|
|
2017-07-10 20:55:21 -04:00
|
|
|
process.send({ message: buf.toString() });
|
2011-10-27 10:48:38 -04:00
|
|
|
|
2016-10-26 19:59:32 -07:00
|
|
|
if (receivedMessages.length === messages.length) {
|
2019-03-21 16:19:05 -07:00
|
|
|
process.nextTick(() => { listenSocket.close(); });
|
2011-10-27 10:48:38 -04:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2019-03-21 16:19:05 -07:00
|
|
|
listenSocket.on('close', () => {
|
2018-03-25 22:27:38 +08:00
|
|
|
// 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 indicative of some other issue.
|
2019-03-21 16:19:05 -07:00
|
|
|
setTimeout(() => { process.exit(); }, 1000);
|
2011-10-27 10:48:38 -04:00
|
|
|
});
|
|
|
|
|
|
2019-03-21 16:19:05 -07:00
|
|
|
listenSocket.on('listening', () => { process.send({ listening: true }); });
|
2011-10-27 10:48:38 -04:00
|
|
|
|
|
|
|
|
listenSocket.bind(common.PORT);
|
|
|
|
|
}
|