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'; 23const common = require('../common'); 24if (common.isWindows) 25 common.skip('This test is disabled on windows.'); 26 27const assert = require('assert'); 28const http = require('http'); 29const net = require('net'); 30const spawn = require('child_process').spawn; 31 32switch (process.argv[2]) { 33 case 'child': return child(); 34 case 'parent': return parent(); 35 default: return test(); 36} 37 38// Spawn the parent, and listen for it to tell us the pid of the child. 39// WARNING: This is an example of listening on some arbitrary FD number 40// that has already been bound elsewhere in advance. However, binding 41// server handles to stdio fd's is NOT a good or reliable way to do 42// concurrency in HTTP servers! Use the cluster module, or if you want 43// a more low-level approach, use child process IPC manually. 44function test() { 45 const parent = spawn(process.execPath, [__filename, 'parent'], { 46 stdio: [ 0, 'pipe', 2 ] 47 }); 48 let json = ''; 49 parent.stdout.on('data', function(c) { 50 json += c.toString(); 51 if (json.includes('\n')) next(); 52 }); 53 function next() { 54 console.error('output from parent = %s', json); 55 const child = JSON.parse(json); 56 // Now make sure that we can request to the subprocess, then kill it. 57 http.get({ 58 server: 'localhost', 59 port: child.port, 60 path: '/', 61 }).on('response', function(res) { 62 let s = ''; 63 res.on('data', function(c) { 64 s += c.toString(); 65 }); 66 res.on('end', function() { 67 // Kill the subprocess before we start doing asserts. 68 // it's really annoying when tests leave orphans! 69 process.kill(child.pid, 'SIGKILL'); 70 try { 71 parent.kill(); 72 } catch {} 73 74 assert.strictEqual(s, 'hello from child\n'); 75 assert.strictEqual(res.statusCode, 200); 76 }); 77 }); 78 } 79} 80 81function parent() { 82 const server = net.createServer(function(conn) { 83 console.error('connection on parent'); 84 conn.end('hello from parent\n'); 85 }).listen(0, function() { 86 console.error('server listening on %d', this.address().port); 87 88 const child = spawn(process.execPath, [__filename, 'child'], { 89 stdio: [ 'ignore', 'ignore', 'ignore', server._handle ], 90 detached: true 91 }); 92 93 console.log('%j\n', { pid: child.pid, port: this.address().port }); 94 95 // Now close the parent, so that the child is the only thing 96 // referencing that handle. Note that connections will still 97 // be accepted, because the child has the fd open, but the parent 98 // will exit gracefully. 99 server.close(); 100 child.unref(); 101 }); 102} 103 104function child() { 105 // Start a server on fd=3 106 http.createServer(function(req, res) { 107 console.error('request on child'); 108 console.error('%s %s', req.method, req.url, req.headers); 109 res.end('hello from child\n'); 110 }).listen({ fd: 3 }, function() { 111 console.error('child listening on fd=3'); 112 }); 113} 114