-
Notifications
You must be signed in to change notification settings - Fork 30k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: refactor test-cluster-worker-isconnected.js
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
Showing
1 changed file
with
11 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} |