2017-01-03 13:16:48 -08:00
|
|
|
// Copyright Joyent, Inc. and other Node contributors.
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
// copy of this software and associated documentation files (the
|
|
|
|
// "Software"), to deal in the Software without restriction, including
|
|
|
|
// without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
// distribute, sublicense, and/or sell copies of the Software, and to permit
|
|
|
|
// persons to whom the Software is furnished to do so, subject to the
|
|
|
|
// following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included
|
|
|
|
// in all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
|
|
|
|
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
|
|
|
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
|
|
|
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
|
|
|
// USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
|
2015-05-19 13:00:06 +02:00
|
|
|
'use strict';
|
2016-05-29 03:06:56 -04:00
|
|
|
require('../common');
|
2016-12-30 18:38:06 -05:00
|
|
|
const assert = require('assert');
|
2010-12-05 01:45:52 +03:00
|
|
|
|
2016-12-30 18:38:06 -05:00
|
|
|
const net = require('net');
|
2009-05-19 21:53:26 +02:00
|
|
|
|
2017-01-08 13:19:00 +00:00
|
|
|
const N = 50;
|
|
|
|
let client_recv_count = 0;
|
|
|
|
let client_end_count = 0;
|
|
|
|
let disconnect_count = 0;
|
2009-06-08 19:10:23 +02:00
|
|
|
|
2017-01-08 13:19:00 +00:00
|
|
|
const server = net.createServer(function(socket) {
|
2012-12-13 09:51:31 -08:00
|
|
|
console.error('SERVER: got socket connection');
|
|
|
|
socket.resume();
|
|
|
|
|
2013-02-28 23:25:29 +01:00
|
|
|
console.error('SERVER connect, writing');
|
|
|
|
socket.write('hello\r\n');
|
2009-05-19 21:53:26 +02:00
|
|
|
|
2020-08-25 11:53:32 +00:00
|
|
|
socket.on('end', () => {
|
2012-12-13 09:51:31 -08:00
|
|
|
console.error('SERVER socket end, calling end()');
|
2010-04-08 10:44:22 -07:00
|
|
|
socket.end();
|
2009-06-27 20:40:43 +02:00
|
|
|
});
|
2009-05-19 21:53:26 +02:00
|
|
|
|
2020-08-25 11:53:32 +00:00
|
|
|
socket.on('close', (had_error) => {
|
2017-04-28 04:06:42 +03:00
|
|
|
console.log(`SERVER had_error: ${JSON.stringify(had_error)}`);
|
2018-10-12 11:38:16 -07:00
|
|
|
assert.strictEqual(had_error, false);
|
2009-06-27 20:40:43 +02:00
|
|
|
});
|
2009-08-26 18:22:00 +02:00
|
|
|
});
|
2010-03-09 16:27:49 -08:00
|
|
|
|
2016-05-29 03:06:56 -04:00
|
|
|
server.listen(0, function() {
|
2012-12-13 09:51:31 -08:00
|
|
|
console.log('SERVER listening');
|
2017-01-08 13:19:00 +00:00
|
|
|
const client = net.createConnection(this.address().port);
|
2009-08-26 18:22:00 +02:00
|
|
|
|
2010-12-05 01:45:52 +03:00
|
|
|
client.setEncoding('UTF8');
|
2009-08-26 18:22:00 +02:00
|
|
|
|
2020-08-25 11:53:32 +00:00
|
|
|
client.on('connect', () => {
|
2012-12-13 09:51:31 -08:00
|
|
|
console.error('CLIENT connected', client._writableState);
|
2010-03-09 16:27:49 -08:00
|
|
|
});
|
2009-08-26 18:22:00 +02:00
|
|
|
|
2011-10-15 01:08:36 +02:00
|
|
|
client.on('data', function(chunk) {
|
2010-03-09 16:27:49 -08:00
|
|
|
client_recv_count += 1;
|
2017-04-28 04:06:42 +03:00
|
|
|
console.log(`client_recv_count ${client_recv_count}`);
|
2018-10-12 11:38:16 -07:00
|
|
|
assert.strictEqual(chunk, 'hello\r\n');
|
2012-12-13 09:51:31 -08:00
|
|
|
console.error('CLIENT: calling end', client._writableState);
|
2010-04-08 10:44:22 -07:00
|
|
|
client.end();
|
2010-03-09 16:27:49 -08:00
|
|
|
});
|
2009-08-26 18:22:00 +02:00
|
|
|
|
2020-08-25 11:53:32 +00:00
|
|
|
client.on('end', () => {
|
2012-12-13 09:51:31 -08:00
|
|
|
console.error('CLIENT end');
|
2014-01-23 01:15:04 +04:00
|
|
|
client_end_count++;
|
2012-12-13 09:51:31 -08:00
|
|
|
});
|
|
|
|
|
2020-08-25 11:53:32 +00:00
|
|
|
client.on('close', (had_error) => {
|
2012-12-13 09:51:31 -08:00
|
|
|
console.log('CLIENT disconnect');
|
2018-10-12 11:38:16 -07:00
|
|
|
assert.strictEqual(had_error, false);
|
2010-03-09 16:27:49 -08:00
|
|
|
if (disconnect_count++ < N)
|
2016-05-29 03:06:56 -04:00
|
|
|
client.connect(server.address().port); // reconnect
|
2010-03-09 16:27:49 -08:00
|
|
|
else
|
|
|
|
server.close();
|
|
|
|
});
|
2009-08-26 18:22:00 +02:00
|
|
|
});
|
2009-06-08 19:10:23 +02:00
|
|
|
|
2020-08-25 11:53:32 +00:00
|
|
|
process.on('exit', () => {
|
2018-10-12 11:38:16 -07:00
|
|
|
assert.strictEqual(disconnect_count, N + 1);
|
|
|
|
assert.strictEqual(client_recv_count, N + 1);
|
|
|
|
assert.strictEqual(client_end_count, N + 1);
|
2009-08-26 18:51:04 +02:00
|
|
|
});
|