summaryrefslogtreecommitdiff
path: root/test/parallel/test-cluster-concurrent-disconnect.js
blob: 22f720402630277e04a3f0b8513195ffb79367e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
'use strict';

// Ref: https://github.com/nodejs/node/issues/32106

const common = require('../common');

const assert = require('assert');
const cluster = require('cluster');
const os = require('os');

if (cluster.isMaster) {
  const workers = [];
  const numCPUs = os.cpus().length;
  let waitOnline = numCPUs;
  for (let i = 0; i < numCPUs; i++) {
    const worker = cluster.fork();
    workers[i] = worker;
    worker.once('online', common.mustCall(() => {
      if (--waitOnline === 0)
        for (const worker of workers)
          if (worker.isConnected())
            worker.send(i % 2 ? 'disconnect' : 'destroy');
    }));

    // These errors can occur due to the nature of the test, we might be trying
    // to send messages when the worker is disconnecting.
    worker.on('error', (err) => {
      assert.strictEqual(err.syscall, 'write');
      assert.strictEqual(err.code, 'EPIPE');
    });

    worker.once('disconnect', common.mustCall(() => {
      for (const worker of workers)
        if (worker.isConnected())
          worker.send('disconnect');
    }));

    worker.once('exit', common.mustCall((code, signal) => {
      assert.strictEqual(code, 0);
      assert.strictEqual(signal, null);
    }));
  }
} else {
  process.on('message', (msg) => {
    if (cluster.worker.isConnected())
      cluster.worker[msg]();
  });
}