• 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_NONE` schedulingPolicy.
13cluster.schedulingPolicy = cluster.SCHED_NONE;
14const host = '::';
15const WORKER_ACCOUNT = 3;
16
17if (cluster.isMaster) {
18  const workers = [];
19
20  for (let i = 0; i < WORKER_ACCOUNT; 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        assert.strictEqual(workerAddress.addressType, 6);
26        assert.strictEqual(workerAddress.address, host);
27        assert.strictEqual(workerAddress.port, common.PORT);
28        resolve(worker);
29      }));
30    });
31
32    workers.push(myWorker);
33  }
34
35  Promise.all(workers).then(common.mustCall((resolvedWorkers) => {
36    // Make sure the `ipv6Only` option works. This is the part of the test that
37    // requires the whole test to use `common.PORT` rather than port `0`. If it
38    // used port `0` instead, then the operating system can supply a port that
39    // is available for the IPv6 interface but in use by the IPv4 interface.
40    // Refs: https://github.com/nodejs/node/issues/29679
41    const server = net.createServer().listen({
42      host: '0.0.0.0',
43      port: common.PORT,
44    }, common.mustCall(() => {
45      // Exit.
46      server.close();
47      resolvedWorkers.forEach((resolvedWorker) => {
48        resolvedWorker.disconnect();
49      });
50    }));
51  }));
52} else {
53  net.createServer().listen({
54    host,
55    port: common.PORT,
56    ipv6Only: true,
57  }, common.mustCall());
58}
59