1'use strict';
2// test-cluster-worker-kill-signal.js
3// verifies that when we're killing a worker using Worker.prototype.kill
4// and the worker's process was killed with the given signal (SIGKILL)
5
6
7const common = require('../common');
8const assert = require('assert');
9const cluster = require('cluster');
10
11if (cluster.isWorker) {
12  // Make the worker run something
13  const http = require('http');
14  const server = http.Server(() => { });
15
16  server.once('listening', common.mustCall());
17  server.listen(0, '127.0.0.1');
18
19} else if (cluster.isMaster) {
20  const KILL_SIGNAL = 'SIGKILL';
21
22  // Start worker
23  const worker = cluster.fork();
24
25  // When the worker is up and running, kill it
26  worker.once('listening', common.mustCall(() => {
27    worker.kill(KILL_SIGNAL);
28  }));
29
30  // Check worker events and properties
31  worker.on('disconnect', common.mustCall(() => {
32    assert.strictEqual(worker.exitedAfterDisconnect, false);
33    assert.strictEqual(worker.state, 'disconnected');
34  }, 1));
35
36  // Check that the worker died
37  worker.once('exit', common.mustCall((exitCode, signalCode) => {
38    const isWorkerProcessStillAlive = common.isAlive(worker.process.pid);
39    const numOfRunningWorkers = Object.keys(cluster.workers).length;
40
41    assert.strictEqual(exitCode, null);
42    assert.strictEqual(signalCode, KILL_SIGNAL);
43    assert.strictEqual(isWorkerProcessStillAlive, false);
44    assert.strictEqual(numOfRunningWorkers, 0);
45  }, 1));
46
47  // Check if the cluster was killed as well
48  cluster.on('exit', common.mustCall(1));
49}
50