Skip to content

Commit 2659573

Browse files
jklepatchaddaleax
authored andcommitted
test: make http(s)-set-timeout-server more similar
Make test-http(s)-set-timeout-server tests more similar and resolve the following issues: * `test-https-set-timeout-server.js` was missing some `assert` statements, including with `http` module * Both files were missing some calls to `common.mustCall()` * Both files were calling `createServer()` in different ways PR-URL: #13822 Refs: #13588 Refs: #13625 Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Alexey Orlenko <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 587c905 commit 2659573

File tree

2 files changed

+111
-85
lines changed

2 files changed

+111
-85
lines changed

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

+43-31
Original file line numberDiff line numberDiff line change
@@ -42,70 +42,76 @@ function run() {
4242
}
4343

4444
test(function serverTimeout(cb) {
45-
const server = http.createServer(function(req, res) {
45+
const server = http.createServer(common.mustCall(function(req, res) {
4646
// just do nothing, we should get a timeout event.
47-
});
48-
server.listen(common.mustCall(function() {
49-
http.get({ port: server.address().port }).on('error', common.mustCall());
5047
}));
51-
const s = server.setTimeout(50, common.mustCall(function(socket) {
52-
socket.destroy();
53-
server.close();
54-
cb();
48+
server.listen(common.mustCall(function() {
49+
const s = server.setTimeout(50, common.mustCall(function(socket) {
50+
socket.destroy();
51+
server.close();
52+
cb();
53+
}));
54+
assert.ok(s instanceof http.Server);
55+
http.get({
56+
port: server.address().port
57+
}).on('error', common.mustCall());
5558
}));
56-
assert.ok(s instanceof http.Server);
5759
});
5860

5961
test(function serverRequestTimeout(cb) {
60-
const server = http.createServer(function(req, res) {
62+
const server = http.createServer(common.mustCall(function(req, res) {
6163
// just do nothing, we should get a timeout event.
6264
const s = req.setTimeout(50, common.mustCall(function(socket) {
6365
socket.destroy();
6466
server.close();
6567
cb();
6668
}));
6769
assert.ok(s instanceof http.IncomingMessage);
68-
});
70+
}));
6971
server.listen(common.mustCall(function() {
70-
const port = server.address().port;
71-
const req = http.request({ port: port, method: 'POST' });
72+
const req = http.request({
73+
port: server.address().port,
74+
method: 'POST'
75+
});
7276
req.on('error', common.mustCall());
7377
req.write('Hello');
7478
// req is in progress
7579
}));
7680
});
7781

7882
test(function serverResponseTimeout(cb) {
79-
const server = http.createServer(function(req, res) {
83+
const server = http.createServer(common.mustCall(function(req, res) {
8084
// just do nothing, we should get a timeout event.
8185
const s = res.setTimeout(50, common.mustCall(function(socket) {
8286
socket.destroy();
8387
server.close();
8488
cb();
8589
}));
8690
assert.ok(s instanceof http.OutgoingMessage);
87-
});
91+
}));
8892
server.listen(common.mustCall(function() {
89-
const port = server.address().port;
90-
http.get({ port: port }).on('error', common.mustCall());
93+
http.get({
94+
port: server.address().port
95+
}).on('error', common.mustCall());
9196
}));
9297
});
9398

9499
test(function serverRequestNotTimeoutAfterEnd(cb) {
95-
const server = http.createServer(function(req, res) {
100+
const server = http.createServer(common.mustCall(function(req, res) {
96101
// just do nothing, we should get a timeout event.
97102
const s = req.setTimeout(50, common.mustNotCall());
98103
assert.ok(s instanceof http.IncomingMessage);
99104
res.on('timeout', common.mustCall());
100-
});
101-
server.on('timeout', function(socket) {
105+
}));
106+
server.on('timeout', common.mustCall(function(socket) {
102107
socket.destroy();
103108
server.close();
104109
cb();
105-
});
110+
}));
106111
server.listen(common.mustCall(function() {
107-
const port = server.address().port;
108-
http.get({ port: port }).on('error', common.mustCall());
112+
http.get({
113+
port: server.address().port
114+
}).on('error', common.mustCall());
109115
}));
110116
});
111117

@@ -124,16 +130,19 @@ test(function serverResponseTimeoutWithPipeline(cb) {
124130
assert.ok(s instanceof http.OutgoingMessage);
125131
if (req.url === '/1') res.end();
126132
});
127-
server.on('timeout', function(socket) {
133+
server.on('timeout', common.mustCall(function(socket) {
128134
if (secReceived) {
129135
socket.destroy();
130136
server.close();
131137
cb();
132138
}
133-
});
139+
}));
134140
server.listen(common.mustCall(function() {
135-
const port = server.address().port;
136-
const c = net.connect({ port: port, allowHalfOpen: true }, function() {
141+
const options = {
142+
port: server.address().port,
143+
allowHalfOpen: true,
144+
};
145+
const c = net.connect(options, function() {
137146
c.write('GET /1 HTTP/1.1\r\nHost: localhost\r\n\r\n');
138147
c.write('GET /2 HTTP/1.1\r\nHost: localhost\r\n\r\n');
139148
c.write('GET /3 HTTP/1.1\r\nHost: localhost\r\n\r\n');
@@ -142,20 +151,23 @@ test(function serverResponseTimeoutWithPipeline(cb) {
142151
});
143152

144153
test(function idleTimeout(cb) {
145-
const server = http.createServer(function(req, res) {
154+
const server = http.createServer(common.mustCall(function(req, res) {
146155
req.on('timeout', common.mustNotCall());
147156
res.on('timeout', common.mustNotCall());
148157
res.end();
149-
});
158+
}));
150159
const s = server.setTimeout(50, common.mustCall(function(socket) {
151160
socket.destroy();
152161
server.close();
153162
cb();
154163
}));
155164
assert.ok(s instanceof http.Server);
156165
server.listen(common.mustCall(function() {
157-
const port = server.address().port;
158-
const c = net.connect({ port: port, allowHalfOpen: true }, function() {
166+
const options = {
167+
port: server.address().port,
168+
allowHalfOpen: true,
169+
};
170+
const c = net.connect(options, function() {
159171
c.write('GET /1 HTTP/1.1\r\nHost: localhost\r\n\r\n');
160172
// Keep-Alive
161173
});

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

+68-54
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ if (!common.hasCrypto) {
3030
const assert = require('assert');
3131
const fs = require('fs');
3232
const https = require('https');
33+
const http = require('http');
3334
const tls = require('tls');
3435

3536
const tests = [];
@@ -56,83 +57,93 @@ function run() {
5657
}
5758

5859
test(function serverTimeout(cb) {
59-
const server = https.createServer(serverOptions, function(req, res) {
60-
// just do nothing, we should get a timeout event.
61-
});
62-
server.listen(0, common.mustCall(function() {
60+
const server = https.createServer(
61+
serverOptions,
62+
common.mustCall(function(req, res) {
63+
// just do nothing, we should get a
64+
// timeout event.
65+
}));
66+
server.listen(common.mustCall(function() {
6367
const s = server.setTimeout(50, common.mustCall(function(socket) {
6468
socket.destroy();
6569
server.close();
6670
cb();
6771
}));
6872
assert.ok(s instanceof https.Server);
6973
https.get({
70-
port: this.address().port,
74+
port: server.address().port,
7175
rejectUnauthorized: false
7276
}).on('error', common.mustCall());
7377
}));
7478
});
7579

7680
test(function serverRequestTimeout(cb) {
77-
function handler(req, res) {
78-
// just do nothing, we should get a timeout event.
79-
req.setTimeout(50, common.mustCall(function() {
80-
req.socket.destroy();
81-
server.close();
82-
cb();
81+
const server = https.createServer(
82+
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+
}));
93+
assert.ok(s instanceof http.IncomingMessage);
8394
}));
84-
}
85-
86-
const server = https.createServer(serverOptions, common.mustCall(handler));
87-
server.listen(0, function() {
95+
server.listen(common.mustCall(function() {
8896
const req = https.request({
89-
port: this.address().port,
97+
port: server.address().port,
9098
method: 'POST',
9199
rejectUnauthorized: false
92100
});
93101
req.on('error', common.mustCall());
94102
req.write('Hello');
95103
// req is in progress
96-
});
104+
}));
97105
});
98106

99107
test(function serverResponseTimeout(cb) {
100-
function handler(req, res) {
101-
// just do nothing, we should get a timeout event.
102-
res.setTimeout(50, common.mustCall(function() {
103-
res.socket.destroy();
104-
server.close();
105-
cb();
108+
const server = https.createServer(
109+
serverOptions,
110+
common.mustCall(function(req, res) {
111+
// just do nothing, we should get a timeout event.
112+
const s = res.setTimeout(50, common.mustCall(function(socket) {
113+
socket.destroy();
114+
server.close();
115+
cb();
116+
}));
117+
assert.ok(s instanceof http.OutgoingMessage);
106118
}));
107-
}
108-
109-
const server = https.createServer(serverOptions, common.mustCall(handler));
110-
server.listen(0, function() {
119+
server.listen(common.mustCall(function() {
111120
https.get({
112-
port: this.address().port,
121+
port: server.address().port,
113122
rejectUnauthorized: false
114123
}).on('error', common.mustCall());
115-
});
124+
}));
116125
});
117126

118127
test(function serverRequestNotTimeoutAfterEnd(cb) {
119-
function handler(req, res) {
120-
// just do nothing, we should get a timeout event.
121-
req.setTimeout(50, common.mustNotCall());
122-
res.on('timeout', common.mustCall());
123-
}
124-
const server = https.createServer(serverOptions, common.mustCall(handler));
125-
server.on('timeout', function(socket) {
128+
const server = https.createServer(
129+
serverOptions,
130+
common.mustCall(function(req, res) {
131+
// just do nothing, we should get a timeout event.
132+
const s = req.setTimeout(50, common.mustNotCall());
133+
assert.ok(s instanceof http.IncomingMessage);
134+
res.on('timeout', common.mustCall());
135+
}));
136+
server.on('timeout', common.mustCall(function(socket) {
126137
socket.destroy();
127138
server.close();
128139
cb();
129-
});
130-
server.listen(0, function() {
140+
}));
141+
server.listen(common.mustCall(function() {
131142
https.get({
132-
port: this.address().port,
143+
port: server.address().port,
133144
rejectUnauthorized: false
134145
}).on('error', common.mustCall());
135-
});
146+
}));
136147
});
137148

138149
test(function serverResponseTimeoutWithPipeline(cb) {
@@ -144,9 +155,10 @@ test(function serverResponseTimeoutWithPipeline(cb) {
144155
const server = https.createServer(serverOptions, function(req, res) {
145156
if (req.url === '/2')
146157
secReceived = true;
147-
res.setTimeout(50, function() {
158+
const s = res.setTimeout(50, function() {
148159
caughtTimeout += req.url;
149160
});
161+
assert.ok(s instanceof http.OutgoingMessage);
150162
if (req.url === '/1') res.end();
151163
});
152164
server.on('timeout', function(socket) {
@@ -156,9 +168,9 @@ test(function serverResponseTimeoutWithPipeline(cb) {
156168
cb();
157169
}
158170
});
159-
server.listen(0, function() {
171+
server.listen(common.mustCall(function() {
160172
const options = {
161-
port: this.address().port,
173+
port: server.address().port,
162174
allowHalfOpen: true,
163175
rejectUnauthorized: false
164176
};
@@ -167,30 +179,32 @@ test(function serverResponseTimeoutWithPipeline(cb) {
167179
c.write('GET /2 HTTP/1.1\r\nHost: localhost\r\n\r\n');
168180
c.write('GET /3 HTTP/1.1\r\nHost: localhost\r\n\r\n');
169181
});
170-
});
182+
}));
171183
});
172184

173185
test(function idleTimeout(cb) {
174-
const server = https.createServer(serverOptions,
175-
common.mustCall(function(req, res) {
176-
req.on('timeout', common.mustNotCall());
177-
res.on('timeout', common.mustNotCall());
178-
res.end();
179-
}));
180-
server.setTimeout(50, common.mustCall(function(socket) {
186+
const server = https.createServer(
187+
serverOptions,
188+
common.mustCall(function(req, res) {
189+
req.on('timeout', common.mustNotCall());
190+
res.on('timeout', common.mustNotCall());
191+
res.end();
192+
}));
193+
const s = server.setTimeout(50, common.mustCall(function(socket) {
181194
socket.destroy();
182195
server.close();
183196
cb();
184197
}));
185-
server.listen(0, function() {
198+
assert.ok(s instanceof https.Server);
199+
server.listen(common.mustCall(function() {
186200
const options = {
187-
port: this.address().port,
201+
port: server.address().port,
188202
allowHalfOpen: true,
189203
rejectUnauthorized: false
190204
};
191205
tls.connect(options, function() {
192206
this.write('GET /1 HTTP/1.1\r\nHost: localhost\r\n\r\n');
193207
// Keep-Alive
194208
});
195-
});
209+
}));
196210
});

0 commit comments

Comments
 (0)