Skip to content

Commit

Permalink
test: refactor test-cluster-setup-master
Browse files Browse the repository at this point in the history
- use mustCall instead of counters
- include totalWorkers and settings in the error messages

PR-URL: nodejs#16065
Reviewed-By: Ruben Bridgewater <[email protected]>
Reviewed-By: Joyee Cheung <[email protected]>
Reviewed-By: Yuta Hiroto <[email protected]>
  • Loading branch information
Jean-Baptiste Brossard authored and joyeecheung committed Oct 15, 2017
1 parent e308761 commit e8a2438
Showing 1 changed file with 17 additions and 18 deletions.
35 changes: 17 additions & 18 deletions test/parallel/test-cluster-setup-master.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
// USE OR OTHER DEALINGS IN THE SOFTWARE.

'use strict';
require('../common');
const common = require('../common');
const assert = require('assert');
const cluster = require('cluster');

Expand All @@ -38,7 +38,7 @@ if (cluster.isWorker) {
};

const totalWorkers = 2;
let onlineWorkers = 0;
let settings;

// Setup master
cluster.setupMaster({
Expand All @@ -49,7 +49,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 &&
Expand All @@ -58,37 +58,36 @@ 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();
cluster.fork();

// 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);
});

}

0 comments on commit e8a2438

Please sign in to comment.