Skip to content

Commit 969e285

Browse files
committed
add debug info to test-net-connect-options
1 parent 8d6ee2c commit 969e285

File tree

1 file changed

+77
-34
lines changed

1 file changed

+77
-34
lines changed

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

+77-34
Original file line numberDiff line numberDiff line change
@@ -49,42 +49,77 @@ const forAllClients = (cb) => common.mustCall(cb, CLIENT_VARIANTS);
4949

5050
// Test allowHalfOpen
5151
{
52-
let counter = 0;
52+
let clientReceivedFIN = 0;
53+
let serverConnections = 0;
54+
let clientSentFIN = 0;
55+
let serverReceivedFIN = 0;
5356
const server = net.createServer({
5457
allowHalfOpen: true
5558
})
56-
.on('connection', forAllClients(function(socket) {
59+
.on('connection', forAllClients(function serverOnConnection(socket) {
60+
const serverConnection = ++serverConnections;
61+
let clientId;
62+
console.error(`${serverConnections} 'connection' emitted on server`);
5763
socket.resume();
5864
// 'end' on each socket must not be emitted twice
59-
socket.on('end', common.mustCall(function() {}, 1));
65+
socket.on('data', common.mustCall(function(data) {
66+
clientId = data.toString();
67+
console.error(`${serverConnection} server connection is started ` +
68+
`by client No. ${clientId}`);
69+
}));
70+
socket.on('end', common.mustCall(function() {
71+
serverReceivedFIN++;
72+
console.error(`Server recieved FIN sent by No. ${clientId}`);
73+
if (serverReceivedFIN === CLIENT_VARIANTS) {
74+
setTimeout(() => {
75+
server.close();
76+
console.error(`No. ${clientId} connection is closing server: ` +
77+
`${serverReceivedFIN} FIN received by server, ` +
78+
`${clientReceivedFIN} FIN received by client, ` +
79+
`${clientSentFIN} FIN sent by client, ` +
80+
`${serverConnections} FIN sent by server`);
81+
}, 50);
82+
}
83+
}, 1));
6084
socket.end();
85+
console.error(`Server has sent ${serverConnections} FIN`);
86+
}))
87+
.on('close', common.mustCall(function serverOnClose() {
88+
console.error('Server has been closed: ' +
89+
`${serverReceivedFIN} FIN received by server, ` +
90+
`${clientReceivedFIN} FIN received by client, ` +
91+
`${clientSentFIN} FIN sent by client, ` +
92+
`${serverConnections} FIN sent by server`);
6193
}))
62-
.listen(0, common.mustCall(function() {
94+
.listen(0, 'localhost', common.mustCall(function serverOnListen() {
95+
const host = 'localhost';
96+
const port = server.address().port;
97+
98+
console.error(`Server starts at ${host}:${port}`);
6399
const getSocketOpt = () => ({ allowHalfOpen: true });
64-
const getConnectOpt = () => ({
65-
host: server.address().address,
66-
port: server.address().port,
67-
});
68-
const getConnectCb = () => common.mustCall(function() {
100+
const getConnectOpt = () => ({ host, port });
101+
const getConnectCb = (index) => common.mustCall(function clientOnConnect() {
69102
const client = this;
103+
console.error(`'connect' emitted on Client ${index}`);
70104
client.resume();
71-
client.on('end', common.mustCall(function() {
105+
client.on('end', common.mustCall(function clientOnEnd() {
72106
setTimeout(function() {
73107
// when allowHalfOpen is true, client must still be writable
74108
// after the server closes the connections, but not readable
109+
console.error(`No. ${index} client received FIN`);
75110
assert(!client.readable);
76111
assert(client.writable);
77-
assert(client.write('foo'));
112+
assert(client.write(index + ''));
78113
client.end();
114+
clientSentFIN++;
115+
console.error(`No. ${index} client sent FIN, ` +
116+
`${clientSentFIN} have been sent`);
79117
}, 50);
80118
}));
81-
client.on('close', common.mustCall(function() {
82-
counter++;
83-
if (counter === CLIENT_VARIANTS) {
84-
setTimeout(() => {
85-
server.close();
86-
}, 50);
87-
}
119+
client.on('close', common.mustCall(function clientOnClose() {
120+
clientReceivedFIN++;
121+
console.error(`No. ${index} connection has been closed by both ` +
122+
`sides, ${clientReceivedFIN} clients have closed`);
88123
}));
89124
});
90125

@@ -96,10 +131,18 @@ const forAllClients = (cb) => common.mustCall(cb, CLIENT_VARIANTS);
96131
if (!common.isWindows) { // Doesn't support this on windows
97132
let counter = 0;
98133
const server = net.createServer()
99-
.on('connection', forAllClients(function(socket) {
134+
.on('connection', forAllClients(function serverOnConnection(socket) {
100135
socket.end('ok');
136+
socket.on('end', common.mustCall(function() {
137+
counter++;
138+
if (counter === CLIENT_VARIANTS) {
139+
setTimeout(() => {
140+
server.close();
141+
}, 50);
142+
}
143+
}, 1));
101144
}))
102-
.listen(0, common.mustCall(function() {
145+
.listen(0, 'localhost', common.mustCall(function serverOnListen() {
103146
const handleMap = new Map();
104147
const getSocketOpt = (index) => {
105148
const handle = new TCP();
@@ -118,11 +161,11 @@ if (!common.isWindows) { // Doesn't support this on windows
118161
};
119162

120163
const getConnectOpt = () => ({
121-
host: server.address().address,
164+
host: 'localhost',
122165
port: server.address().port,
123166
});
124167

125-
const getConnectCb = (index) => common.mustCall(function() {
168+
const getConnectCb = (index) => common.mustCall(function clientOnConnect() {
126169
const client = this;
127170
// Test if it's wrapping an existing fd
128171
assert(handleMap.has(index));
@@ -131,10 +174,6 @@ if (!common.isWindows) { // Doesn't support this on windows
131174
client.end();
132175
client.on('close', common.mustCall(function() {
133176
oldHandle.close();
134-
counter++;
135-
if (counter === CLIENT_VARIANTS) {
136-
server.close();
137-
}
138177
}));
139178
});
140179

@@ -149,10 +188,18 @@ if (!common.isWindows) { // Doesn't support this on windows
149188
let counter = 0;
150189
let socketCounter = 0;
151190
const server = net.createServer()
152-
.on('connection', forAllClients(function(socket) {
191+
.on('connection', forAllClients(function serverOnConnection(socket) {
153192
socket.end('ok');
193+
socket.on('end', common.mustCall(function() {
194+
counter++;
195+
if (counter === CLIENT_VARIANTS) {
196+
setTimeout(() => {
197+
server.close();
198+
}, 50);
199+
}
200+
}, 1));
154201
}))
155-
.listen({path: serverPath}, common.mustCall(function() {
202+
.listen({path: serverPath}, common.mustCall(function serverOnListen() {
156203
const handleMap = new Map();
157204
const getSocketOpt = (index) => {
158205
const handle = new Pipe();
@@ -165,19 +212,15 @@ if (!common.isWindows) { // Doesn't support this on windows
165212
const getConnectOpt = () => ({
166213
path: serverPath
167214
});
168-
const getConnectCb = (index) => common.mustCall(function() {
215+
const getConnectCb = (index) => common.mustCall(function clientOnConnect() {
169216
const client = this;
170217
// Test if it's wrapping an existing fd
171218
assert(handleMap.has(index));
172219
const oldHandle = handleMap.get(index);
173220
assert.strictEqual(oldHandle.fd, this._handle.fd);
174221
client.end();
175-
client.on('close', common.mustCall(function() {
222+
client.on('close', common.mustCall(function clientOnClose() {
176223
oldHandle.close();
177-
counter++;
178-
if (counter === CLIENT_VARIANTS) {
179-
server.close();
180-
}
181224
}));
182225
});
183226

0 commit comments

Comments
 (0)