|
| 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 | +const common = require('../common'); |
| 24 | +const assert = require('assert'); |
| 25 | +const fork = require('child_process').fork; |
| 26 | +const net = require('net'); |
| 27 | + |
| 28 | +function ProgressTracker(missing, callback) { |
| 29 | + this.missing = missing; |
| 30 | + this.callback = callback; |
| 31 | +} |
| 32 | +ProgressTracker.prototype.done = function() { |
| 33 | + this.missing -= 1; |
| 34 | + this.check(); |
| 35 | +}; |
| 36 | +ProgressTracker.prototype.check = function() { |
| 37 | + if (this.missing === 0) this.callback(); |
| 38 | +}; |
| 39 | + |
| 40 | +if (process.argv[2] === 'child') { |
| 41 | + |
| 42 | + let serverScope; |
| 43 | + |
| 44 | + process.on('message', function onServer(msg, server) { |
| 45 | + if (msg.what !== 'server') return; |
| 46 | + process.removeListener('message', onServer); |
| 47 | + |
| 48 | + serverScope = server; |
| 49 | + |
| 50 | + server.on('connection', function(socket) { |
| 51 | + console.log('CHILD: got connection'); |
| 52 | + process.send({ what: 'connection' }); |
| 53 | + socket.destroy(); |
| 54 | + }); |
| 55 | + |
| 56 | + // Start making connection from parent. |
| 57 | + console.log('CHILD: server listening'); |
| 58 | + process.send({ what: 'listening' }); |
| 59 | + }); |
| 60 | + |
| 61 | + process.on('message', function onClose(msg) { |
| 62 | + if (msg.what !== 'close') return; |
| 63 | + process.removeListener('message', onClose); |
| 64 | + |
| 65 | + serverScope.on('close', function() { |
| 66 | + process.send({ what: 'close' }); |
| 67 | + }); |
| 68 | + serverScope.close(); |
| 69 | + }); |
| 70 | + |
| 71 | + process.send({ what: 'ready' }); |
| 72 | +} else { |
| 73 | + |
| 74 | + const child = fork(process.argv[1], ['child']); |
| 75 | + |
| 76 | + child.on('exit', common.mustCall(function(code, signal) { |
| 77 | + const message = `CHILD: died with ${code}, ${signal}`; |
| 78 | + assert.strictEqual(code, 0, message); |
| 79 | + })); |
| 80 | + |
| 81 | + // Send net.Server to child and test by connecting. |
| 82 | + function testServer(callback) { |
| 83 | + |
| 84 | + // Destroy server execute callback when done. |
| 85 | + const progress = new ProgressTracker(2, function() { |
| 86 | + server.on('close', function() { |
| 87 | + console.log('PARENT: server closed'); |
| 88 | + child.send({ what: 'close' }); |
| 89 | + }); |
| 90 | + server.close(); |
| 91 | + }); |
| 92 | + |
| 93 | + // We expect 4 connections and close events. |
| 94 | + const connections = new ProgressTracker(4, progress.done.bind(progress)); |
| 95 | + const closed = new ProgressTracker(4, progress.done.bind(progress)); |
| 96 | + |
| 97 | + // Create server and send it to child. |
| 98 | + const server = net.createServer(); |
| 99 | + server.on('connection', function(socket) { |
| 100 | + console.log('PARENT: got connection'); |
| 101 | + socket.destroy(); |
| 102 | + connections.done(); |
| 103 | + }); |
| 104 | + server.on('listening', function() { |
| 105 | + console.log('PARENT: server listening'); |
| 106 | + child.send({ what: 'server' }, server); |
| 107 | + }); |
| 108 | + server.listen(0); |
| 109 | + |
| 110 | + // Handle client messages. |
| 111 | + function messageHandlers(msg) { |
| 112 | + |
| 113 | + if (msg.what === 'listening') { |
| 114 | + // Make connections. |
| 115 | + let socket; |
| 116 | + for (let i = 0; i < 4; i++) { |
| 117 | + socket = net.connect(server.address().port, function() { |
| 118 | + console.log('CLIENT: connected'); |
| 119 | + }); |
| 120 | + socket.on('close', function() { |
| 121 | + closed.done(); |
| 122 | + console.log('CLIENT: closed'); |
| 123 | + }); |
| 124 | + } |
| 125 | + |
| 126 | + } else if (msg.what === 'connection') { |
| 127 | + // child got connection |
| 128 | + connections.done(); |
| 129 | + } else if (msg.what === 'close') { |
| 130 | + child.removeListener('message', messageHandlers); |
| 131 | + callback(); |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + child.on('message', messageHandlers); |
| 136 | + } |
| 137 | + |
| 138 | + // Create server and send it to child. |
| 139 | + child.on('message', function onReady(msg) { |
| 140 | + if (msg.what !== 'ready') return; |
| 141 | + child.removeListener('message', onReady); |
| 142 | + |
| 143 | + testServer(common.mustCall()); |
| 144 | + }); |
| 145 | +} |
0 commit comments