test: refactor test for readability
PR-URL: https://github.com/nodejs/node/pull/13003 Fixes: https://github.com/nodejs/node/issues/12951 Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
This commit is contained in:
parent
6763fb2f0e
commit
01c680b92a
@ -24,101 +24,95 @@ const common = require('../common');
|
|||||||
const assert = require('assert');
|
const assert = require('assert');
|
||||||
const net = require('net');
|
const net = require('net');
|
||||||
|
|
||||||
function testClients(getSocketOpt, getConnectOpt, getConnectCb) {
|
|
||||||
const cloneOptions = (index) =>
|
|
||||||
Object.assign({}, getSocketOpt(index), getConnectOpt(index));
|
|
||||||
return [
|
|
||||||
net.connect(cloneOptions(0), getConnectCb(0)),
|
|
||||||
net.connect(cloneOptions(1))
|
|
||||||
.on('connect', getConnectCb(1)),
|
|
||||||
net.createConnection(cloneOptions(2), getConnectCb(2)),
|
|
||||||
net.createConnection(cloneOptions(3))
|
|
||||||
.on('connect', getConnectCb(3)),
|
|
||||||
new net.Socket(getSocketOpt(4)).connect(getConnectOpt(4), getConnectCb(4)),
|
|
||||||
new net.Socket(getSocketOpt(5)).connect(getConnectOpt(5))
|
|
||||||
.on('connect', getConnectCb(5))
|
|
||||||
];
|
|
||||||
}
|
|
||||||
|
|
||||||
const CLIENT_VARIANTS = 6; // Same length as array above
|
|
||||||
const forAllClients = (cb) => common.mustCall(cb, CLIENT_VARIANTS);
|
|
||||||
|
|
||||||
// Test allowHalfOpen
|
// Test allowHalfOpen
|
||||||
{
|
{
|
||||||
let clientReceivedFIN = 0;
|
let clientReceivedFIN = 0;
|
||||||
let serverConnections = 0;
|
let serverConnections = 0;
|
||||||
let clientSentFIN = 0;
|
let clientSentFIN = 0;
|
||||||
let serverReceivedFIN = 0;
|
let serverReceivedFIN = 0;
|
||||||
const server = net.createServer({
|
const host = common.localhostIPv4;
|
||||||
allowHalfOpen: true
|
|
||||||
})
|
function serverOnConnection(socket) {
|
||||||
.on('connection', forAllClients(function serverOnConnection(socket) {
|
console.log(`'connection' ${++serverConnections} emitted on server`);
|
||||||
const serverConnection = ++serverConnections;
|
const srvConn = serverConnections;
|
||||||
let clientId;
|
|
||||||
console.error(`${serverConnections} 'connection' emitted on server`);
|
|
||||||
socket.resume();
|
socket.resume();
|
||||||
// 'end' on each socket must not be emitted twice
|
socket.on('data', common.mustCall(function socketOnData(data) {
|
||||||
socket.on('data', common.mustCall(function(data) {
|
this.clientId = data.toString();
|
||||||
clientId = data.toString();
|
console.log(
|
||||||
console.error(`${serverConnection} server connection is started ` +
|
`server connection ${srvConn} is started by client ${this.clientId}`);
|
||||||
`by client No. ${clientId}`);
|
|
||||||
}));
|
}));
|
||||||
socket.on('end', common.mustCall(function() {
|
// 'end' on each socket must not be emitted twice
|
||||||
serverReceivedFIN++;
|
socket.on('end', common.mustCall(function socketOnEnd() {
|
||||||
console.error(`Server received FIN sent by No. ${clientId}`);
|
console.log(`Server received FIN sent by client ${this.clientId}`);
|
||||||
if (serverReceivedFIN === CLIENT_VARIANTS) {
|
if (++serverReceivedFIN < CLIENT_VARIANTS) return;
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
server.close();
|
server.close();
|
||||||
console.error(`No. ${clientId} connection is closing server: ` +
|
console.log(`connection ${this.clientId} is closing the server:
|
||||||
`${serverReceivedFIN} FIN received by server, ` +
|
FIN ${serverReceivedFIN} received by server,
|
||||||
`${clientReceivedFIN} FIN received by client, ` +
|
FIN ${clientReceivedFIN} received by client
|
||||||
`${clientSentFIN} FIN sent by client, ` +
|
FIN ${clientSentFIN} sent by client,
|
||||||
`${serverConnections} FIN sent by server`);
|
FIN ${serverConnections} sent by server`.replace(/ {3,}/g, ''));
|
||||||
}, 50);
|
}, 50);
|
||||||
}
|
|
||||||
}, 1));
|
}, 1));
|
||||||
socket.end();
|
socket.end();
|
||||||
console.error(`Server has sent ${serverConnections} FIN`);
|
console.log(`Server has sent ${serverConnections} FIN`);
|
||||||
}))
|
}
|
||||||
.on('close', common.mustCall(function serverOnClose() {
|
|
||||||
console.error('Server has been closed: ' +
|
|
||||||
`${serverReceivedFIN} FIN received by server, ` +
|
|
||||||
`${clientReceivedFIN} FIN received by client, ` +
|
|
||||||
`${clientSentFIN} FIN sent by client, ` +
|
|
||||||
`${serverConnections} FIN sent by server`);
|
|
||||||
}))
|
|
||||||
.listen(0, 'localhost', common.mustCall(function serverOnListen() {
|
|
||||||
const host = 'localhost';
|
|
||||||
const port = server.address().port;
|
|
||||||
|
|
||||||
console.error(`Server starts at ${host}:${port}`);
|
// These two levels of functions (and not arrows) are necessary in order to
|
||||||
const getSocketOpt = () => ({ allowHalfOpen: true });
|
// bind the `index`, and the calling socket (`this`)
|
||||||
const getConnectOpt = () => ({ host, port });
|
function clientOnConnect(index) {
|
||||||
const getConnectCb = (index) => common.mustCall(function clientOnConnect() {
|
return common.mustCall(function clientOnConnectInner() {
|
||||||
const client = this;
|
const client = this;
|
||||||
console.error(`'connect' emitted on Client ${index}`);
|
console.log(`'connect' emitted on Client ${index}`);
|
||||||
client.resume();
|
client.resume();
|
||||||
client.on('end', common.mustCall(function clientOnEnd() {
|
client.on('end', common.mustCall(function clientOnEnd() {
|
||||||
setTimeout(function() {
|
setTimeout(function closeServer() {
|
||||||
// when allowHalfOpen is true, client must still be writable
|
// when allowHalfOpen is true, client must still be writable
|
||||||
// after the server closes the connections, but not readable
|
// after the server closes the connections, but not readable
|
||||||
console.error(`No. ${index} client received FIN`);
|
console.log(`client ${index} received FIN`);
|
||||||
assert(!client.readable);
|
assert(!client.readable);
|
||||||
assert(client.writable);
|
assert(client.writable);
|
||||||
assert(client.write(String(index)));
|
assert(client.write(String(index)));
|
||||||
client.end();
|
client.end();
|
||||||
clientSentFIN++;
|
clientSentFIN++;
|
||||||
console.error(`No. ${index} client sent FIN, ` +
|
console.log(
|
||||||
`${clientSentFIN} have been sent`);
|
`client ${index} sent FIN, ${clientSentFIN} have been sent`);
|
||||||
}, 50);
|
}, 50);
|
||||||
}));
|
}));
|
||||||
client.on('close', common.mustCall(function clientOnClose() {
|
client.on('close', common.mustCall(function clientOnClose() {
|
||||||
clientReceivedFIN++;
|
clientReceivedFIN++;
|
||||||
console.error(`No. ${index} connection has been closed by both ` +
|
console.log(`connection ${index} has been closed by both sides,` +
|
||||||
`sides, ${clientReceivedFIN} clients have closed`);
|
` ${clientReceivedFIN} clients have closed`);
|
||||||
}));
|
}));
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
testClients(getSocketOpt, getConnectOpt, getConnectCb);
|
function serverOnClose() {
|
||||||
}));
|
console.log(`Server has been closed:
|
||||||
|
FIN ${serverReceivedFIN} received by server
|
||||||
|
FIN ${clientReceivedFIN} received by client
|
||||||
|
FIN ${clientSentFIN} sent by client
|
||||||
|
FIN ${serverConnections} sent by server`.replace(/ {3,}/g, ''));
|
||||||
|
}
|
||||||
|
|
||||||
|
function serverOnListen() {
|
||||||
|
const port = server.address().port;
|
||||||
|
console.log(`Server started listening at ${host}:${port}`);
|
||||||
|
const opts = { allowHalfOpen: true, host, port };
|
||||||
|
// 6 variations === CLIENT_VARIANTS
|
||||||
|
net.connect(opts, clientOnConnect(1));
|
||||||
|
net.connect(opts).on('connect', clientOnConnect(2));
|
||||||
|
net.createConnection(opts, clientOnConnect(3));
|
||||||
|
net.createConnection(opts).on('connect', clientOnConnect(4));
|
||||||
|
new net.Socket(opts).connect(opts, clientOnConnect(5));
|
||||||
|
new net.Socket(opts).connect(opts).on('connect', clientOnConnect(6));
|
||||||
|
}
|
||||||
|
|
||||||
|
const CLIENT_VARIANTS = 6;
|
||||||
|
|
||||||
|
// The trigger
|
||||||
|
const server = net.createServer({ allowHalfOpen: true })
|
||||||
|
.on('connection', common.mustCall(serverOnConnection, CLIENT_VARIANTS))
|
||||||
|
.on('close', common.mustCall(serverOnClose))
|
||||||
|
.listen(0, host, common.mustCall(serverOnListen));
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user