11cb0ef41Sopenharmony_ci// Copyright Joyent, Inc. and other Node contributors. 21cb0ef41Sopenharmony_ci// 31cb0ef41Sopenharmony_ci// Permission is hereby granted, free of charge, to any person obtaining a 41cb0ef41Sopenharmony_ci// copy of this software and associated documentation files (the 51cb0ef41Sopenharmony_ci// "Software"), to deal in the Software without restriction, including 61cb0ef41Sopenharmony_ci// without limitation the rights to use, copy, modify, merge, publish, 71cb0ef41Sopenharmony_ci// distribute, sublicense, and/or sell copies of the Software, and to permit 81cb0ef41Sopenharmony_ci// persons to whom the Software is furnished to do so, subject to the 91cb0ef41Sopenharmony_ci// following conditions: 101cb0ef41Sopenharmony_ci// 111cb0ef41Sopenharmony_ci// The above copyright notice and this permission notice shall be included 121cb0ef41Sopenharmony_ci// in all copies or substantial portions of the Software. 131cb0ef41Sopenharmony_ci// 141cb0ef41Sopenharmony_ci// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 151cb0ef41Sopenharmony_ci// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 161cb0ef41Sopenharmony_ci// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN 171cb0ef41Sopenharmony_ci// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 181cb0ef41Sopenharmony_ci// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 191cb0ef41Sopenharmony_ci// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 201cb0ef41Sopenharmony_ci// USE OR OTHER DEALINGS IN THE SOFTWARE. 211cb0ef41Sopenharmony_ci 221cb0ef41Sopenharmony_ci'use strict'; 231cb0ef41Sopenharmony_ci 241cb0ef41Sopenharmony_ci// The purpose of this test is to make sure that when forking a process, 251cb0ef41Sopenharmony_ci// sending a fd representing a UDP socket to the child and sending messages 261cb0ef41Sopenharmony_ci// to this endpoint, these messages are distributed to the parent and the 271cb0ef41Sopenharmony_ci// child process. 281cb0ef41Sopenharmony_ci 291cb0ef41Sopenharmony_ci 301cb0ef41Sopenharmony_ciconst common = require('../common'); 311cb0ef41Sopenharmony_ciif (common.isWindows) 321cb0ef41Sopenharmony_ci common.skip('Sending dgram sockets to child processes is not supported'); 331cb0ef41Sopenharmony_ci 341cb0ef41Sopenharmony_ciconst dgram = require('dgram'); 351cb0ef41Sopenharmony_ciconst fork = require('child_process').fork; 361cb0ef41Sopenharmony_ciconst assert = require('assert'); 371cb0ef41Sopenharmony_ci 381cb0ef41Sopenharmony_ciif (process.argv[2] === 'child') { 391cb0ef41Sopenharmony_ci let childServer; 401cb0ef41Sopenharmony_ci 411cb0ef41Sopenharmony_ci process.once('message', (msg, clusterServer) => { 421cb0ef41Sopenharmony_ci childServer = clusterServer; 431cb0ef41Sopenharmony_ci 441cb0ef41Sopenharmony_ci childServer.once('message', () => { 451cb0ef41Sopenharmony_ci process.send('gotMessage'); 461cb0ef41Sopenharmony_ci childServer.close(); 471cb0ef41Sopenharmony_ci }); 481cb0ef41Sopenharmony_ci 491cb0ef41Sopenharmony_ci process.send('handleReceived'); 501cb0ef41Sopenharmony_ci }); 511cb0ef41Sopenharmony_ci 521cb0ef41Sopenharmony_ci} else { 531cb0ef41Sopenharmony_ci const parentServer = dgram.createSocket('udp4'); 541cb0ef41Sopenharmony_ci const client = dgram.createSocket('udp4'); 551cb0ef41Sopenharmony_ci const child = fork(__filename, ['child']); 561cb0ef41Sopenharmony_ci 571cb0ef41Sopenharmony_ci const msg = Buffer.from('Some bytes'); 581cb0ef41Sopenharmony_ci 591cb0ef41Sopenharmony_ci let childGotMessage = false; 601cb0ef41Sopenharmony_ci let parentGotMessage = false; 611cb0ef41Sopenharmony_ci 621cb0ef41Sopenharmony_ci parentServer.once('message', (msg, rinfo) => { 631cb0ef41Sopenharmony_ci parentGotMessage = true; 641cb0ef41Sopenharmony_ci parentServer.close(); 651cb0ef41Sopenharmony_ci }); 661cb0ef41Sopenharmony_ci 671cb0ef41Sopenharmony_ci parentServer.on('listening', () => { 681cb0ef41Sopenharmony_ci child.send('server', parentServer); 691cb0ef41Sopenharmony_ci 701cb0ef41Sopenharmony_ci child.on('message', (msg) => { 711cb0ef41Sopenharmony_ci if (msg === 'gotMessage') { 721cb0ef41Sopenharmony_ci childGotMessage = true; 731cb0ef41Sopenharmony_ci } else if (msg === 'handleReceived') { 741cb0ef41Sopenharmony_ci sendMessages(); 751cb0ef41Sopenharmony_ci } 761cb0ef41Sopenharmony_ci }); 771cb0ef41Sopenharmony_ci }); 781cb0ef41Sopenharmony_ci 791cb0ef41Sopenharmony_ci function sendMessages() { 801cb0ef41Sopenharmony_ci const serverPort = parentServer.address().port; 811cb0ef41Sopenharmony_ci 821cb0ef41Sopenharmony_ci const timer = setInterval(() => { 831cb0ef41Sopenharmony_ci // Both the parent and the child got at least one message, 841cb0ef41Sopenharmony_ci // test passed, clean up everything. 851cb0ef41Sopenharmony_ci if (parentGotMessage && childGotMessage) { 861cb0ef41Sopenharmony_ci clearInterval(timer); 871cb0ef41Sopenharmony_ci client.close(); 881cb0ef41Sopenharmony_ci } else { 891cb0ef41Sopenharmony_ci client.send( 901cb0ef41Sopenharmony_ci msg, 911cb0ef41Sopenharmony_ci 0, 921cb0ef41Sopenharmony_ci msg.length, 931cb0ef41Sopenharmony_ci serverPort, 941cb0ef41Sopenharmony_ci '127.0.0.1', 951cb0ef41Sopenharmony_ci (err) => { 961cb0ef41Sopenharmony_ci assert.ifError(err); 971cb0ef41Sopenharmony_ci } 981cb0ef41Sopenharmony_ci ); 991cb0ef41Sopenharmony_ci } 1001cb0ef41Sopenharmony_ci }, 1); 1011cb0ef41Sopenharmony_ci } 1021cb0ef41Sopenharmony_ci 1031cb0ef41Sopenharmony_ci parentServer.bind(0, '127.0.0.1'); 1041cb0ef41Sopenharmony_ci 1051cb0ef41Sopenharmony_ci process.once('exit', () => { 1061cb0ef41Sopenharmony_ci assert(parentGotMessage); 1071cb0ef41Sopenharmony_ci assert(childGotMessage); 1081cb0ef41Sopenharmony_ci }); 1091cb0ef41Sopenharmony_ci} 110