summaryrefslogtreecommitdiff
path: root/test/parallel/test-cluster-master-error.js
diff options
context:
space:
mode:
authorJames M Snell <jasnell@gmail.com>2016-08-24 13:55:12 -0700
committerJames M Snell <jasnell@gmail.com>2016-09-01 07:13:47 -0700
commitbaa0ffdab37a490e8ca69c82772425f8bdec0ec2 (patch)
tree778fb8017556b49c5b17fa89e72da4d5de19ced2 /test/parallel/test-cluster-master-error.js
parent2168432c3616a841699814786a9bc52e7f819e6b (diff)
downloadandroid-node-v8-baa0ffdab37a490e8ca69c82772425f8bdec0ec2.tar.gz
android-node-v8-baa0ffdab37a490e8ca69c82772425f8bdec0ec2.tar.bz2
android-node-v8-baa0ffdab37a490e8ca69c82772425f8bdec0ec2.zip
test: refactor/cleanup a number of cluster tests
* Move shared code into common * Favor use of strictEqual * Add some missing common.mustCalls * Other general cleanup PR-URL: https://github.com/nodejs/node/pull/8261 Reviewed-By: Santiago Gimeno <santiago.gimeno@gmail.com>
Diffstat (limited to 'test/parallel/test-cluster-master-error.js')
-rw-r--r--test/parallel/test-cluster-master-error.js68
1 files changed, 26 insertions, 42 deletions
diff --git a/test/parallel/test-cluster-master-error.js b/test/parallel/test-cluster-master-error.js
index 1f0cb8c909..6f9c78a7ba 100644
--- a/test/parallel/test-cluster-master-error.js
+++ b/test/parallel/test-cluster-master-error.js
@@ -1,22 +1,22 @@
'use strict';
-var common = require('../common');
-var assert = require('assert');
-var cluster = require('cluster');
+const common = require('../common');
+const assert = require('assert');
+const cluster = require('cluster');
+
+const totalWorkers = 2;
// Cluster setup
if (cluster.isWorker) {
- var http = require('http');
- http.Server(function() {
+ const http = require('http');
+ http.Server(() => {
}).listen(common.PORT, '127.0.0.1');
} else if (process.argv[2] === 'cluster') {
- var totalWorkers = 2;
-
// Send PID to testcase process
var forkNum = 0;
- cluster.on('fork', function forkEvent(worker) {
+ cluster.on('fork', common.mustCall(function forkEvent(worker) {
// Send PID
process.send({
@@ -28,11 +28,11 @@ if (cluster.isWorker) {
if (++forkNum === totalWorkers) {
cluster.removeListener('fork', forkEvent);
}
- });
+ }));
// Throw accidental error when all workers are listening
var listeningNum = 0;
- cluster.on('listening', function listeningEvent() {
+ cluster.on('listening', common.mustCall(function listeningEvent() {
// When all workers are listening
if (++listeningNum === totalWorkers) {
@@ -40,13 +40,12 @@ if (cluster.isWorker) {
cluster.removeListener('listening', listeningEvent);
// Throw accidental error
- process.nextTick(function() {
- console.error('about to throw');
+ process.nextTick(() => {
throw new Error('accidental error');
});
}
- });
+ }));
// Startup a basic cluster
cluster.fork();
@@ -55,51 +54,36 @@ if (cluster.isWorker) {
} else {
// This is the testcase
- var fork = require('child_process').fork;
-
- var isAlive = function(pid) {
- try {
- //this will throw an error if the process is dead
- process.kill(pid, 0);
-
- return true;
- } catch (e) {
- return false;
- }
- };
+ const fork = require('child_process').fork;
var masterExited = false;
var workersExited = false;
// List all workers
- var workers = [];
+ const workers = [];
// Spawn a cluster process
- var master = fork(process.argv[1], ['cluster'], {silent: true});
+ const master = fork(process.argv[1], ['cluster'], {silent: true});
// Handle messages from the cluster
- master.on('message', function(data) {
+ master.on('message', common.mustCall((data) => {
// Add worker pid to list and progress tracker
if (data.cmd === 'worker') {
workers.push(data.workerPID);
}
- });
+ }, totalWorkers));
// When cluster is dead
- master.on('exit', function(code) {
+ master.on('exit', common.mustCall((code) => {
// Check that the cluster died accidentally (non-zero exit code)
masterExited = !!code;
- var pollWorkers = function() {
+ const pollWorkers = function() {
// When master is dead all workers should be dead too
var alive = false;
- workers.forEach(function(pid) {
- if (isAlive(pid)) {
- alive = true;
- }
- });
+ workers.forEach((pid) => alive = common.isAlive(pid));
if (alive) {
setTimeout(pollWorkers, 50);
} else {
@@ -109,13 +93,13 @@ if (cluster.isWorker) {
// Loop indefinitely until worker exit
pollWorkers();
- });
+ }));
- process.once('exit', function() {
- var m = 'The master did not die after an error was thrown';
- assert.ok(masterExited, m);
- m = 'The workers did not die after an error in the master';
- assert.ok(workersExited, m);
+ process.once('exit', () => {
+ assert(masterExited,
+ 'The master did not die after an error was thrown');
+ assert(workersExited,
+ 'The workers did not die after an error in the master');
});
}