|
| 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 net = require('net'); |
| 26 | + |
| 27 | +function testClients(getSocketOpt, getConnectOpt, getConnectCb) { |
| 28 | + const cloneOptions = (index) => |
| 29 | + Object.assign({}, getSocketOpt(index), getConnectOpt(index)); |
| 30 | + return [ |
| 31 | + net.connect(cloneOptions(0), getConnectCb(0)), |
| 32 | + net.connect(cloneOptions(1)) |
| 33 | + .on('connect', getConnectCb(1)), |
| 34 | + net.createConnection(cloneOptions(2), getConnectCb(2)), |
| 35 | + net.createConnection(cloneOptions(3)) |
| 36 | + .on('connect', getConnectCb(3)), |
| 37 | + new net.Socket(getSocketOpt(4)).connect(getConnectOpt(4), getConnectCb(4)), |
| 38 | + new net.Socket(getSocketOpt(5)).connect(getConnectOpt(5)) |
| 39 | + .on('connect', getConnectCb(5)) |
| 40 | + ]; |
| 41 | +} |
| 42 | + |
| 43 | +const CLIENT_VARIANTS = 6; // Same length as array above |
| 44 | +const forAllClients = (cb) => common.mustCall(cb, CLIENT_VARIANTS); |
| 45 | + |
| 46 | +// Test allowHalfOpen |
| 47 | +{ |
| 48 | + let clientReceivedFIN = 0; |
| 49 | + let serverConnections = 0; |
| 50 | + let clientSentFIN = 0; |
| 51 | + let serverReceivedFIN = 0; |
| 52 | + const server = net.createServer({ |
| 53 | + allowHalfOpen: true |
| 54 | + }) |
| 55 | + .on('connection', forAllClients(function serverOnConnection(socket) { |
| 56 | + const serverConnection = ++serverConnections; |
| 57 | + let clientId; |
| 58 | + console.error(`${serverConnections} 'connection' emitted on server`); |
| 59 | + socket.resume(); |
| 60 | + // 'end' on each socket must not be emitted twice |
| 61 | + socket.on('data', common.mustCall(function(data) { |
| 62 | + clientId = data.toString(); |
| 63 | + console.error(`${serverConnection} server connection is started ` + |
| 64 | + `by client No. ${clientId}`); |
| 65 | + })); |
| 66 | + socket.on('end', common.mustCall(function() { |
| 67 | + serverReceivedFIN++; |
| 68 | + console.error(`Server recieved FIN sent by No. ${clientId}`); |
| 69 | + if (serverReceivedFIN === CLIENT_VARIANTS) { |
| 70 | + setTimeout(() => { |
| 71 | + server.close(); |
| 72 | + console.error(`No. ${clientId} connection is closing server: ` + |
| 73 | + `${serverReceivedFIN} FIN received by server, ` + |
| 74 | + `${clientReceivedFIN} FIN received by client, ` + |
| 75 | + `${clientSentFIN} FIN sent by client, ` + |
| 76 | + `${serverConnections} FIN sent by server`); |
| 77 | + }, 50); |
| 78 | + } |
| 79 | + }, 1)); |
| 80 | + socket.end(); |
| 81 | + console.error(`Server has sent ${serverConnections} FIN`); |
| 82 | + })) |
| 83 | + .on('close', common.mustCall(function serverOnClose() { |
| 84 | + console.error('Server has been closed: ' + |
| 85 | + `${serverReceivedFIN} FIN received by server, ` + |
| 86 | + `${clientReceivedFIN} FIN received by client, ` + |
| 87 | + `${clientSentFIN} FIN sent by client, ` + |
| 88 | + `${serverConnections} FIN sent by server`); |
| 89 | + })) |
| 90 | + .listen(0, 'localhost', common.mustCall(function serverOnListen() { |
| 91 | + const host = 'localhost'; |
| 92 | + const port = server.address().port; |
| 93 | + |
| 94 | + console.error(`Server starts at ${host}:${port}`); |
| 95 | + const getSocketOpt = () => ({ allowHalfOpen: true }); |
| 96 | + const getConnectOpt = () => ({ host, port }); |
| 97 | + const getConnectCb = (index) => common.mustCall(function clientOnConnect() { |
| 98 | + const client = this; |
| 99 | + console.error(`'connect' emitted on Client ${index}`); |
| 100 | + client.resume(); |
| 101 | + client.on('end', common.mustCall(function clientOnEnd() { |
| 102 | + setTimeout(function() { |
| 103 | + // when allowHalfOpen is true, client must still be writable |
| 104 | + // after the server closes the connections, but not readable |
| 105 | + console.error(`No. ${index} client received FIN`); |
| 106 | + assert(!client.readable); |
| 107 | + assert(client.writable); |
| 108 | + assert(client.write(index + '')); |
| 109 | + client.end(); |
| 110 | + clientSentFIN++; |
| 111 | + console.error(`No. ${index} client sent FIN, ` + |
| 112 | + `${clientSentFIN} have been sent`); |
| 113 | + }, 50); |
| 114 | + })); |
| 115 | + client.on('close', common.mustCall(function clientOnClose() { |
| 116 | + clientReceivedFIN++; |
| 117 | + console.error(`No. ${index} connection has been closed by both ` + |
| 118 | + `sides, ${clientReceivedFIN} clients have closed`); |
| 119 | + })); |
| 120 | + }); |
| 121 | + |
| 122 | + testClients(getSocketOpt, getConnectOpt, getConnectCb); |
| 123 | + })); |
| 124 | +} |
0 commit comments