|
| 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 | +var common = require('../common'); |
| 23 | +var assert = require('assert'); |
| 24 | +var cluster = require('cluster'); |
| 25 | +var dgram = require('dgram'); |
| 26 | + |
| 27 | +// Without an explicit bind, send() causes an implicit bind, which always |
| 28 | +// generate a unique per-socket ephemeral port. An explicit bind to a port |
| 29 | +// number causes all sockets bound to that number to share a port. |
| 30 | +// |
| 31 | +// The 2 workers that call bind() will share a port, the two workers that do |
| 32 | +// not will not share a port, so master will see 3 unique source ports. |
| 33 | + |
| 34 | +// Note that on Windows, clustered dgram is not supported. Since explicit |
| 35 | +// binding causes the dgram to be clustered, don't fork the workers that bind. |
| 36 | +// This is a useful test, still, because it demonstrates that by avoiding |
| 37 | +// clustering, client (ephemeral, implicitly bound) dgram sockets become |
| 38 | +// supported while using cluster, though servers still cause the master to error |
| 39 | +// with ENOTSUP. |
| 40 | + |
| 41 | +var windows = process.platform === 'win32'; |
| 42 | + |
| 43 | +if (cluster.isMaster) { |
| 44 | + var pass; |
| 45 | + var messages = 0; |
| 46 | + var ports = {}; |
| 47 | + |
| 48 | + process.on('exit', function() { |
| 49 | + assert.equal(pass, true); |
| 50 | + }); |
| 51 | + |
| 52 | + var target = dgram.createSocket('udp4'); |
| 53 | + |
| 54 | + target.on('message', function(buf, rinfo) { |
| 55 | + messages++; |
| 56 | + ports[rinfo.port] = true; |
| 57 | + |
| 58 | + if (windows && messages === 2) { |
| 59 | + assert.equal(Object.keys(ports).length, 2); |
| 60 | + done(); |
| 61 | + } |
| 62 | + |
| 63 | + if (!windows && messages === 4) { |
| 64 | + assert.equal(Object.keys(ports).length, 3); |
| 65 | + done(); |
| 66 | + } |
| 67 | + |
| 68 | + function done() { |
| 69 | + pass = true; |
| 70 | + cluster.disconnect(); |
| 71 | + target.close(); |
| 72 | + } |
| 73 | + }); |
| 74 | + |
| 75 | + target.on('listening', function() { |
| 76 | + cluster.fork(); |
| 77 | + cluster.fork(); |
| 78 | + if (!windows) { |
| 79 | + cluster.fork({BOUND: 'y'}); |
| 80 | + cluster.fork({BOUND: 'y'}); |
| 81 | + } |
| 82 | + }); |
| 83 | + |
| 84 | + target.bind({port: common.PORT, exclusive: true}); |
| 85 | + |
| 86 | + return; |
| 87 | +} |
| 88 | + |
| 89 | +var source = dgram.createSocket('udp4'); |
| 90 | + |
| 91 | +if (process.env.BOUND === 'y') { |
| 92 | + source.bind(0); |
| 93 | +} else { |
| 94 | + // cluster doesn't know about exclusive sockets, so it won't close them. This |
| 95 | + // is expected, its the same situation for timers, outgoing tcp connections, |
| 96 | + // etc, which also keep workers alive after disconnect was requested. |
| 97 | + source.unref(); |
| 98 | +} |
| 99 | + |
| 100 | +source.send(Buffer('abc'), 0, 3, common.PORT, '127.0.0.1'); |
0 commit comments