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'; 23/* 24 * The purpose of this test is to make sure that when forking a process, 25 * sending a fd representing a UDP socket to the child and sending messages 26 * to this endpoint, these messages are distributed to the parent and the 27 * child process. 28 */ 29 30const common = require('../common'); 31if (common.isWindows) 32 common.skip('Sending dgram sockets to child processes is not supported'); 33 34const dgram = require('dgram'); 35const fork = require('child_process').fork; 36const assert = require('assert'); 37 38if (process.argv[2] === 'child') { 39 let childServer; 40 41 process.once('message', (msg, clusterServer) => { 42 childServer = clusterServer; 43 44 childServer.once('message', () => { 45 process.send('gotMessage'); 46 childServer.close(); 47 }); 48 49 process.send('handleReceived'); 50 }); 51 52} else { 53 const parentServer = dgram.createSocket('udp4'); 54 const client = dgram.createSocket('udp4'); 55 const child = fork(__filename, ['child']); 56 57 const msg = Buffer.from('Some bytes'); 58 59 let childGotMessage = false; 60 let parentGotMessage = false; 61 62 parentServer.once('message', (msg, rinfo) => { 63 parentGotMessage = true; 64 parentServer.close(); 65 }); 66 67 parentServer.on('listening', () => { 68 child.send('server', parentServer); 69 70 child.on('message', (msg) => { 71 if (msg === 'gotMessage') { 72 childGotMessage = true; 73 } else if (msg === 'handleReceived') { 74 sendMessages(); 75 } 76 }); 77 }); 78 79 function sendMessages() { 80 const serverPort = parentServer.address().port; 81 82 const timer = setInterval(() => { 83 /* 84 * Both the parent and the child got at least one message, 85 * test passed, clean up everything. 86 */ 87 if (parentGotMessage && childGotMessage) { 88 clearInterval(timer); 89 client.close(); 90 } else { 91 client.send( 92 msg, 93 0, 94 msg.length, 95 serverPort, 96 '127.0.0.1', 97 (err) => { 98 assert.ifError(err); 99 } 100 ); 101 } 102 }, 1); 103 } 104 105 parentServer.bind(0, '127.0.0.1'); 106 107 process.once('exit', () => { 108 assert(parentGotMessage); 109 assert(childGotMessage); 110 }); 111} 112