• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3const common = require('../common');
4if (!common.hasIPv6)
5  common.skip('no IPv6 support');
6
7const assert = require('assert');
8const cluster = require('cluster');
9const net = require('net');
10
11// This test ensures that dual-stack support still works for cluster module
12// when `ipv6Only` is not `true`.
13const host = '::';
14const WORKER_COUNT = 3;
15
16if (cluster.isPrimary) {
17  const workers = [];
18  let address;
19
20  for (let i = 0; i < WORKER_COUNT; i += 1) {
21    const myWorker = new Promise((resolve) => {
22      const worker = cluster.fork().on('exit', common.mustCall((statusCode) => {
23        assert.strictEqual(statusCode, 0);
24      })).on('listening', common.mustCall((workerAddress) => {
25        if (!address) {
26          address = workerAddress;
27        } else {
28          assert.strictEqual(address.addressType, workerAddress.addressType);
29          assert.strictEqual(address.host, workerAddress.host);
30          assert.strictEqual(address.port, workerAddress.port);
31        }
32        resolve(worker);
33      }));
34    });
35
36    workers.push(myWorker);
37  }
38
39  Promise.all(workers).then(common.mustCall((resolvedWorkers) => {
40    const socket = net.connect({
41      port: address.port,
42      host: '0.0.0.0',
43    }, common.mustCall(() => {
44      socket.destroy();
45      resolvedWorkers.forEach((resolvedWorker) => {
46        resolvedWorker.disconnect();
47      });
48    }));
49    socket.on('error', common.mustNotCall());
50  }));
51} else {
52  net.createServer().listen({
53    host,
54    port: 0,
55  }, common.mustCall());
56}
57