Browse Source

test: refactor test-cluster-worker-isconnected.js

PR-URL: https://github.com/nodejs/node/pull/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>
v6
cjihrig 8 years ago
committed by James M Snell
parent
commit
06ca205719
  1. 30
      test/parallel/test-cluster-worker-isconnected.js

30
test/parallel/test-cluster-worker-isconnected.js

@ -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');
}

Loading…
Cancel
Save