Skip to content

Commit

Permalink
test: refactor test-dgram-broadcast-multi-process
Browse files Browse the repository at this point in the history
* Add check that `signal` is not null in callback.
* Use arrow functions for callbacks, destructuring where appropriate,
  and a trailing comma in multi-line arrays

PR-URL: nodejs#26846
Reviewed-By: Richard Lau <riclau@uk.ibm.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
  • Loading branch information
Trott committed Mar 24, 2019
1 parent 683e01b commit 8330555
Showing 1 changed file with 18 additions and 22 deletions.
40 changes: 18 additions & 22 deletions test/internet/test-dgram-broadcast-multi-process.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ const assert = require('assert');
const dgram = require('dgram');
const util = require('util');
const networkInterfaces = require('os').networkInterfaces();
const fork = require('child_process').fork;
const { fork } = require('child_process');
const LOCAL_BROADCAST_HOST = '255.255.255.255';
const TIMEOUT = common.platformTimeout(5000);
const messages = [
Buffer.from('First message to send'),
Buffer.from('Second message to send'),
Buffer.from('Third message to send'),
Buffer.from('Fourth message to send')
Buffer.from('Fourth message to send'),
];

let bindAddress = null;
Expand Down Expand Up @@ -65,7 +65,7 @@ if (process.argv[2] !== 'child') {
let timer = null;

// Exit the test if it doesn't succeed within TIMEOUT
timer = setTimeout(function() {
timer = setTimeout(() => {
console.error('[PARENT] Responses were not received within %d ms.',
TIMEOUT);
console.error('[PARENT] Fail');
Expand All @@ -84,7 +84,7 @@ if (process.argv[2] !== 'child') {
worker.messagesReceived = [];

// Handle the death of workers
worker.on('exit', function(code, signal) {
worker.on('exit', (code, signal) => {
// Don't consider this the true death if the worker
// has finished successfully
// or if the exit code is 0
Expand All @@ -98,6 +98,8 @@ if (process.argv[2] !== 'child') {
dead,
listeners);

assert.notStrictEqual(signal, null);

if (dead === listeners) {
console.error('[PARENT] All workers have died.');
console.error('[PARENT] Fail');
Expand All @@ -108,7 +110,7 @@ if (process.argv[2] !== 'child') {
}
});

worker.on('message', function(msg) {
worker.on('message', (msg) => {
if (msg.listening) {
listening += 1;

Expand All @@ -132,12 +134,12 @@ if (process.argv[2] !== 'child') {
'required number of ' +
'messages. Will now compare.');

Object.keys(workers).forEach(function(pid) {
Object.keys(workers).forEach((pid) => {
const worker = workers[pid];

let count = 0;

worker.messagesReceived.forEach(function(buf) {
worker.messagesReceived.forEach((buf) => {
for (let i = 0; i < messages.length; ++i) {
if (buf.toString() === messages[i].toString()) {
count++;
Expand Down Expand Up @@ -170,11 +172,11 @@ if (process.argv[2] !== 'child') {
// Bind the address explicitly for sending
// INADDR_BROADCAST to only one interface
sendSocket.bind(common.PORT, bindAddress);
sendSocket.on('listening', function() {
sendSocket.on('listening', () => {
sendSocket.setBroadcast(true);
});

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

Expand All @@ -192,7 +194,7 @@ if (process.argv[2] !== 'child') {
buf.length,
common.PORT,
LOCAL_BROADCAST_HOST,
function(err) {
(err) => {
assert.ifError(err);
console.error('[PARENT] sent %s to %s:%s',
util.inspect(buf.toString()),
Expand All @@ -204,7 +206,7 @@ if (process.argv[2] !== 'child') {
};

function killSubprocesses(subprocesses) {
Object.keys(subprocesses).forEach(function(key) {
Object.keys(subprocesses).forEach((key) => {
const subprocess = subprocesses[key];
subprocess.kill();
});
Expand All @@ -218,7 +220,7 @@ if (process.argv[2] === 'child') {
reuseAddr: true
});

listenSocket.on('message', function(buf, rinfo) {
listenSocket.on('message', (buf, rinfo) => {
// Receive udp messages only sent from parent
if (rinfo.address !== bindAddress) return;

Expand All @@ -232,24 +234,18 @@ if (process.argv[2] === 'child') {
process.send({ message: buf.toString() });

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

listenSocket.on('close', function() {
listenSocket.on('close', () => {
// 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.
setTimeout(function() {
process.exit();
}, 1000);
setTimeout(() => { process.exit(); }, 1000);
});

listenSocket.on('listening', function() {
process.send({ listening: true });
});
listenSocket.on('listening', () => { process.send({ listening: true }); });

listenSocket.bind(common.PORT);
}

0 comments on commit 8330555

Please sign in to comment.