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‎

Lines changed: 43 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -42,70 +42,76 @@ function run(){
4242
}
4343

4444
test(functionserverTimeout(cb){
45-
constserver=http.createServer(function(req,res){
45+
constserver=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-
consts=server.setTimeout(50,common.mustCall(function(socket){
52-
socket.destroy();
53-
server.close();
54-
cb();
48+
server.listen(common.mustCall(function(){
49+
consts=server.setTimeout(50,common.mustCall(function(socket){
50+
socket.destroy();
51+
server.close();
52+
cb();
53+
}));
54+
assert.ok(sinstanceofhttp.Server);
55+
http.get({
56+
port: server.address().port
57+
}).on('error',common.mustCall());
5558
}));
56-
assert.ok(sinstanceofhttp.Server);
5759
});
5860

5961
test(functionserverRequestTimeout(cb){
60-
constserver=http.createServer(function(req,res){
62+
constserver=http.createServer(common.mustCall(function(req,res){
6163
// just do nothing, we should get a timeout event.
6264
consts=req.setTimeout(50,common.mustCall(function(socket){
6365
socket.destroy();
6466
server.close();
6567
cb();
6668
}));
6769
assert.ok(sinstanceofhttp.IncomingMessage);
68-
});
70+
}));
6971
server.listen(common.mustCall(function(){
70-
constport=server.address().port;
71-
constreq=http.request({port: port,method: 'POST'});
72+
constreq=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(functionserverResponseTimeout(cb){
79-
constserver=http.createServer(function(req,res){
83+
constserver=http.createServer(common.mustCall(function(req,res){
8084
// just do nothing, we should get a timeout event.
8185
consts=res.setTimeout(50,common.mustCall(function(socket){
8286
socket.destroy();
8387
server.close();
8488
cb();
8589
}));
8690
assert.ok(sinstanceofhttp.OutgoingMessage);
87-
});
91+
}));
8892
server.listen(common.mustCall(function(){
89-
constport=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(functionserverRequestNotTimeoutAfterEnd(cb){
95-
constserver=http.createServer(function(req,res){
100+
constserver=http.createServer(common.mustCall(function(req,res){
96101
// just do nothing, we should get a timeout event.
97102
consts=req.setTimeout(50,common.mustNotCall());
98103
assert.ok(sinstanceofhttp.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-
constport=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(sinstanceofhttp.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-
constport=server.address().port;
136-
constc=net.connect({port: port,allowHalfOpen: true},function(){
141+
constoptions={
142+
port: server.address().port,
143+
allowHalfOpen: true,
144+
};
145+
constc=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(functionidleTimeout(cb){
145-
constserver=http.createServer(function(req,res){
154+
constserver=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
consts=server.setTimeout(50,common.mustCall(function(socket){
151160
socket.destroy();
152161
server.close();
153162
cb();
154163
}));
155164
assert.ok(sinstanceofhttp.Server);
156165
server.listen(common.mustCall(function(){
157-
constport=server.address().port;
158-
constc=net.connect({port: port,allowHalfOpen: true},function(){
166+
constoptions={
167+
port: server.address().port,
168+
allowHalfOpen: true,
169+
};
170+
constc=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‎

Lines changed: 68 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ if (!common.hasCrypto){
3030
constassert=require('assert');
3131
constfs=require('fs');
3232
consthttps=require('https');
33+
consthttp=require('http');
3334
consttls=require('tls');
3435

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

5859
test(functionserverTimeout(cb){
59-
constserver=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+
constserver=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
consts=server.setTimeout(50,common.mustCall(function(socket){
6468
socket.destroy();
6569
server.close();
6670
cb();
6771
}));
6872
assert.ok(sinstanceofhttps.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(functionserverRequestTimeout(cb){
77-
functionhandler(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+
constserver=https.createServer(
82+
serverOptions,
83+
common.mustCall(function(req,res){
84+
// just do nothing, we should get a
85+
// timeout event.
86+
consts=req.setTimeout(
87+
50,
88+
common.mustCall(function(socket){
89+
socket.destroy();
90+
server.close();
91+
cb();
92+
}));
93+
assert.ok(sinstanceofhttp.IncomingMessage);
8394
}));
84-
}
85-
86-
constserver=https.createServer(serverOptions,common.mustCall(handler));
87-
server.listen(0,function(){
95+
server.listen(common.mustCall(function(){
8896
constreq=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(functionserverResponseTimeout(cb){
100-
functionhandler(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+
constserver=https.createServer(
109+
serverOptions,
110+
common.mustCall(function(req,res){
111+
// just do nothing, we should get a timeout event.
112+
consts=res.setTimeout(50,common.mustCall(function(socket){
113+
socket.destroy();
114+
server.close();
115+
cb();
116+
}));
117+
assert.ok(sinstanceofhttp.OutgoingMessage);
106118
}));
107-
}
108-
109-
constserver=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(functionserverRequestNotTimeoutAfterEnd(cb){
119-
functionhandler(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-
constserver=https.createServer(serverOptions,common.mustCall(handler));
125-
server.on('timeout',function(socket){
128+
constserver=https.createServer(
129+
serverOptions,
130+
common.mustCall(function(req,res){
131+
// just do nothing, we should get a timeout event.
132+
consts=req.setTimeout(50,common.mustNotCall());
133+
assert.ok(sinstanceofhttp.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(functionserverResponseTimeoutWithPipeline(cb){
@@ -144,9 +155,10 @@ test(function serverResponseTimeoutWithPipeline(cb){
144155
constserver=https.createServer(serverOptions,function(req,res){
145156
if(req.url==='/2')
146157
secReceived=true;
147-
res.setTimeout(50,function(){
158+
consts=res.setTimeout(50,function(){
148159
caughtTimeout+=req.url;
149160
});
161+
assert.ok(sinstanceofhttp.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
constoptions={
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(functionidleTimeout(cb){
174-
constserver=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+
constserver=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+
consts=server.setTimeout(50,common.mustCall(function(socket){
181194
socket.destroy();
182195
server.close();
183196
cb();
184197
}));
185-
server.listen(0,function(){
198+
assert.ok(sinstanceofhttps.Server);
199+
server.listen(common.mustCall(function(){
186200
constoptions={
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)