From eb11a704245ba99f489480d5340414b94156077e Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Brossard Date: Fri, 6 Oct 2017 12:32:25 -0700 Subject: [PATCH] test: refactor test-cluster-setup-master - use mustCall instead of counters - include totalWorkers and settings in the error messages PR-URL: https://github.com/nodejs/node/pull/16065 Reviewed-By: Ruben Bridgewater Reviewed-By: Joyee Cheung Reviewed-By: Yuta Hiroto --- test/parallel/test-cluster-setup-master.js | 35 +++++++++++----------- 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/test/parallel/test-cluster-setup-master.js b/test/parallel/test-cluster-setup-master.js index 5a92b988e61e5e..9a911b8ed20d11 100644 --- a/test/parallel/test-cluster-setup-master.js +++ b/test/parallel/test-cluster-setup-master.js @@ -1,5 +1,5 @@ 'use strict'; -require('../common'); +const common = require('../common'); const assert = require('assert'); const cluster = require('cluster'); @@ -17,7 +17,7 @@ if (cluster.isWorker) { }; const totalWorkers = 2; - let onlineWorkers = 0; + let settings; // Setup master cluster.setupMaster({ @@ -28,7 +28,7 @@ if (cluster.isWorker) { cluster.once('setup', function() { checks.setupEvent = true; - const settings = cluster.settings; + settings = cluster.settings; if (settings && settings.args && settings.args[0] === 'custom argument' && settings.silent === true && @@ -37,25 +37,19 @@ if (cluster.isWorker) { } }); - let correctIn = 0; + let correctInput = 0; - cluster.on('online', function lisenter(worker) { - - onlineWorkers++; + cluster.on('online', common.mustCall(function listener(worker) { worker.once('message', function(data) { - correctIn += (data === 'custom argument' ? 1 : 0); - if (correctIn === totalWorkers) { + correctInput += (data === 'custom argument' ? 1 : 0); + if (correctInput === totalWorkers) { checks.args = true; } worker.kill(); }); - // All workers are online - if (onlineWorkers === totalWorkers) { - checks.workers = true; - } - }); + }, totalWorkers)); // Start all workers cluster.fork(); @@ -63,11 +57,16 @@ if (cluster.isWorker) { // Check all values process.once('exit', function() { - assert.ok(checks.workers, 'Not all workers went online'); - assert.ok(checks.args, 'The arguments was noy send to the worker'); + const argsMsg = 'Arguments was not send for one or more worker. ' + + `${correctInput} workers receive argument, ` + + `but ${totalWorkers} were expected.`; + assert.ok(checks.args, argsMsg); + assert.ok(checks.setupEvent, 'The setup event was never emitted'); - const m = 'The settingsObject do not have correct properties'; - assert.ok(checks.settingsObject, m); + + const settingObjectMsg = 'The settingsObject do not have correct ' + + `properties : ${JSON.stringify(settings)}`; + assert.ok(checks.settingsObject, settingObjectMsg); }); }