0
0
mirror of https://github.com/nodejs/node.git synced 2024-11-29 23:16:30 +01:00
nodejs/test/parallel/test-net-server-connections-child-null.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

47 lines
1.0 KiB
JavaScript

'use strict';
const common = require('../common');
const assert = require('assert');
const fork = require('child_process').fork;
const net = require('net');
if (process.argv[2] === 'child') {
process.on('message', (msg, socket) => {
socket.end('goodbye');
});
process.send('hello');
} else {
const child = fork(process.argv[1], ['child']);
const runTest = common.mustCall(() => {
const server = net.createServer();
// server.connections should start as 0
assert.strictEqual(server.connections, 0);
server.on('connection', (socket) => {
child.send({ what: 'socket' }, socket);
});
server.on('close', () => {
child.kill();
});
server.listen(0, common.mustCall(() => {
const connect = net.connect(server.address().port);
connect.on('close', common.mustCall(() => {
// now server.connections should be null
assert.strictEqual(server.connections, null);
server.close();
}));
connect.resume();
}));
});
child.on('message', runTest);
}