Skip to content

Commit baa0ffd

Browse files
committed
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: #8261 Reviewed-By: Santiago Gimeno <[email protected]>
1 parent 2168432 commit baa0ffd

14 files changed

+278
-360
lines changed

test/common.js

+9
Original file line numberDiff line numberDiff line change
@@ -498,3 +498,12 @@ exports.busyLoop = function busyLoop(time) {
498498
var stopTime = startTime + time;
499499
while (Timer.now() < stopTime) {}
500500
};
501+
502+
exports.isAlive = function isAlive(pid) {
503+
try {
504+
process.kill(pid, 'SIGCONT');
505+
return true;
506+
} catch (e) {
507+
return false;
508+
}
509+
};

test/parallel/test-cluster-basic.js

+45-46
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
11
'use strict';
2-
var common = require('../common');
3-
var assert = require('assert');
4-
var cluster = require('cluster');
2+
const common = require('../common');
3+
const assert = require('assert');
4+
const cluster = require('cluster');
55

6-
assert.equal('NODE_UNIQUE_ID' in process.env, false,
7-
'NODE_UNIQUE_ID should be removed on startup');
6+
assert.strictEqual('NODE_UNIQUE_ID' in process.env, false,
7+
'NODE_UNIQUE_ID should be removed on startup');
88

99
function forEach(obj, fn) {
10-
Object.keys(obj).forEach(function(name, index) {
10+
Object.keys(obj).forEach((name, index) => {
1111
fn(obj[name], name, index);
1212
});
1313
}
1414

1515

1616
if (cluster.isWorker) {
17-
var http = require('http');
17+
const http = require('http');
1818
http.Server(function() {
1919

2020
}).listen(common.PORT, '127.0.0.1');
2121
} else if (cluster.isMaster) {
2222

23-
var checks = {
23+
const checks = {
2424
cluster: {
2525
events: {
2626
fork: false,
@@ -57,13 +57,13 @@ if (cluster.isWorker) {
5757
};
5858

5959
var worker;
60-
var stateNames = Object.keys(checks.worker.states);
60+
const stateNames = Object.keys(checks.worker.states);
6161

6262
//Check events, states, and emit arguments
63-
forEach(checks.cluster.events, function(bool, name, index) {
63+
forEach(checks.cluster.events, (bool, name, index) => {
6464

6565
//Listen on event
66-
cluster.on(name, function(/* worker */) {
66+
cluster.on(name, common.mustCall(function(/* worker */) {
6767

6868
//Set event
6969
checks.cluster.events[name] = true;
@@ -74,28 +74,26 @@ if (cluster.isWorker) {
7474
//Check state
7575
var state = stateNames[index];
7676
checks.worker.states[state] = (state === worker.state);
77-
});
77+
}));
7878
});
7979

8080
//Kill worker when listening
81-
cluster.on('listening', function() {
81+
cluster.on('listening', common.mustCall(() => {
8282
worker.kill();
83-
});
83+
}));
8484

8585
//Kill process when worker is killed
86-
cluster.on('exit', function() {
87-
process.exit(0);
88-
});
86+
cluster.on('exit', common.mustCall(() => {}));
8987

9088
//Create worker
9189
worker = cluster.fork();
92-
assert.equal(worker.id, 1);
93-
assert.ok(worker instanceof cluster.Worker,
94-
'the worker is not a instance of the Worker constructor');
90+
assert.strictEqual(worker.id, 1);
91+
assert(worker instanceof cluster.Worker,
92+
'the worker is not a instance of the Worker constructor');
9593

9694
//Check event
9795
forEach(checks.worker.events, function(bool, name, index) {
98-
worker.on(name, function() {
96+
worker.on(name, common.mustCall(function() {
9997
//Set event
10098
checks.worker.events[name] = true;
10199

@@ -104,56 +102,57 @@ if (cluster.isWorker) {
104102

105103
switch (name) {
106104
case 'exit':
107-
assert.equal(arguments[0], worker.process.exitCode);
108-
assert.equal(arguments[1], worker.process.signalCode);
109-
assert.equal(arguments.length, 2);
105+
assert.strictEqual(arguments[0], worker.process.exitCode);
106+
assert.strictEqual(arguments[1], worker.process.signalCode);
107+
assert.strictEqual(arguments.length, 2);
110108
break;
111109

112110
case 'listening':
113-
assert.equal(arguments.length, 1);
114-
var expect = { address: '127.0.0.1',
115-
port: common.PORT,
116-
addressType: 4,
117-
fd: undefined };
111+
assert.strictEqual(arguments.length, 1);
112+
const expect = { address: '127.0.0.1',
113+
port: common.PORT,
114+
addressType: 4,
115+
fd: undefined };
118116
assert.deepStrictEqual(arguments[0], expect);
119117
break;
120118

121119
default:
122-
assert.equal(arguments.length, 0);
120+
assert.strictEqual(arguments.length, 0);
123121
break;
124122
}
125-
});
123+
}));
126124
});
127125

128126
//Check all values
129-
process.once('exit', function() {
127+
process.once('exit', () => {
130128
//Check cluster events
131-
forEach(checks.cluster.events, function(check, name) {
132-
assert.ok(check, 'The cluster event "' + name + '" on the cluster ' +
133-
'object did not fire');
129+
forEach(checks.cluster.events, (check, name) => {
130+
assert(check,
131+
`The cluster event "${name}" on the cluster object did not fire`);
134132
});
135133

136134
//Check cluster event arguments
137-
forEach(checks.cluster.equal, function(check, name) {
138-
assert.ok(check, 'The cluster event "' + name + '" did not emit ' +
139-
'with correct argument');
135+
forEach(checks.cluster.equal, (check, name) => {
136+
assert(check,
137+
`The cluster event "${name}" did not emit with correct argument`);
140138
});
141139

142140
//Check worker states
143-
forEach(checks.worker.states, function(check, name) {
144-
assert.ok(check, 'The worker state "' + name + '" was not set to true');
141+
forEach(checks.worker.states, (check, name) => {
142+
assert(check,
143+
`The worker state "${name}" was not set to true`);
145144
});
146145

147146
//Check worker events
148-
forEach(checks.worker.events, function(check, name) {
149-
assert.ok(check, 'The worker event "' + name + '" on the worker object ' +
150-
'did not fire');
147+
forEach(checks.worker.events, (check, name) => {
148+
assert(check,
149+
`The worker event "${name}" on the worker object did not fire`);
151150
});
152151

153152
//Check worker event arguments
154-
forEach(checks.worker.equal, function(check, name) {
155-
assert.ok(check, 'The worker event "' + name + '" did not emit with ' +
156-
'corrent argument');
153+
forEach(checks.worker.equal, (check, name) => {
154+
assert(check,
155+
`The worker event "${name}" did not emit with correct argument`);
157156
});
158157
});
159158

Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
'use strict';
2-
var common = require('../common');
3-
var assert = require('assert');
4-
var cluster = require('cluster');
5-
var net = require('net');
2+
const common = require('../common');
3+
const assert = require('assert');
4+
const cluster = require('cluster');
5+
const net = require('net');
66

77
if (common.isWindows) {
88
common.skip('not reliable on Windows.');
@@ -15,14 +15,14 @@ if (process.getuid() === 0) {
1515
}
1616

1717
if (cluster.isMaster) {
18-
cluster.fork().on('exit', common.mustCall(function(exitCode) {
19-
assert.equal(exitCode, 0);
18+
cluster.fork().on('exit', common.mustCall((exitCode) => {
19+
assert.strictEqual(exitCode, 0);
2020
}));
2121
} else {
2222
var s = net.createServer(common.fail);
2323
s.listen(42, common.fail.bind(null, 'listen should have failed'));
24-
s.on('error', common.mustCall(function(err) {
25-
assert.equal(err.code, 'EACCES');
24+
s.on('error', common.mustCall((err) => {
25+
assert.strictEqual(err.code, 'EACCES');
2626
process.disconnect();
2727
}));
2828
}

test/parallel/test-cluster-bind-twice.js

+30-44
Original file line numberDiff line numberDiff line change
@@ -18,88 +18,74 @@
1818
//
1919
// See https://github.com/joyent/node/issues/2721 for more details.
2020

21-
var common = require('../common');
22-
var assert = require('assert');
23-
var cluster = require('cluster');
24-
var fork = require('child_process').fork;
25-
var http = require('http');
21+
const common = require('../common');
22+
const assert = require('assert');
23+
const cluster = require('cluster');
24+
const fork = require('child_process').fork;
25+
const http = require('http');
2626

27-
var id = process.argv[2];
27+
const id = process.argv[2];
2828

2929
if (!id) {
30-
var a = fork(__filename, ['one']);
31-
var b = fork(__filename, ['two']);
30+
const a = fork(__filename, ['one']);
31+
const b = fork(__filename, ['two']);
3232

33-
a.on('exit', function(c) {
33+
a.on('exit', common.mustCall((c) => {
3434
if (c) {
3535
b.send('QUIT');
3636
throw new Error('A exited with ' + c);
3737
}
38-
});
38+
}));
3939

40-
b.on('exit', function(c) {
40+
b.on('exit', common.mustCall((c) => {
4141
if (c) {
4242
a.send('QUIT');
4343
throw new Error('B exited with ' + c);
4444
}
45-
});
45+
}));
4646

4747

48-
a.on('message', function(m) {
48+
a.on('message', common.mustCall((m) => {
4949
if (typeof m === 'object') return;
50-
assert.equal(m, 'READY');
50+
assert.strictEqual(m, 'READY');
5151
b.send('START');
52-
});
52+
}));
5353

54-
let ok = false;
55-
56-
b.on('message', function(m) {
57-
if (typeof m === 'object') return; // ignore system messages
58-
assert.equal(m, 'EADDRINUSE');
59-
ok = true;
54+
b.on('message', common.mustCall((m) => {
55+
assert.strictEqual(m, 'EADDRINUSE');
6056
a.send('QUIT');
6157
b.send('QUIT');
62-
});
58+
}));
6359

64-
process.on('exit', function() {
65-
assert(ok);
66-
});
6760
} else if (id === 'one') {
6861
if (cluster.isMaster) return startWorker();
6962

70-
http.createServer(common.fail).listen(common.PORT, function() {
63+
http.createServer(common.fail).listen(common.PORT, common.mustCall(() => {
7164
process.send('READY');
72-
});
65+
}));
7366

74-
process.on('message', function(m) {
67+
process.on('message', common.mustCall((m) => {
7568
if (m === 'QUIT') process.exit();
76-
});
69+
}));
7770
} else if (id === 'two') {
7871
if (cluster.isMaster) return startWorker();
7972

80-
let ok = false;
81-
process.on('exit', function() {
82-
assert(ok);
83-
});
84-
85-
var server = http.createServer(common.fail);
86-
process.on('message', function(m) {
87-
if (typeof m === 'object') return; // ignore system messages
73+
const server = http.createServer(common.fail);
74+
process.on('message', common.mustCall((m) => {
8875
if (m === 'QUIT') process.exit();
89-
assert.equal(m, 'START');
76+
assert.strictEqual(m, 'START');
9077
server.listen(common.PORT, common.fail);
91-
server.on('error', function(e) {
92-
assert.equal(e.code, 'EADDRINUSE');
78+
server.on('error', common.mustCall((e) => {
79+
assert.strictEqual(e.code, 'EADDRINUSE');
9380
process.send(e.code);
94-
ok = true;
95-
});
96-
});
81+
}));
82+
}, 2));
9783
} else {
9884
assert(0); // bad command line argument
9985
}
10086

10187
function startWorker() {
102-
var worker = cluster.fork();
88+
const worker = cluster.fork();
10389
worker.on('exit', process.exit);
10490
worker.on('message', process.send.bind(process));
10591
process.on('message', worker.send.bind(worker));

0 commit comments

Comments
 (0)