Skip to content

Commit 346f199

Browse files
aqrlnMylesBorins
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 3b3d47c commit 346f199

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
@@ -21,11 +21,11 @@ function run() {
2121
}
2222

2323
test(function serverTimeout(cb) {
24-
const server = http.createServer(common.mustCall(function(req, res) {
24+
const server = http.createServer(common.mustCall((req, res) => {
2525
// just do nothing, we should get a timeout event.
2626
}));
27-
server.listen(common.mustCall(function() {
28-
const s = server.setTimeout(50, common.mustCall(function(socket) {
27+
server.listen(common.mustCall(() => {
28+
const s = server.setTimeout(50, common.mustCall((socket) => {
2929
socket.destroy();
3030
server.close();
3131
cb();
@@ -38,16 +38,16 @@ test(function serverTimeout(cb) {
3838
});
3939

4040
test(function serverRequestTimeout(cb) {
41-
const server = http.createServer(common.mustCall(function(req, res) {
41+
const server = http.createServer(common.mustCall((req, res) => {
4242
// just do nothing, we should get a timeout event.
43-
const s = req.setTimeout(50, common.mustCall(function(socket) {
43+
const s = req.setTimeout(50, common.mustCall((socket) => {
4444
socket.destroy();
4545
server.close();
4646
cb();
4747
}));
4848
assert.ok(s instanceof http.IncomingMessage);
4949
}));
50-
server.listen(common.mustCall(function() {
50+
server.listen(common.mustCall(() => {
5151
const req = http.request({
5252
port: server.address().port,
5353
method: 'POST'
@@ -59,35 +59,35 @@ test(function serverRequestTimeout(cb) {
5959
});
6060

6161
test(function serverResponseTimeout(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 = res.setTimeout(50, common.mustCall(function(socket) {
64+
const s = res.setTimeout(50, common.mustCall((socket) => {
6565
socket.destroy();
6666
server.close();
6767
cb();
6868
}));
6969
assert.ok(s instanceof http.OutgoingMessage);
7070
}));
71-
server.listen(common.mustCall(function() {
71+
server.listen(common.mustCall(() => {
7272
http.get({
7373
port: server.address().port
7474
}).on('error', common.mustCall());
7575
}));
7676
});
7777

7878
test(function serverRequestNotTimeoutAfterEnd(cb) {
79-
const server = http.createServer(common.mustCall(function(req, res) {
79+
const server = http.createServer(common.mustCall((req, res) => {
8080
// just do nothing, we should get a timeout event.
8181
const s = req.setTimeout(50, common.mustNotCall());
8282
assert.ok(s instanceof http.IncomingMessage);
8383
res.on('timeout', common.mustCall());
8484
}));
85-
server.on('timeout', common.mustCall(function(socket) {
85+
server.on('timeout', common.mustCall((socket) => {
8686
socket.destroy();
8787
server.close();
8888
cb();
8989
}));
90-
server.listen(common.mustCall(function() {
90+
server.listen(common.mustCall(() => {
9191
http.get({
9292
port: server.address().port
9393
}).on('error', common.mustCall());
@@ -97,31 +97,31 @@ test(function serverRequestNotTimeoutAfterEnd(cb) {
9797
test(function serverResponseTimeoutWithPipeline(cb) {
9898
let caughtTimeout = '';
9999
let secReceived = false;
100-
process.on('exit', function() {
100+
process.on('exit', () => {
101101
assert.strictEqual(caughtTimeout, '/2');
102102
});
103-
const server = http.createServer(function(req, res) {
103+
const server = http.createServer((req, res) => {
104104
if (req.url === '/2')
105105
secReceived = true;
106-
const s = res.setTimeout(50, function() {
106+
const s = res.setTimeout(50, () => {
107107
caughtTimeout += req.url;
108108
});
109109
assert.ok(s instanceof http.OutgoingMessage);
110110
if (req.url === '/1') res.end();
111111
});
112-
server.on('timeout', common.mustCall(function(socket) {
112+
server.on('timeout', common.mustCall((socket) => {
113113
if (secReceived) {
114114
socket.destroy();
115115
server.close();
116116
cb();
117117
}
118118
}));
119-
server.listen(common.mustCall(function() {
119+
server.listen(common.mustCall(() => {
120120
const options = {
121121
port: server.address().port,
122122
allowHalfOpen: true,
123123
};
124-
const c = net.connect(options, function() {
124+
const c = net.connect(options, () => {
125125
c.write('GET /1 HTTP/1.1\r\nHost: localhost\r\n\r\n');
126126
c.write('GET /2 HTTP/1.1\r\nHost: localhost\r\n\r\n');
127127
c.write('GET /3 HTTP/1.1\r\nHost: localhost\r\n\r\n');
@@ -130,23 +130,23 @@ test(function serverResponseTimeoutWithPipeline(cb) {
130130
});
131131

132132
test(function idleTimeout(cb) {
133-
const server = http.createServer(common.mustCall(function(req, res) {
133+
const server = http.createServer(common.mustCall((req, res) => {
134134
req.on('timeout', common.mustNotCall());
135135
res.on('timeout', common.mustNotCall());
136136
res.end();
137137
}));
138-
const s = server.setTimeout(50, common.mustCall(function(socket) {
138+
const s = server.setTimeout(50, common.mustCall((socket) => {
139139
socket.destroy();
140140
server.close();
141141
cb();
142142
}));
143143
assert.ok(s instanceof http.Server);
144-
server.listen(common.mustCall(function() {
144+
server.listen(common.mustCall(() => {
145145
const options = {
146146
port: server.address().port,
147147
allowHalfOpen: true,
148148
};
149-
const c = net.connect(options, function() {
149+
const c = net.connect(options, () => {
150150
c.write('GET /1 HTTP/1.1\r\nHost: localhost\r\n\r\n');
151151
// Keep-Alive
152152
});

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

+31-38
Original file line numberDiff line numberDiff line change
@@ -23,28 +23,24 @@ const serverOptions = {
2323
function test(fn) {
2424
if (!tests.length)
2525
process.nextTick(run);
26-
tests.push(fn);
26+
tests.push(common.mustCall(fn));
2727
}
2828

2929
function run() {
3030
const fn = tests.shift();
3131
if (fn) {
32-
console.log('# %s', fn.name);
3332
fn(run);
34-
} else {
35-
console.log('ok');
3633
}
3734
}
3835

3936
test(function serverTimeout(cb) {
4037
const server = https.createServer(
4138
serverOptions,
42-
common.mustCall(function(req, res) {
43-
// just do nothing, we should get a
44-
// timeout event.
39+
common.mustCall((req, res) => {
40+
// just do nothing, we should get a timeout event.
4541
}));
46-
server.listen(common.mustCall(function() {
47-
const s = server.setTimeout(50, common.mustCall(function(socket) {
42+
server.listen(common.mustCall(() => {
43+
const s = server.setTimeout(50, common.mustCall((socket) => {
4844
socket.destroy();
4945
server.close();
5046
cb();
@@ -60,19 +56,16 @@ test(function serverTimeout(cb) {
6056
test(function serverRequestTimeout(cb) {
6157
const server = https.createServer(
6258
serverOptions,
63-
common.mustCall(function(req, res) {
64-
// just do nothing, we should get a
65-
// timeout event.
66-
const s = req.setTimeout(
67-
50,
68-
common.mustCall(function(socket) {
69-
socket.destroy();
70-
server.close();
71-
cb();
72-
}));
59+
common.mustCall((req, res) => {
60+
// just do nothing, we should get a timeout event.
61+
const s = req.setTimeout(50, common.mustCall((socket) => {
62+
socket.destroy();
63+
server.close();
64+
cb();
65+
}));
7366
assert.ok(s instanceof http.IncomingMessage);
7467
}));
75-
server.listen(common.mustCall(function() {
68+
server.listen(common.mustCall(() => {
7669
const req = https.request({
7770
port: server.address().port,
7871
method: 'POST',
@@ -87,16 +80,16 @@ test(function serverRequestTimeout(cb) {
8780
test(function serverResponseTimeout(cb) {
8881
const server = https.createServer(
8982
serverOptions,
90-
common.mustCall(function(req, res) {
83+
common.mustCall((req, res) => {
9184
// just do nothing, we should get a timeout event.
92-
const s = res.setTimeout(50, common.mustCall(function(socket) {
85+
const s = res.setTimeout(50, common.mustCall((socket) => {
9386
socket.destroy();
9487
server.close();
9588
cb();
9689
}));
9790
assert.ok(s instanceof http.OutgoingMessage);
9891
}));
99-
server.listen(common.mustCall(function() {
92+
server.listen(common.mustCall(() => {
10093
https.get({
10194
port: server.address().port,
10295
rejectUnauthorized: false
@@ -107,18 +100,18 @@ test(function serverResponseTimeout(cb) {
107100
test(function serverRequestNotTimeoutAfterEnd(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.
112105
const s = req.setTimeout(50, common.mustNotCall());
113106
assert.ok(s instanceof http.IncomingMessage);
114107
res.on('timeout', common.mustCall());
115108
}));
116-
server.on('timeout', common.mustCall(function(socket) {
109+
server.on('timeout', common.mustCall((socket) => {
117110
socket.destroy();
118111
server.close();
119112
cb();
120113
}));
121-
server.listen(common.mustCall(function() {
114+
server.listen(common.mustCall(() => {
122115
https.get({
123116
port: server.address().port,
124117
rejectUnauthorized: false
@@ -129,32 +122,32 @@ test(function serverRequestNotTimeoutAfterEnd(cb) {
129122
test(function serverResponseTimeoutWithPipeline(cb) {
130123
let caughtTimeout = '';
131124
let secReceived = false;
132-
process.on('exit', function() {
125+
process.on('exit', () => {
133126
assert.strictEqual(caughtTimeout, '/2');
134127
});
135-
const server = https.createServer(serverOptions, function(req, res) {
128+
const server = https.createServer(serverOptions, (req, res) => {
136129
if (req.url === '/2')
137130
secReceived = true;
138-
const s = res.setTimeout(50, function() {
131+
const s = res.setTimeout(50, () => {
139132
caughtTimeout += req.url;
140133
});
141134
assert.ok(s instanceof http.OutgoingMessage);
142135
if (req.url === '/1') res.end();
143136
});
144-
server.on('timeout', function(socket) {
137+
server.on('timeout', common.mustCall((socket) => {
145138
if (secReceived) {
146139
socket.destroy();
147140
server.close();
148141
cb();
149142
}
150-
});
151-
server.listen(common.mustCall(function() {
143+
}));
144+
server.listen(common.mustCall(() => {
152145
const options = {
153146
port: server.address().port,
154147
allowHalfOpen: true,
155148
rejectUnauthorized: false
156149
};
157-
const c = tls.connect(options, function() {
150+
const c = tls.connect(options, () => {
158151
c.write('GET /1 HTTP/1.1\r\nHost: localhost\r\n\r\n');
159152
c.write('GET /2 HTTP/1.1\r\nHost: localhost\r\n\r\n');
160153
c.write('GET /3 HTTP/1.1\r\nHost: localhost\r\n\r\n');
@@ -165,25 +158,25 @@ test(function serverResponseTimeoutWithPipeline(cb) {
165158
test(function idleTimeout(cb) {
166159
const server = https.createServer(
167160
serverOptions,
168-
common.mustCall(function(req, res) {
161+
common.mustCall((req, res) => {
169162
req.on('timeout', common.mustNotCall());
170163
res.on('timeout', common.mustNotCall());
171164
res.end();
172165
}));
173-
const s = server.setTimeout(50, common.mustCall(function(socket) {
166+
const s = server.setTimeout(50, common.mustCall((socket) => {
174167
socket.destroy();
175168
server.close();
176169
cb();
177170
}));
178171
assert.ok(s instanceof https.Server);
179-
server.listen(common.mustCall(function() {
172+
server.listen(common.mustCall(() => {
180173
const options = {
181174
port: server.address().port,
182175
allowHalfOpen: true,
183176
rejectUnauthorized: false
184177
};
185-
tls.connect(options, function() {
186-
this.write('GET /1 HTTP/1.1\r\nHost: localhost\r\n\r\n');
178+
const c = tls.connect(options, () => {
179+
c.write('GET /1 HTTP/1.1\r\nHost: localhost\r\n\r\n');
187180
// Keep-Alive
188181
});
189182
}));

0 commit comments

Comments
 (0)