• 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 the `ipv6Only` option in `net.Server.listen()`
12// works as expected when we use cluster with `SCHED_RR` schedulingPolicy.
13cluster.schedulingPolicy = cluster.SCHED_RR;
14const host = '::';
15const WORKER_ACCOUNT = 3;
16
17if (cluster.isPrimary) {
18  const workers = [];
19  let address;
20
21  for (let i = 0; i < WORKER_ACCOUNT; i += 1) {
22    const myWorker = new Promise((resolve) => {
23      const worker = cluster.fork().on('exit', common.mustCall((statusCode) => {
24        assert.strictEqual(statusCode, 0);
25      })).on('listening', common.mustCall((workerAddress) => {
26        if (!address) {
27          address = workerAddress;
28        } else {
29          assert.deepStrictEqual(workerAddress, address);
30        }
31        resolve(worker);
32      }));
33    });
34
35    workers.push(myWorker);
36  }
37
38  Promise.all(workers).then(common.mustCall((resolvedWorkers) => {
39    // Make sure the `ipv6Only` option works. Should be able to use the port on
40    // IPv4.
41    const server = net.createServer().listen({
42      host: '0.0.0.0',
43      port: address.port,
44    }, common.mustCall(() => {
45      // Exit.
46      server.close();
47      resolvedWorkers.forEach((resolvedWorker) => {
48        resolvedWorker.disconnect();
49      });
50    }));
51  }));
52} else {
53  // As the cluster member has the potential to grab any port
54  // from the environment, this can cause collision when primary
55  // obtains the port from cluster member and tries to listen on.
56  // So move this to sequential, and provide a static port.
57  // Refs: https://github.com/nodejs/node/issues/25813
58  net.createServer().listen({
59    host: host,
60    port: common.PORT,
61    ipv6Only: true,
62  }, common.mustCall());
63}
64