11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci// test-cluster-worker-kill-signal.js
31cb0ef41Sopenharmony_ci// verifies that when we're killing a worker using Worker.prototype.kill
41cb0ef41Sopenharmony_ci// and the worker's process was killed with the given signal (SIGKILL)
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ciconst common = require('../common');
81cb0ef41Sopenharmony_ciconst assert = require('assert');
91cb0ef41Sopenharmony_ciconst cluster = require('cluster');
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_ciif (cluster.isWorker) {
121cb0ef41Sopenharmony_ci  // Make the worker run something
131cb0ef41Sopenharmony_ci  const http = require('http');
141cb0ef41Sopenharmony_ci  const server = http.Server(() => { });
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_ci  server.once('listening', common.mustCall());
171cb0ef41Sopenharmony_ci  server.listen(0, '127.0.0.1');
181cb0ef41Sopenharmony_ci
191cb0ef41Sopenharmony_ci} else if (cluster.isMaster) {
201cb0ef41Sopenharmony_ci  const KILL_SIGNAL = 'SIGKILL';
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_ci  // Start worker
231cb0ef41Sopenharmony_ci  const worker = cluster.fork();
241cb0ef41Sopenharmony_ci
251cb0ef41Sopenharmony_ci  // When the worker is up and running, kill it
261cb0ef41Sopenharmony_ci  worker.once('listening', common.mustCall(() => {
271cb0ef41Sopenharmony_ci    worker.kill(KILL_SIGNAL);
281cb0ef41Sopenharmony_ci  }));
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_ci  // Check worker events and properties
311cb0ef41Sopenharmony_ci  worker.on('disconnect', common.mustCall(() => {
321cb0ef41Sopenharmony_ci    assert.strictEqual(worker.exitedAfterDisconnect, false);
331cb0ef41Sopenharmony_ci    assert.strictEqual(worker.state, 'disconnected');
341cb0ef41Sopenharmony_ci  }, 1));
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_ci  // Check that the worker died
371cb0ef41Sopenharmony_ci  worker.once('exit', common.mustCall((exitCode, signalCode) => {
381cb0ef41Sopenharmony_ci    const isWorkerProcessStillAlive = common.isAlive(worker.process.pid);
391cb0ef41Sopenharmony_ci    const numOfRunningWorkers = Object.keys(cluster.workers).length;
401cb0ef41Sopenharmony_ci
411cb0ef41Sopenharmony_ci    assert.strictEqual(exitCode, null);
421cb0ef41Sopenharmony_ci    assert.strictEqual(signalCode, KILL_SIGNAL);
431cb0ef41Sopenharmony_ci    assert.strictEqual(isWorkerProcessStillAlive, false);
441cb0ef41Sopenharmony_ci    assert.strictEqual(numOfRunningWorkers, 0);
451cb0ef41Sopenharmony_ci  }, 1));
461cb0ef41Sopenharmony_ci
471cb0ef41Sopenharmony_ci  // Check if the cluster was killed as well
481cb0ef41Sopenharmony_ci  cluster.on('exit', common.mustCall(1));
491cb0ef41Sopenharmony_ci}
50