|
| 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 dgram = require('dgram'); |
| 23 | +var fork = require('child_process').fork; |
| 24 | +var assert = require('assert'); |
| 25 | +var common = require('../common'); |
| 26 | + |
| 27 | +if (process.argv[2] === 'child') { |
| 28 | + var childCollected = 0; |
| 29 | + var server; |
| 30 | + |
| 31 | + process.on('message', function removeMe(msg, clusterServer) { |
| 32 | + if (msg === 'server') { |
| 33 | + server = clusterServer; |
| 34 | + |
| 35 | + server.on('message', function () { |
| 36 | + childCollected += 1; |
| 37 | + }); |
| 38 | + |
| 39 | + } else if (msg === 'stop') { |
| 40 | + server.close(); |
| 41 | + process.send(childCollected); |
| 42 | + process.removeListener('message', removeMe); |
| 43 | + } |
| 44 | + }); |
| 45 | + |
| 46 | +} else { |
| 47 | + var server = dgram.createSocket('udp4'); |
| 48 | + var client = dgram.createSocket('udp4'); |
| 49 | + var child = fork(__filename, ['child']); |
| 50 | + var msg = new Buffer('Some bytes'); |
| 51 | + |
| 52 | + var parentCollected = 0; |
| 53 | + var childCollected = 0; |
| 54 | + server.on('message', function (msg, rinfo) { |
| 55 | + parentCollected += 1; |
| 56 | + }); |
| 57 | + |
| 58 | + server.on('listening', function () { |
| 59 | + child.send('server', server); |
| 60 | + |
| 61 | + sendMessages(); |
| 62 | + }); |
| 63 | + |
| 64 | + var sendMessages = function () { |
| 65 | + var wait = 0; |
| 66 | + var send = 0; |
| 67 | + var total = 100; |
| 68 | + |
| 69 | + var timer = setInterval(function () { |
| 70 | + send += 1; |
| 71 | + if (send === total) { |
| 72 | + clearInterval(timer); |
| 73 | + } |
| 74 | + |
| 75 | + client.send(msg, 0, msg.length, common.PORT, '127.0.0.1', function(err) { |
| 76 | + if (err) throw err; |
| 77 | + |
| 78 | + wait += 1; |
| 79 | + if (wait === total) { |
| 80 | + shutdown(); |
| 81 | + } |
| 82 | + } |
| 83 | + ); |
| 84 | + }, 1); |
| 85 | + }; |
| 86 | + |
| 87 | + var shutdown = function () { |
| 88 | + child.send('stop'); |
| 89 | + child.once('message', function (collected) { |
| 90 | + childCollected = collected; |
| 91 | + }); |
| 92 | + |
| 93 | + server.close(); |
| 94 | + client.close(); |
| 95 | + }; |
| 96 | + |
| 97 | + server.bind(common.PORT, '127.0.0.1'); |
| 98 | + |
| 99 | + process.once('exit', function () { |
| 100 | + assert(childCollected > 0); |
| 101 | + assert(parentCollected > 0); |
| 102 | + }); |
| 103 | +} |
0 commit comments