diff --git a/doc/api/cluster.md b/doc/api/cluster.md index c323282d0f94b1..f3c8017da1eafd 100644 --- a/doc/api/cluster.md +++ b/doc/api/cluster.md @@ -257,6 +257,8 @@ It is not emitted in the worker. added: v0.7.7 --> +* Returns: {Worker} A reference to `worker`. + In a worker, this function will close all servers, wait for the `'close'` event on those servers, and then disconnect the IPC channel. diff --git a/lib/cluster.js b/lib/cluster.js index 67e8833941789a..e95e19bcc672d4 100644 --- a/lib/cluster.js +++ b/lib/cluster.js @@ -430,6 +430,7 @@ function masterInit() { send(this, { act: 'disconnect' }); removeHandlesForWorker(this); removeWorker(this); + return this; }; Worker.prototype.destroy = function(signo) { @@ -687,6 +688,7 @@ function workerInit() { Worker.prototype.disconnect = function() { _disconnect.call(this); + return this; }; Worker.prototype.destroy = function() { diff --git a/test/parallel/test-cluster-worker-destroy.js b/test/parallel/test-cluster-worker-destroy.js index c802177530e4af..ca3ac77e7b25cc 100644 --- a/test/parallel/test-cluster-worker-destroy.js +++ b/test/parallel/test-cluster-worker-destroy.js @@ -8,6 +8,7 @@ */ const common = require('../common'); +const assert = require('assert'); var cluster = require('cluster'); var worker1, worker2; @@ -26,7 +27,8 @@ if (cluster.isMaster) { cluster.worker.destroy(); }); - cluster.worker.disconnect(); + const w = cluster.worker.disconnect(); + assert.strictEqual(w, cluster.worker, 'did not return a reference'); } else { // Call destroy when worker is not disconnected yet cluster.worker.destroy(); diff --git a/test/parallel/test-cluster-worker-disconnect.js b/test/parallel/test-cluster-worker-disconnect.js index 4e17a7d475e3d0..684fd5541eac55 100644 --- a/test/parallel/test-cluster-worker-disconnect.js +++ b/test/parallel/test-cluster-worker-disconnect.js @@ -40,7 +40,8 @@ if (cluster.isWorker) { // Disconnect worker when it is ready worker.once('listening', common.mustCall(() => { - worker.disconnect(); + const w = worker.disconnect(); + assert.strictEqual(worker, w, 'did not return a reference'); })); // Check cluster events diff --git a/test/parallel/test-cluster-worker-init.js b/test/parallel/test-cluster-worker-init.js index 3b82866d1b14eb..eaa9746c5f99a3 100644 --- a/test/parallel/test-cluster-worker-init.js +++ b/test/parallel/test-cluster-worker-init.js @@ -13,7 +13,8 @@ if (cluster.isMaster) { worker.on('message', common.mustCall((message) => { assert.strictEqual(message, true, 'did not receive expected message'); - worker.disconnect(); + const w = worker.disconnect(); + assert.strictEqual(worker, w, 'did not return a reference'); })); worker.on('online', () => {