test: refactor test-dgram-broadcast-multi-process

* 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: https://github.com/nodejs/node/pull/26846
Reviewed-By: Richard Lau <riclau@uk.ibm.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
This commit is contained in:
Rich Trott 2019-03-21 16:19:05 -07:00
parent 683e01b250
commit 8330555295

View File

@ -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;
@ -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');
@ -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
@ -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');
@ -108,7 +110,7 @@ if (process.argv[2] !== 'child') {
}
});
worker.on('message', function(msg) {
worker.on('message', (msg) => {
if (msg.listening) {
listening += 1;
@ -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++;
@ -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');
});
@ -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()),
@ -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();
});
@ -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;
@ -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);
}