0
0
mirror of https://github.com/nodejs/node.git synced 2024-11-29 23:16:30 +01:00
nodejs/test/parallel/test-net-connect-options-path.js
Luigi Pinca 9b7a6914a7 net: emit 'close' after 'end'
Currently the writable side of the socket is closed as soon as `UV_EOF`
is read regardless of the state of the socket. This allows the handle
to be closed before `'end'` is emitted and thus `'close'` can be
emitted before `'end'` if the socket is paused.

This commit prevents the handle from being closed until `'end'` is
emitted ensuring the correct order of events.

PR-URL: https://github.com/nodejs/node/pull/19241
Fixes: https://github.com/nodejs/node/issues/19166
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
2018-03-21 18:28:16 +01:00

60 lines
1.8 KiB
JavaScript

'use strict';
const common = require('../common');
const net = require('net');
// This file tests the option handling of net.connect,
// net.createConnect, and new Socket().connect
const tmpdir = require('../common/tmpdir');
tmpdir.refresh();
const CLIENT_VARIANTS = 12;
// Test connect(path)
{
const prefix = `${common.PIPE}-net-connect-options-path`;
const serverPath = `${prefix}-server`;
let counter = 0;
const server = net.createServer()
.on('connection', common.mustCall(function(socket) {
socket.end('ok');
}, CLIENT_VARIANTS))
.listen(serverPath, common.mustCall(function() {
const getConnectCb = () => common.mustCall(function() {
this.end();
this.on('close', common.mustCall(function() {
counter++;
if (counter === CLIENT_VARIANTS) {
server.close();
}
}));
});
// CLIENT_VARIANTS depends on the following code
net.connect(serverPath, getConnectCb()).resume();
net.connect(serverPath)
.on('connect', getConnectCb())
.resume();
net.createConnection(serverPath, getConnectCb()).resume();
net.createConnection(serverPath)
.on('connect', getConnectCb())
.resume();
new net.Socket().connect(serverPath, getConnectCb()).resume();
new net.Socket().connect(serverPath)
.on('connect', getConnectCb())
.resume();
net.connect({ path: serverPath }, getConnectCb()).resume();
net.connect({ path: serverPath })
.on('connect', getConnectCb())
.resume();
net.createConnection({ path: serverPath }, getConnectCb()).resume();
net.createConnection({ path: serverPath })
.on('connect', getConnectCb())
.resume();
new net.Socket().connect({ path: serverPath }, getConnectCb()).resume();
new net.Socket().connect({ path: serverPath })
.on('connect', getConnectCb())
.resume();
}));
}