Skip to content

Commit 01c680b

Browse files
committed
test: refactor test for readability
PR-URL: #13003 Fixes: #12951 Reviewed-By: Matteo Collina <[email protected]>
1 parent 6763fb2 commit 01c680b

File tree

1 file changed

+63
-69
lines changed

1 file changed

+63
-69
lines changed

test/parallel/test-net-connect-options-allowhalfopen.js

+63-69
Original file line numberDiff line numberDiff line change
@@ -24,101 +24,95 @@ const common = require('../common');
2424
const assert = require('assert');
2525
const net = require('net');
2626

27-
function testClients(getSocketOpt, getConnectOpt, getConnectCb) {
28-
const cloneOptions = (index) =>
29-
Object.assign({}, getSocketOpt(index), getConnectOpt(index));
30-
return [
31-
net.connect(cloneOptions(0), getConnectCb(0)),
32-
net.connect(cloneOptions(1))
33-
.on('connect', getConnectCb(1)),
34-
net.createConnection(cloneOptions(2), getConnectCb(2)),
35-
net.createConnection(cloneOptions(3))
36-
.on('connect', getConnectCb(3)),
37-
new net.Socket(getSocketOpt(4)).connect(getConnectOpt(4), getConnectCb(4)),
38-
new net.Socket(getSocketOpt(5)).connect(getConnectOpt(5))
39-
.on('connect', getConnectCb(5))
40-
];
41-
}
42-
43-
const CLIENT_VARIANTS = 6; // Same length as array above
44-
const forAllClients = (cb) => common.mustCall(cb, CLIENT_VARIANTS);
45-
4627
// Test allowHalfOpen
4728
{
4829
let clientReceivedFIN = 0;
4930
let serverConnections = 0;
5031
let clientSentFIN = 0;
5132
let serverReceivedFIN = 0;
52-
const server = net.createServer({
53-
allowHalfOpen: true
54-
})
55-
.on('connection', forAllClients(function serverOnConnection(socket) {
56-
const serverConnection = ++serverConnections;
57-
let clientId;
58-
console.error(`${serverConnections} 'connection' emitted on server`);
33+
const host = common.localhostIPv4;
34+
35+
function serverOnConnection(socket) {
36+
console.log(`'connection' ${++serverConnections} emitted on server`);
37+
const srvConn = serverConnections;
5938
socket.resume();
60-
// 'end' on each socket must not be emitted twice
61-
socket.on('data', common.mustCall(function(data) {
62-
clientId = data.toString();
63-
console.error(`${serverConnection} server connection is started ` +
64-
`by client No. ${clientId}`);
39+
socket.on('data', common.mustCall(function socketOnData(data) {
40+
this.clientId = data.toString();
41+
console.log(
42+
`server connection ${srvConn} is started by client ${this.clientId}`);
6543
}));
66-
socket.on('end', common.mustCall(function() {
67-
serverReceivedFIN++;
68-
console.error(`Server received FIN sent by No. ${clientId}`);
69-
if (serverReceivedFIN === CLIENT_VARIANTS) {
70-
setTimeout(() => {
71-
server.close();
72-
console.error(`No. ${clientId} connection is closing server: ` +
73-
`${serverReceivedFIN} FIN received by server, ` +
74-
`${clientReceivedFIN} FIN received by client, ` +
75-
`${clientSentFIN} FIN sent by client, ` +
76-
`${serverConnections} FIN sent by server`);
77-
}, 50);
78-
}
44+
// 'end' on each socket must not be emitted twice
45+
socket.on('end', common.mustCall(function socketOnEnd() {
46+
console.log(`Server received FIN sent by client ${this.clientId}`);
47+
if (++serverReceivedFIN < CLIENT_VARIANTS) return;
48+
setTimeout(() => {
49+
server.close();
50+
console.log(`connection ${this.clientId} is closing the server:
51+
FIN ${serverReceivedFIN} received by server,
52+
FIN ${clientReceivedFIN} received by client
53+
FIN ${clientSentFIN} sent by client,
54+
FIN ${serverConnections} sent by server`.replace(/ {3,}/g, ''));
55+
}, 50);
7956
}, 1));
8057
socket.end();
81-
console.error(`Server has sent ${serverConnections} FIN`);
82-
}))
83-
.on('close', common.mustCall(function serverOnClose() {
84-
console.error('Server has been closed: ' +
85-
`${serverReceivedFIN} FIN received by server, ` +
86-
`${clientReceivedFIN} FIN received by client, ` +
87-
`${clientSentFIN} FIN sent by client, ` +
88-
`${serverConnections} FIN sent by server`);
89-
}))
90-
.listen(0, 'localhost', common.mustCall(function serverOnListen() {
91-
const host = 'localhost';
92-
const port = server.address().port;
58+
console.log(`Server has sent ${serverConnections} FIN`);
59+
}
9360

94-
console.error(`Server starts at ${host}:${port}`);
95-
const getSocketOpt = () => ({ allowHalfOpen: true });
96-
const getConnectOpt = () => ({ host, port });
97-
const getConnectCb = (index) => common.mustCall(function clientOnConnect() {
61+
// These two levels of functions (and not arrows) are necessary in order to
62+
// bind the `index`, and the calling socket (`this`)
63+
function clientOnConnect(index) {
64+
return common.mustCall(function clientOnConnectInner() {
9865
const client = this;
99-
console.error(`'connect' emitted on Client ${index}`);
66+
console.log(`'connect' emitted on Client ${index}`);
10067
client.resume();
10168
client.on('end', common.mustCall(function clientOnEnd() {
102-
setTimeout(function() {
69+
setTimeout(function closeServer() {
10370
// when allowHalfOpen is true, client must still be writable
10471
// after the server closes the connections, but not readable
105-
console.error(`No. ${index} client received FIN`);
72+
console.log(`client ${index} received FIN`);
10673
assert(!client.readable);
10774
assert(client.writable);
10875
assert(client.write(String(index)));
10976
client.end();
11077
clientSentFIN++;
111-
console.error(`No. ${index} client sent FIN, ` +
112-
`${clientSentFIN} have been sent`);
78+
console.log(
79+
`client ${index} sent FIN, ${clientSentFIN} have been sent`);
11380
}, 50);
11481
}));
11582
client.on('close', common.mustCall(function clientOnClose() {
11683
clientReceivedFIN++;
117-
console.error(`No. ${index} connection has been closed by both ` +
118-
`sides, ${clientReceivedFIN} clients have closed`);
84+
console.log(`connection ${index} has been closed by both sides,` +
85+
` ${clientReceivedFIN} clients have closed`);
11986
}));
12087
});
88+
}
89+
90+
function serverOnClose() {
91+
console.log(`Server has been closed:
92+
FIN ${serverReceivedFIN} received by server
93+
FIN ${clientReceivedFIN} received by client
94+
FIN ${clientSentFIN} sent by client
95+
FIN ${serverConnections} sent by server`.replace(/ {3,}/g, ''));
96+
}
97+
98+
function serverOnListen() {
99+
const port = server.address().port;
100+
console.log(`Server started listening at ${host}:${port}`);
101+
const opts = { allowHalfOpen: true, host, port };
102+
// 6 variations === CLIENT_VARIANTS
103+
net.connect(opts, clientOnConnect(1));
104+
net.connect(opts).on('connect', clientOnConnect(2));
105+
net.createConnection(opts, clientOnConnect(3));
106+
net.createConnection(opts).on('connect', clientOnConnect(4));
107+
new net.Socket(opts).connect(opts, clientOnConnect(5));
108+
new net.Socket(opts).connect(opts).on('connect', clientOnConnect(6));
109+
}
110+
111+
const CLIENT_VARIANTS = 6;
121112

122-
testClients(getSocketOpt, getConnectOpt, getConnectCb);
123-
}));
113+
// The trigger
114+
const server = net.createServer({ allowHalfOpen: true })
115+
.on('connection', common.mustCall(serverOnConnection, CLIENT_VARIANTS))
116+
.on('close', common.mustCall(serverOnClose))
117+
.listen(0, host, common.mustCall(serverOnListen));
124118
}

0 commit comments

Comments
 (0)