• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2// Copyright Joyent, Inc. and other Node contributors.
3//
4// Permission is hereby granted, free of charge, to any person obtaining a
5// copy of this software and associated documentation files (the
6// "Software"), to deal in the Software without restriction, including
7// without limitation the rights to use, copy, modify, merge, publish,
8// distribute, sublicense, and/or sell copies of the Software, and to permit
9// persons to whom the Software is furnished to do so, subject to the
10// following conditions:
11//
12// The above copyright notice and this permission notice shall be included
13// in all copies or substantial portions of the Software.
14//
15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
18// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
19// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21// USE OR OTHER DEALINGS IN THE SOFTWARE.
22
23const common = require('../common');
24const assert = require('assert');
25const cluster = require('cluster');
26const dgram = require('dgram');
27
28// Without an explicit bind, send() causes an implicit bind, which always
29// generate a unique per-socket ephemeral port. An explicit bind to a port
30// number causes all sockets bound to that number to share a port.
31//
32// The 2 workers that call bind() will share a port, the two workers that do
33// not will not share a port, so master will see 3 unique source ports.
34
35// Note that on Windows, clustered dgram is not supported. Since explicit
36// binding causes the dgram to be clustered, don't fork the workers that bind.
37// This is a useful test, still, because it demonstrates that by avoiding
38// clustering, client (ephemeral, implicitly bound) dgram sockets become
39// supported while using cluster, though servers still cause the master to error
40// with ENOTSUP.
41
42if (cluster.isMaster) {
43  let messages = 0;
44  const ports = {};
45  const pids = [];
46
47  const target = dgram.createSocket('udp4');
48
49  const done = common.mustCall(function() {
50    cluster.disconnect();
51    target.close();
52  });
53
54  target.on('message', function(buf, rinfo) {
55    if (pids.includes(buf.toString()))
56      return;
57    pids.push(buf.toString());
58    messages++;
59    ports[rinfo.port] = true;
60
61    if (common.isWindows && messages === 2) {
62      assert.strictEqual(Object.keys(ports).length, 2);
63      done();
64    }
65
66    if (!common.isWindows && messages === 4) {
67      assert.strictEqual(Object.keys(ports).length, 3);
68      done();
69    }
70  });
71
72  target.on('listening', function() {
73    cluster.fork({ PORT: target.address().port });
74    cluster.fork({ PORT: target.address().port });
75    if (!common.isWindows) {
76      cluster.fork({ BOUND: 'y', PORT: target.address().port });
77      cluster.fork({ BOUND: 'y', PORT: target.address().port });
78    }
79  });
80
81  target.bind({ port: 0, exclusive: true });
82
83  return;
84}
85
86const source = dgram.createSocket('udp4');
87
88source.on('close', function() {
89  clearInterval(interval);
90});
91
92if (process.env.BOUND === 'y') {
93  source.bind(0);
94} else {
95  // Cluster doesn't know about exclusive sockets, so it won't close them. This
96  // is expected, its the same situation for timers, outgoing tcp connections,
97  // etc, which also keep workers alive after disconnect was requested.
98  source.unref();
99}
100
101assert(process.env.PORT);
102const buf = Buffer.from(process.pid.toString());
103const interval = setInterval(() => {
104  source.send(buf, process.env.PORT, '127.0.0.1');
105}, 1).unref();
106