Skip to content

Commit

Permalink
test: refactor test-cluster-worker-isconnected.js
Browse files Browse the repository at this point in the history
PR-URL: #13685
Reviewed-By: Refael Ackermann <refack@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: David Cai <davidcai1993@yahoo.com>
Reviewed-By: Daniel Bevenius <daniel.bevenius@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
cjihrig authored and MylesBorins committed Jul 17, 2017
1 parent fa75be7 commit ef3698c
Showing 1 changed file with 11 additions and 19 deletions.
30 changes: 11 additions & 19 deletions test/parallel/test-cluster-worker-isconnected.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,30 @@
'use strict';
require('../common');
const common = require('../common');
const cluster = require('cluster');
const assert = require('assert');

if (cluster.isMaster) {
const worker = cluster.fork();

assert.ok(worker.isConnected(),
'isConnected() should return true as soon as the worker has ' +
'been created.');
assert.strictEqual(worker.isConnected(), true);

worker.on('disconnect', function() {
assert.ok(!worker.isConnected(),
'After a disconnect event has been emitted, ' +
'isConncted should return false');
});
worker.on('disconnect', common.mustCall(() => {
assert.strictEqual(worker.isConnected(), false);
}));

worker.on('message', function(msg) {
if (msg === 'readyToDisconnect') {
worker.disconnect();
}
});

} else {
assert.ok(cluster.worker.isConnected(),
'isConnected() should return true from within a worker at all ' +
'times.');
function assertNotConnected() {
assert.strictEqual(cluster.worker.isConnected(), false);
}

cluster.worker.process.on('disconnect', function() {
assert.ok(!cluster.worker.isConnected(),
'isConnected() should return false from within a worker ' +
'after its underlying process has been disconnected from ' +
'the master');
});
assert.strictEqual(cluster.worker.isConnected(), true);
cluster.worker.on('disconnect', common.mustCall(assertNotConnected));
cluster.worker.process.on('disconnect', common.mustCall(assertNotConnected));

process.send('readyToDisconnect');
}

0 comments on commit ef3698c

Please sign in to comment.