|
| 1 | +'use strict'; |
| 2 | +const common = require('../common'); |
| 3 | +const assert = require('assert'); |
| 4 | +const net = require('net'); |
| 5 | +const child_process = require('child_process'); |
| 6 | +const tmpdir = require('../common/tmpdir'); |
| 7 | + |
| 8 | +const CODE = ` |
| 9 | + const net = require('net'); |
| 10 | + process.on('message', (message, handle) => { |
| 11 | + // net.Socket or net.Server |
| 12 | + handle instanceof net.Socket ? handle.destroy() : handle.close(); |
| 13 | + process.send('accepted'); |
| 14 | + }) |
| 15 | +`; |
| 16 | + |
| 17 | +const child = child_process.fork(process.execPath, { execArgv: ['-e', CODE ] }); |
| 18 | + |
| 19 | +let tcpServer; |
| 20 | +let pipeServer; |
| 21 | +const sockets = []; |
| 22 | +let accepted = 0; |
| 23 | + |
| 24 | +child.on('message', (message) => { |
| 25 | + assert.strictEqual(message, 'accepted'); |
| 26 | + if (++accepted === 4) { |
| 27 | + child.kill(); |
| 28 | + tcpServer.close(); |
| 29 | + pipeServer.close(); |
| 30 | + sockets.forEach((socket) => { |
| 31 | + socket.destroy(); |
| 32 | + }); |
| 33 | + } |
| 34 | +}); |
| 35 | + |
| 36 | +// Pipe |
| 37 | +tmpdir.refresh(); |
| 38 | +pipeServer = net.createServer(common.mustCall((socket) => { |
| 39 | + child.send(null, socket); |
| 40 | + sockets.push(socket); |
| 41 | +})); |
| 42 | +pipeServer.listen(common.PIPE, common.mustCall(() => { |
| 43 | + net.connect(common.PIPE); |
| 44 | + child.send(null, pipeServer); |
| 45 | +})); |
| 46 | + |
| 47 | +// TCP |
| 48 | +tcpServer = net.createServer(common.mustCall((socket) => { |
| 49 | + const setSimultaneousAccepts = socket._handle.setSimultaneousAccepts; |
| 50 | + if (typeof setSimultaneousAccepts === 'function') { |
| 51 | + socket._handle.setSimultaneousAccepts = common.mustCall((...args) => { |
| 52 | + const ret = setSimultaneousAccepts.call(socket._handle, ...args); |
| 53 | + assert.strictEqual(ret, undefined); |
| 54 | + }); |
| 55 | + } |
| 56 | + child.send(null, socket._handle); |
| 57 | + sockets.push(socket); |
| 58 | +})); |
| 59 | +tcpServer.listen(0, common.mustCall(() => { |
| 60 | + net.connect(tcpServer.address().port); |
| 61 | + const setSimultaneousAccepts = tcpServer._handle.setSimultaneousAccepts; |
| 62 | + if (typeof setSimultaneousAccepts === 'function') { |
| 63 | + tcpServer._handle.setSimultaneousAccepts = common.mustCall((...args) => { |
| 64 | + const ret = setSimultaneousAccepts.call(tcpServer._handle, ...args); |
| 65 | + assert.strictEqual(ret, 0); |
| 66 | + }); |
| 67 | + } |
| 68 | + child.send(null, tcpServer); |
| 69 | +})); |
0 commit comments