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'; 23require('../common'); 24const assert = require('assert'); 25 26const net = require('net'); 27 28const N = 50; 29let client_recv_count = 0; 30let client_end_count = 0; 31let disconnect_count = 0; 32 33const server = net.createServer(function(socket) { 34 console.error('SERVER: got socket connection'); 35 socket.resume(); 36 37 console.error('SERVER connect, writing'); 38 socket.write('hello\r\n'); 39 40 socket.on('end', () => { 41 console.error('SERVER socket end, calling end()'); 42 socket.end(); 43 }); 44 45 socket.on('close', (had_error) => { 46 console.log(`SERVER had_error: ${JSON.stringify(had_error)}`); 47 assert.strictEqual(had_error, false); 48 }); 49}); 50 51server.listen(0, function() { 52 console.log('SERVER listening'); 53 const client = net.createConnection(this.address().port); 54 55 client.setEncoding('UTF8'); 56 57 client.on('connect', () => { 58 console.error('CLIENT connected', client._writableState); 59 }); 60 61 client.on('data', function(chunk) { 62 client_recv_count += 1; 63 console.log(`client_recv_count ${client_recv_count}`); 64 assert.strictEqual(chunk, 'hello\r\n'); 65 console.error('CLIENT: calling end', client._writableState); 66 client.end(); 67 }); 68 69 client.on('end', () => { 70 console.error('CLIENT end'); 71 client_end_count++; 72 }); 73 74 client.on('close', (had_error) => { 75 console.log('CLIENT disconnect'); 76 assert.strictEqual(had_error, false); 77 if (disconnect_count++ < N) 78 client.connect(server.address().port); // reconnect 79 else 80 server.close(); 81 }); 82}); 83 84process.on('exit', () => { 85 assert.strictEqual(disconnect_count, N + 1); 86 assert.strictEqual(client_recv_count, N + 1); 87 assert.strictEqual(client_end_count, N + 1); 88}); 89