• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright Joyent, Inc. and other Node contributors.
2//
3// Permission is hereby granted, free of charge, to any person obtaining a
4// copy of this software and associated documentation files (the
5// "Software"), to deal in the Software without restriction, including
6// without limitation the rights to use, copy, modify, merge, publish,
7// distribute, sublicense, and/or sell copies of the Software, and to permit
8// persons to whom the Software is furnished to do so, subject to the
9// following conditions:
10//
11// The above copyright notice and this permission notice shall be included
12// in all copies or substantial portions of the Software.
13//
14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20// USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22'use strict';
23const common = require('../common');
24if (common.isWindows)
25  common.skip('dgram clustering is currently not supported on Windows.');
26
27const NUM_WORKERS = 4;
28const PACKETS_PER_WORKER = 10;
29
30const cluster = require('cluster');
31const dgram = require('dgram');
32const assert = require('assert');
33
34if (cluster.isMaster)
35  master();
36else
37  worker();
38
39
40function master() {
41  let received = 0;
42
43  // Start listening on a socket.
44  const socket = dgram.createSocket('udp4');
45  socket.bind({ port: 0 }, common.mustCall(() => {
46
47    // Fork workers.
48    for (let i = 0; i < NUM_WORKERS; i++) {
49      const worker = cluster.fork();
50      worker.send({ port: socket.address().port });
51    }
52  }));
53
54  // Disconnect workers when the expected number of messages have been
55  // received.
56  socket.on('message', common.mustCall((data, info) => {
57    received++;
58
59    if (received === PACKETS_PER_WORKER * NUM_WORKERS) {
60
61      // Close the socket.
62      socket.close();
63
64      // Disconnect all workers.
65      cluster.disconnect();
66    }
67  }, NUM_WORKERS * PACKETS_PER_WORKER));
68}
69
70
71function worker() {
72  // Create udp socket and send packets to master.
73  const socket = dgram.createSocket('udp4');
74  const buf = Buffer.from('hello world');
75
76  // This test is intended to exercise the cluster binding of udp sockets, but
77  // since sockets aren't clustered when implicitly bound by at first call of
78  // send(), explicitly bind them to an ephemeral port.
79  socket.bind(0);
80
81  process.on('message', common.mustCall((msg) => {
82    assert(msg.port);
83
84    // There is no guarantee that a sent dgram packet will be received so keep
85    // sending until disconnect.
86    const interval = setInterval(() => {
87      socket.send(buf, 0, buf.length, msg.port, '127.0.0.1');
88    }, 1);
89
90    cluster.worker.on('disconnect', () => {
91      clearInterval(interval);
92    });
93  }));
94}
95