Skip to content

Commit 5e65181

Browse files
thefourtheyervagg
authored andcommitted
test: handling failure cases properly
Refer: #1543 When this test fails, it leaves dead processes in the system. This patch makes sure that the child processes exit first, in case of errors. PR-URL: #2206 Reviewed-By: Johan Bergström <[email protected]> Reviewed-By: Ben Noordhuis <[email protected]>
1 parent c48b95e commit 5e65181

File tree

1 file changed

+32
-21
lines changed

1 file changed

+32
-21
lines changed

test/parallel/test-cluster-eaccess.js

+32-21
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,51 @@
11
'use strict';
2-
// test that errors propagated from cluster children are properly
3-
// received in their master creates an EADDRINUSE condition by also
4-
// forking a child process to listen on a socket
5-
6-
var common = require('../common');
7-
var assert = require('assert');
8-
var cluster = require('cluster');
9-
var fork = require('child_process').fork;
10-
var fs = require('fs');
11-
var net = require('net');
2+
// Test that errors propagated from cluster workers are properly
3+
// received in their master. Creates an EADDRINUSE condition by forking
4+
// a process in child cluster and propagates the error to the master.
125

6+
const common = require('../common');
7+
const assert = require('assert');
8+
const cluster = require('cluster');
9+
const fork = require('child_process').fork;
10+
const fs = require('fs');
11+
const net = require('net');
1312

1413
if (cluster.isMaster) {
15-
var worker = cluster.fork();
16-
var gotError = 0;
17-
worker.on('message', function(err) {
18-
gotError++;
14+
const worker = cluster.fork();
15+
16+
// makes sure master is able to fork the worker
17+
cluster.on('fork', common.mustCall(function() {}));
18+
19+
// makes sure the worker is ready
20+
worker.on('online', common.mustCall(function() {}));
21+
22+
worker.on('message', common.mustCall(function(err) {
23+
// disconnect first, so that we will not leave zombies
24+
worker.disconnect();
25+
1926
console.log(err);
2027
assert.strictEqual('EADDRINUSE', err.code);
21-
worker.disconnect();
22-
});
28+
}));
29+
2330
process.on('exit', function() {
2431
console.log('master exited');
2532
try {
2633
fs.unlinkSync(common.PIPE);
2734
} catch (e) {
2835
}
29-
assert.equal(gotError, 1);
3036
});
37+
3138
} else {
3239
var cp = fork(common.fixturesDir + '/listen-on-socket-and-exit.js',
3340
{ stdio: 'inherit' });
3441

3542
// message from the child indicates it's ready and listening
36-
cp.on('message', function() {
37-
var server = net.createServer().listen(common.PIPE, function() {
38-
console.log('parent listening, should not be!');
43+
cp.on('message', common.mustCall(function() {
44+
const server = net.createServer().listen(common.PIPE, function() {
45+
// message child process so that it can exit
46+
cp.send('end');
47+
// inform master about the unexpected situation
48+
process.send('PIPE should have been in use.');
3949
});
4050

4151
server.on('error', function(err) {
@@ -45,5 +55,6 @@ if (cluster.isMaster) {
4555
// propagate error to parent
4656
process.send(err);
4757
});
48-
});
58+
59+
}));
4960
}

0 commit comments

Comments
 (0)