Skip to content

Commit 534ae44

Browse files
aqrlnaddaleax
authored andcommitted
test: refactor test-http(s)-set-timeout-server
* Make changes to `test-https-set-timeout-server` to resolve inconsistencies with its http counterpart: - Apply the changes analogous to those in GH-13802 to the https test. - Add a missing `common.mustCall()` wrapper. - Make small stylistic changes (e.g., remove unnecessary line breaks in comments) to make it visually consistent with the http test. * Use arrow functions. PR-URL: #13935 Fixes: #13588 Refs: #13802 Refs: #13625 Refs: #13822 Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Refael Ackermann <[email protected]> Reviewed-By: Yuta Hiroto <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent c652845 commit 534ae44

File tree

2 files changed

+53
-60
lines changed

2 files changed

+53
-60
lines changed

test/parallel/test-http-set-timeout-server.js

+22-22
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@ function run() {
4242
}
4343

4444
test(function serverTimeout(cb) {
45-
const server = http.createServer(common.mustCall(function(req, res) {
45+
const server = http.createServer(common.mustCall((req, res) => {
4646
// just do nothing, we should get a timeout event.
4747
}));
48-
server.listen(common.mustCall(function() {
49-
const s = server.setTimeout(50, common.mustCall(function(socket) {
48+
server.listen(common.mustCall(() => {
49+
const s = server.setTimeout(50, common.mustCall((socket) => {
5050
socket.destroy();
5151
server.close();
5252
cb();
@@ -59,16 +59,16 @@ test(function serverTimeout(cb) {
5959
});
6060

6161
test(function serverRequestTimeout(cb) {
62-
const server = http.createServer(common.mustCall(function(req, res) {
62+
const server = http.createServer(common.mustCall((req, res) => {
6363
// just do nothing, we should get a timeout event.
64-
const s = req.setTimeout(50, common.mustCall(function(socket) {
64+
const s = req.setTimeout(50, common.mustCall((socket) => {
6565
socket.destroy();
6666
server.close();
6767
cb();
6868
}));
6969
assert.ok(s instanceof http.IncomingMessage);
7070
}));
71-
server.listen(common.mustCall(function() {
71+
server.listen(common.mustCall(() => {
7272
const req = http.request({
7373
port: server.address().port,
7474
method: 'POST'
@@ -80,35 +80,35 @@ test(function serverRequestTimeout(cb) {
8080
});
8181

8282
test(function serverResponseTimeout(cb) {
83-
const server = http.createServer(common.mustCall(function(req, res) {
83+
const server = http.createServer(common.mustCall((req, res) => {
8484
// just do nothing, we should get a timeout event.
85-
const s = res.setTimeout(50, common.mustCall(function(socket) {
85+
const s = res.setTimeout(50, common.mustCall((socket) => {
8686
socket.destroy();
8787
server.close();
8888
cb();
8989
}));
9090
assert.ok(s instanceof http.OutgoingMessage);
9191
}));
92-
server.listen(common.mustCall(function() {
92+
server.listen(common.mustCall(() => {
9393
http.get({
9494
port: server.address().port
9595
}).on('error', common.mustCall());
9696
}));
9797
});
9898

9999
test(function serverRequestNotTimeoutAfterEnd(cb) {
100-
const server = http.createServer(common.mustCall(function(req, res) {
100+
const server = http.createServer(common.mustCall((req, res) => {
101101
// just do nothing, we should get a timeout event.
102102
const s = req.setTimeout(50, common.mustNotCall());
103103
assert.ok(s instanceof http.IncomingMessage);
104104
res.on('timeout', common.mustCall());
105105
}));
106-
server.on('timeout', common.mustCall(function(socket) {
106+
server.on('timeout', common.mustCall((socket) => {
107107
socket.destroy();
108108
server.close();
109109
cb();
110110
}));
111-
server.listen(common.mustCall(function() {
111+
server.listen(common.mustCall(() => {
112112
http.get({
113113
port: server.address().port
114114
}).on('error', common.mustCall());
@@ -118,31 +118,31 @@ test(function serverRequestNotTimeoutAfterEnd(cb) {
118118
test(function serverResponseTimeoutWithPipeline(cb) {
119119
let caughtTimeout = '';
120120
let secReceived = false;
121-
process.on('exit', function() {
121+
process.on('exit', () => {
122122
assert.strictEqual(caughtTimeout, '/2');
123123
});
124-
const server = http.createServer(function(req, res) {
124+
const server = http.createServer((req, res) => {
125125
if (req.url === '/2')
126126
secReceived = true;
127-
const s = res.setTimeout(50, function() {
127+
const s = res.setTimeout(50, () => {
128128
caughtTimeout += req.url;
129129
});
130130
assert.ok(s instanceof http.OutgoingMessage);
131131
if (req.url === '/1') res.end();
132132
});
133-
server.on('timeout', common.mustCall(function(socket) {
133+
server.on('timeout', common.mustCall((socket) => {
134134
if (secReceived) {
135135
socket.destroy();
136136
server.close();
137137
cb();
138138
}
139139
}));
140-
server.listen(common.mustCall(function() {
140+
server.listen(common.mustCall(() => {
141141
const options = {
142142
port: server.address().port,
143143
allowHalfOpen: true,
144144
};
145-
const c = net.connect(options, function() {
145+
const c = net.connect(options, () => {
146146
c.write('GET /1 HTTP/1.1\r\nHost: localhost\r\n\r\n');
147147
c.write('GET /2 HTTP/1.1\r\nHost: localhost\r\n\r\n');
148148
c.write('GET /3 HTTP/1.1\r\nHost: localhost\r\n\r\n');
@@ -151,23 +151,23 @@ test(function serverResponseTimeoutWithPipeline(cb) {
151151
});
152152

153153
test(function idleTimeout(cb) {
154-
const server = http.createServer(common.mustCall(function(req, res) {
154+
const server = http.createServer(common.mustCall((req, res) => {
155155
req.on('timeout', common.mustNotCall());
156156
res.on('timeout', common.mustNotCall());
157157
res.end();
158158
}));
159-
const s = server.setTimeout(50, common.mustCall(function(socket) {
159+
const s = server.setTimeout(50, common.mustCall((socket) => {
160160
socket.destroy();
161161
server.close();
162162
cb();
163163
}));
164164
assert.ok(s instanceof http.Server);
165-
server.listen(common.mustCall(function() {
165+
server.listen(common.mustCall(() => {
166166
const options = {
167167
port: server.address().port,
168168
allowHalfOpen: true,
169169
};
170-
const c = net.connect(options, function() {
170+
const c = net.connect(options, () => {
171171
c.write('GET /1 HTTP/1.1\r\nHost: localhost\r\n\r\n');
172172
// Keep-Alive
173173
});

test/sequential/test-https-set-timeout-server.js

+31-38
Original file line numberDiff line numberDiff line change
@@ -43,28 +43,24 @@ const serverOptions = {
4343
function test(fn) {
4444
if (!tests.length)
4545
process.nextTick(run);
46-
tests.push(fn);
46+
tests.push(common.mustCall(fn));
4747
}
4848

4949
function run() {
5050
const fn = tests.shift();
5151
if (fn) {
52-
console.log('# %s', fn.name);
5352
fn(run);
54-
} else {
55-
console.log('ok');
5653
}
5754
}
5855

5956
test(function serverTimeout(cb) {
6057
const server = https.createServer(
6158
serverOptions,
62-
common.mustCall(function(req, res) {
63-
// just do nothing, we should get a
64-
// timeout event.
59+
common.mustCall((req, res) => {
60+
// just do nothing, we should get a timeout event.
6561
}));
66-
server.listen(common.mustCall(function() {
67-
const s = server.setTimeout(50, common.mustCall(function(socket) {
62+
server.listen(common.mustCall(() => {
63+
const s = server.setTimeout(50, common.mustCall((socket) => {
6864
socket.destroy();
6965
server.close();
7066
cb();
@@ -80,19 +76,16 @@ test(function serverTimeout(cb) {
8076
test(function serverRequestTimeout(cb) {
8177
const server = https.createServer(
8278
serverOptions,
83-
common.mustCall(function(req, res) {
84-
// just do nothing, we should get a
85-
// timeout event.
86-
const s = req.setTimeout(
87-
50,
88-
common.mustCall(function(socket) {
89-
socket.destroy();
90-
server.close();
91-
cb();
92-
}));
79+
common.mustCall((req, res) => {
80+
// just do nothing, we should get a timeout event.
81+
const s = req.setTimeout(50, common.mustCall((socket) => {
82+
socket.destroy();
83+
server.close();
84+
cb();
85+
}));
9386
assert.ok(s instanceof http.IncomingMessage);
9487
}));
95-
server.listen(common.mustCall(function() {
88+
server.listen(common.mustCall(() => {
9689
const req = https.request({
9790
port: server.address().port,
9891
method: 'POST',
@@ -107,16 +100,16 @@ test(function serverRequestTimeout(cb) {
107100
test(function serverResponseTimeout(cb) {
108101
const server = https.createServer(
109102
serverOptions,
110-
common.mustCall(function(req, res) {
103+
common.mustCall((req, res) => {
111104
// just do nothing, we should get a timeout event.
112-
const s = res.setTimeout(50, common.mustCall(function(socket) {
105+
const s = res.setTimeout(50, common.mustCall((socket) => {
113106
socket.destroy();
114107
server.close();
115108
cb();
116109
}));
117110
assert.ok(s instanceof http.OutgoingMessage);
118111
}));
119-
server.listen(common.mustCall(function() {
112+
server.listen(common.mustCall(() => {
120113
https.get({
121114
port: server.address().port,
122115
rejectUnauthorized: false
@@ -127,18 +120,18 @@ test(function serverResponseTimeout(cb) {
127120
test(function serverRequestNotTimeoutAfterEnd(cb) {
128121
const server = https.createServer(
129122
serverOptions,
130-
common.mustCall(function(req, res) {
123+
common.mustCall((req, res) => {
131124
// just do nothing, we should get a timeout event.
132125
const s = req.setTimeout(50, common.mustNotCall());
133126
assert.ok(s instanceof http.IncomingMessage);
134127
res.on('timeout', common.mustCall());
135128
}));
136-
server.on('timeout', common.mustCall(function(socket) {
129+
server.on('timeout', common.mustCall((socket) => {
137130
socket.destroy();
138131
server.close();
139132
cb();
140133
}));
141-
server.listen(common.mustCall(function() {
134+
server.listen(common.mustCall(() => {
142135
https.get({
143136
port: server.address().port,
144137
rejectUnauthorized: false
@@ -149,32 +142,32 @@ test(function serverRequestNotTimeoutAfterEnd(cb) {
149142
test(function serverResponseTimeoutWithPipeline(cb) {
150143
let caughtTimeout = '';
151144
let secReceived = false;
152-
process.on('exit', function() {
145+
process.on('exit', () => {
153146
assert.strictEqual(caughtTimeout, '/2');
154147
});
155-
const server = https.createServer(serverOptions, function(req, res) {
148+
const server = https.createServer(serverOptions, (req, res) => {
156149
if (req.url === '/2')
157150
secReceived = true;
158-
const s = res.setTimeout(50, function() {
151+
const s = res.setTimeout(50, () => {
159152
caughtTimeout += req.url;
160153
});
161154
assert.ok(s instanceof http.OutgoingMessage);
162155
if (req.url === '/1') res.end();
163156
});
164-
server.on('timeout', function(socket) {
157+
server.on('timeout', common.mustCall((socket) => {
165158
if (secReceived) {
166159
socket.destroy();
167160
server.close();
168161
cb();
169162
}
170-
});
171-
server.listen(common.mustCall(function() {
163+
}));
164+
server.listen(common.mustCall(() => {
172165
const options = {
173166
port: server.address().port,
174167
allowHalfOpen: true,
175168
rejectUnauthorized: false
176169
};
177-
const c = tls.connect(options, function() {
170+
const c = tls.connect(options, () => {
178171
c.write('GET /1 HTTP/1.1\r\nHost: localhost\r\n\r\n');
179172
c.write('GET /2 HTTP/1.1\r\nHost: localhost\r\n\r\n');
180173
c.write('GET /3 HTTP/1.1\r\nHost: localhost\r\n\r\n');
@@ -185,25 +178,25 @@ test(function serverResponseTimeoutWithPipeline(cb) {
185178
test(function idleTimeout(cb) {
186179
const server = https.createServer(
187180
serverOptions,
188-
common.mustCall(function(req, res) {
181+
common.mustCall((req, res) => {
189182
req.on('timeout', common.mustNotCall());
190183
res.on('timeout', common.mustNotCall());
191184
res.end();
192185
}));
193-
const s = server.setTimeout(50, common.mustCall(function(socket) {
186+
const s = server.setTimeout(50, common.mustCall((socket) => {
194187
socket.destroy();
195188
server.close();
196189
cb();
197190
}));
198191
assert.ok(s instanceof https.Server);
199-
server.listen(common.mustCall(function() {
192+
server.listen(common.mustCall(() => {
200193
const options = {
201194
port: server.address().port,
202195
allowHalfOpen: true,
203196
rejectUnauthorized: false
204197
};
205-
tls.connect(options, function() {
206-
this.write('GET /1 HTTP/1.1\r\nHost: localhost\r\n\r\n');
198+
const c = tls.connect(options, () => {
199+
c.write('GET /1 HTTP/1.1\r\nHost: localhost\r\n\r\n');
207200
// Keep-Alive
208201
});
209202
}));

0 commit comments

Comments
 (0)