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 // Both the parent and the child got at least one message, 84 // test passed, clean up everything. 85 if (parentGotMessage && childGotMessage) { 86 clearInterval(timer); 87 client.close(); 88 } else { 89 client.send( 90 msg, 91 0, 92 msg.length, 93 serverPort, 94 '127.0.0.1', 95 (err) => { 96 assert.ifError(err); 97 } 98 ); 99 } 100 }, 1); 101 } 102 103 parentServer.bind(0, '127.0.0.1'); 104 105 process.once('exit', () => { 106 assert(parentGotMessage); 107 assert(childGotMessage); 108 }); 109} 110