1'use strict'; 2 3const common = require('../common'); 4const assert = require('assert'); 5const { spawn } = require('child_process'); 6const net = require('net'); 7 8if (process.argv[2] === 'child') { 9 const server = net.createServer(common.mustCall()); 10 server.listen(0, common.mustCall(() => { 11 process.send({ type: 'ready', data: { port: server.address().port } }); 12 })); 13} else { 14 const cp = spawn(process.execPath, 15 [__filename, 'child'], 16 { 17 stdio: ['ipc', 'inherit', 'inherit'] 18 }); 19 20 cp.on('exit', common.mustCall((code, signal) => { 21 assert.strictEqual(code, null); 22 assert.strictEqual(signal, 'SIGKILL'); 23 })); 24 25 cp.on('message', common.mustCall((msg) => { 26 const { type, data } = msg; 27 assert.strictEqual(type, 'ready'); 28 const port = data.port; 29 30 const conn = net.createConnection({ 31 port, 32 onread: { 33 buffer: Buffer.alloc(65536), 34 callback: () => {}, 35 } 36 }); 37 38 conn.on('error', (err) => { 39 // Error emitted on Windows. 40 assert.strictEqual(err.code, 'ECONNRESET'); 41 }); 42 43 conn.on('connect', common.mustCall(() => { 44 cp.kill('SIGKILL'); 45 })); 46 })); 47} 48