summaryrefslogtreecommitdiff
path: root/test/parallel/test-cluster-net-listen-ipv6only-false.js
blob: f48e9f1c29dc4a015c8dcd6f411284576d712b64 (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
49
50
51
52
53
54
55
56
'use strict';

const common = require('../common');
if (!common.hasIPv6)
  common.skip('no IPv6 support');

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

// This test ensures that dual-stack support still works for cluster module
// when `ipv6Only` is not `true`.
const host = '::';
const WORKER_COUNT = 3;

if (cluster.isMaster) {
  const workers = [];
  let address;

  for (let i = 0; i < WORKER_COUNT; i += 1) {
    const myWorker = new Promise((resolve) => {
      const worker = cluster.fork().on('exit', common.mustCall((statusCode) => {
        assert.strictEqual(statusCode, 0);
      })).on('listening', common.mustCall((workerAddress) => {
        if (!address) {
          address = workerAddress;
        } else {
          assert.strictEqual(address.addressType, workerAddress.addressType);
          assert.strictEqual(address.host, workerAddress.host);
          assert.strictEqual(address.port, workerAddress.port);
        }
        resolve(worker);
      }));
    });

    workers.push(myWorker);
  }

  Promise.all(workers).then(common.mustCall((resolvedWorkers) => {
    const socket = net.connect({
      port: address.port,
      host: '0.0.0.0',
    }, common.mustCall(() => {
      socket.destroy();
      resolvedWorkers.forEach((resolvedWorker) => {
        resolvedWorker.disconnect();
      });
    }));
    socket.on('error', common.mustNotCall());
  }));
} else {
  net.createServer().listen({
    host,
    port: 0,
  }, common.mustCall());
}