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 dgram = require('dgram');
const util = require('util'); const util = require('util');
const networkInterfaces = require('os').networkInterfaces(); 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 LOCAL_BROADCAST_HOST = '255.255.255.255';
const TIMEOUT = common.platformTimeout(5000); const TIMEOUT = common.platformTimeout(5000);
const messages = [ const messages = [
Buffer.from('First message to send'), Buffer.from('First message to send'),
Buffer.from('Second message to send'), Buffer.from('Second message to send'),
Buffer.from('Third message to send'), Buffer.from('Third message to send'),
Buffer.from('Fourth message to send') Buffer.from('Fourth message to send'),
]; ];
let bindAddress = null; let bindAddress = null;
@ -65,7 +65,7 @@ if (process.argv[2] !== 'child') {
let timer = null; let timer = null;
// Exit the test if it doesn't succeed within TIMEOUT // 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.', console.error('[PARENT] Responses were not received within %d ms.',
TIMEOUT); TIMEOUT);
console.error('[PARENT] Fail'); console.error('[PARENT] Fail');
@ -84,7 +84,7 @@ if (process.argv[2] !== 'child') {
worker.messagesReceived = []; worker.messagesReceived = [];
// Handle the death of workers // 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 // Don't consider this the true death if the worker
// has finished successfully // has finished successfully
// or if the exit code is 0 // or if the exit code is 0
@ -98,6 +98,8 @@ if (process.argv[2] !== 'child') {
dead, dead,
listeners); listeners);
assert.notStrictEqual(signal, null);
if (dead === listeners) { if (dead === listeners) {
console.error('[PARENT] All workers have died.'); console.error('[PARENT] All workers have died.');
console.error('[PARENT] Fail'); 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) { if (msg.listening) {
listening += 1; listening += 1;
@ -132,12 +134,12 @@ if (process.argv[2] !== 'child') {
'required number of ' + 'required number of ' +
'messages. Will now compare.'); 'messages. Will now compare.');
Object.keys(workers).forEach(function(pid) { Object.keys(workers).forEach((pid) => {
const worker = workers[pid]; const worker = workers[pid];
let count = 0; let count = 0;
worker.messagesReceived.forEach(function(buf) { worker.messagesReceived.forEach((buf) => {
for (let i = 0; i < messages.length; ++i) { for (let i = 0; i < messages.length; ++i) {
if (buf.toString() === messages[i].toString()) { if (buf.toString() === messages[i].toString()) {
count++; count++;
@ -170,11 +172,11 @@ if (process.argv[2] !== 'child') {
// Bind the address explicitly for sending // Bind the address explicitly for sending
// INADDR_BROADCAST to only one interface // INADDR_BROADCAST to only one interface
sendSocket.bind(common.PORT, bindAddress); sendSocket.bind(common.PORT, bindAddress);
sendSocket.on('listening', function() { sendSocket.on('listening', () => {
sendSocket.setBroadcast(true); sendSocket.setBroadcast(true);
}); });
sendSocket.on('close', function() { sendSocket.on('close', () => {
console.error('[PARENT] sendSocket closed'); console.error('[PARENT] sendSocket closed');
}); });
@ -192,7 +194,7 @@ if (process.argv[2] !== 'child') {
buf.length, buf.length,
common.PORT, common.PORT,
LOCAL_BROADCAST_HOST, LOCAL_BROADCAST_HOST,
function(err) { (err) => {
assert.ifError(err); assert.ifError(err);
console.error('[PARENT] sent %s to %s:%s', console.error('[PARENT] sent %s to %s:%s',
util.inspect(buf.toString()), util.inspect(buf.toString()),
@ -204,7 +206,7 @@ if (process.argv[2] !== 'child') {
}; };
function killSubprocesses(subprocesses) { function killSubprocesses(subprocesses) {
Object.keys(subprocesses).forEach(function(key) { Object.keys(subprocesses).forEach((key) => {
const subprocess = subprocesses[key]; const subprocess = subprocesses[key];
subprocess.kill(); subprocess.kill();
}); });
@ -218,7 +220,7 @@ if (process.argv[2] === 'child') {
reuseAddr: true reuseAddr: true
}); });
listenSocket.on('message', function(buf, rinfo) { listenSocket.on('message', (buf, rinfo) => {
// Receive udp messages only sent from parent // Receive udp messages only sent from parent
if (rinfo.address !== bindAddress) return; if (rinfo.address !== bindAddress) return;
@ -232,24 +234,18 @@ if (process.argv[2] === 'child') {
process.send({ message: buf.toString() }); process.send({ message: buf.toString() });
if (receivedMessages.length === messages.length) { if (receivedMessages.length === messages.length) {
process.nextTick(function() { process.nextTick(() => { listenSocket.close(); });
listenSocket.close();
});
} }
}); });
listenSocket.on('close', function() { listenSocket.on('close', () => {
// HACK: Wait to exit the process to ensure that the parent // HACK: Wait to exit the process to ensure that the parent
// process has had time to receive all messages via process.send() // process has had time to receive all messages via process.send()
// This may be indicative of some other issue. // This may be indicative of some other issue.
setTimeout(function() { setTimeout(() => { process.exit(); }, 1000);
process.exit();
}, 1000);
}); });
listenSocket.on('listening', function() { listenSocket.on('listening', () => { process.send({ listening: true }); });
process.send({ listening: true });
});
listenSocket.bind(common.PORT); listenSocket.bind(common.PORT);
} }