Skip to content

Commit fd6bbc0

Browse files
jklepatchMylesBorins
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 a04d4ea commit fd6bbc0

File tree

2 files changed

+114
-87
lines changed

2 files changed

+114
-87
lines changed

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

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

2323
test(functionserverTimeout(cb){
24-
constserver=http.createServer(function(req,res){
24+
constserver=http.createServer(common.mustCall(function(req,res){
2525
// just do nothing, we should get a timeout event.
26-
});
27-
server.listen(common.mustCall(function(){
28-
http.get({port: server.address().port}).on('error',common.mustCall());
2926
}));
30-
consts=server.setTimeout(50,common.mustCall(function(socket){
31-
socket.destroy();
32-
server.close();
33-
cb();
27+
server.listen(common.mustCall(function(){
28+
consts=server.setTimeout(50,common.mustCall(function(socket){
29+
socket.destroy();
30+
server.close();
31+
cb();
32+
}));
33+
assert.ok(sinstanceofhttp.Server);
34+
http.get({
35+
port: server.address().port
36+
}).on('error',common.mustCall());
3437
}));
35-
assert.ok(sinstanceofhttp.Server);
3638
});
3739

3840
test(functionserverRequestTimeout(cb){
39-
constserver=http.createServer(function(req,res){
41+
constserver=http.createServer(common.mustCall(function(req,res){
4042
// just do nothing, we should get a timeout event.
4143
consts=req.setTimeout(50,common.mustCall(function(socket){
4244
socket.destroy();
4345
server.close();
4446
cb();
4547
}));
4648
assert.ok(sinstanceofhttp.IncomingMessage);
47-
});
49+
}));
4850
server.listen(common.mustCall(function(){
49-
constport=server.address().port;
50-
constreq=http.request({port: port,method: 'POST'});
51+
constreq=http.request({
52+
port: server.address().port,
53+
method: 'POST'
54+
});
5155
req.on('error',common.mustCall());
5256
req.write('Hello');
5357
// req is in progress
5458
}));
5559
});
5660

5761
test(functionserverResponseTimeout(cb){
58-
constserver=http.createServer(function(req,res){
62+
constserver=http.createServer(common.mustCall(function(req,res){
5963
// just do nothing, we should get a timeout event.
6064
consts=res.setTimeout(50,common.mustCall(function(socket){
6165
socket.destroy();
6266
server.close();
6367
cb();
6468
}));
6569
assert.ok(sinstanceofhttp.OutgoingMessage);
66-
});
70+
}));
6771
server.listen(common.mustCall(function(){
68-
constport=server.address().port;
69-
http.get({port: port}).on('error',common.mustCall());
72+
http.get({
73+
port: server.address().port
74+
}).on('error',common.mustCall());
7075
}));
7176
});
7277

7378
test(functionserverRequestNotTimeoutAfterEnd(cb){
74-
constserver=http.createServer(function(req,res){
79+
constserver=http.createServer(common.mustCall(function(req,res){
7580
// just do nothing, we should get a timeout event.
7681
consts=req.setTimeout(50,common.mustNotCall());
7782
assert.ok(sinstanceofhttp.IncomingMessage);
7883
res.on('timeout',common.mustCall());
79-
});
80-
server.on('timeout',function(socket){
84+
}));
85+
server.on('timeout',common.mustCall(function(socket){
8186
socket.destroy();
8287
server.close();
8388
cb();
84-
});
89+
}));
8590
server.listen(common.mustCall(function(){
86-
constport=server.address().port;
87-
http.get({port: port}).on('error',common.mustCall());
91+
http.get({
92+
port: server.address().port
93+
}).on('error',common.mustCall());
8894
}));
8995
});
9096

@@ -103,16 +109,19 @@ test(function serverResponseTimeoutWithPipeline(cb){
103109
assert.ok(sinstanceofhttp.OutgoingMessage);
104110
if(req.url==='/1')res.end();
105111
});
106-
server.on('timeout',function(socket){
112+
server.on('timeout',common.mustCall(function(socket){
107113
if(secReceived){
108114
socket.destroy();
109115
server.close();
110116
cb();
111117
}
112-
});
118+
}));
113119
server.listen(common.mustCall(function(){
114-
constport=server.address().port;
115-
constc=net.connect({port: port,allowHalfOpen: true},function(){
120+
constoptions={
121+
port: server.address().port,
122+
allowHalfOpen: true,
123+
};
124+
constc=net.connect(options,function(){
116125
c.write('GET /1 HTTP/1.1\r\nHost: localhost\r\n\r\n');
117126
c.write('GET /2 HTTP/1.1\r\nHost: localhost\r\n\r\n');
118127
c.write('GET /3 HTTP/1.1\r\nHost: localhost\r\n\r\n');
@@ -121,20 +130,23 @@ test(function serverResponseTimeoutWithPipeline(cb){
121130
});
122131

123132
test(functionidleTimeout(cb){
124-
constserver=http.createServer(function(req,res){
133+
constserver=http.createServer(common.mustCall(function(req,res){
125134
req.on('timeout',common.mustNotCall());
126135
res.on('timeout',common.mustNotCall());
127136
res.end();
128-
});
137+
}));
129138
consts=server.setTimeout(50,common.mustCall(function(socket){
130139
socket.destroy();
131140
server.close();
132141
cb();
133142
}));
134143
assert.ok(sinstanceofhttp.Server);
135144
server.listen(common.mustCall(function(){
136-
constport=server.address().port;
137-
constc=net.connect({port: port,allowHalfOpen: true},function(){
145+
constoptions={
146+
port: server.address().port,
147+
allowHalfOpen: true,
148+
};
149+
constc=net.connect(options,function(){
138150
c.write('GET /1 HTTP/1.1\r\nHost: localhost\r\n\r\n');
139151
// Keep-Alive
140152
});

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

Lines changed: 71 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ if (!common.hasCrypto){
66
common.skip('missing crypto');
77
return;
88
}
9+
910
consthttps=require('https');
11+
consthttp=require('http');
1012

1113
consttls=require('tls');
1214
constfs=require('fs');
@@ -35,83 +37,93 @@ function run(){
3537
}
3638

3739
test(functionserverTimeout(cb){
38-
constserver=https.createServer(serverOptions,function(req,res){
39-
// just do nothing, we should get a timeout event.
40-
});
41-
server.listen(0,common.mustCall(function(){
40+
constserver=https.createServer(
41+
serverOptions,
42+
common.mustCall(function(req,res){
43+
// just do nothing, we should get a
44+
// timeout event.
45+
}));
46+
server.listen(common.mustCall(function(){
4247
consts=server.setTimeout(50,common.mustCall(function(socket){
4348
socket.destroy();
4449
server.close();
4550
cb();
4651
}));
4752
assert.ok(sinstanceofhttps.Server);
4853
https.get({
49-
port: this.address().port,
54+
port: server.address().port,
5055
rejectUnauthorized: false
5156
}).on('error',common.noop);
5257
}));
5358
});
5459

5560
test(functionserverRequestTimeout(cb){
56-
functionhandler(req,res){
57-
// just do nothing, we should get a timeout event.
58-
req.setTimeout(50,common.mustCall(function(){
59-
req.socket.destroy();
60-
server.close();
61-
cb();
61+
constserver=https.createServer(
62+
serverOptions,
63+
common.mustCall(function(req,res){
64+
// just do nothing, we should get a
65+
// timeout event.
66+
consts=req.setTimeout(
67+
50,
68+
common.mustCall(function(socket){
69+
socket.destroy();
70+
server.close();
71+
cb();
72+
}));
73+
assert.ok(sinstanceofhttp.IncomingMessage);
6274
}));
63-
}
64-
65-
letserver=https.createServer(serverOptions,common.mustCall(handler));
66-
server.listen(0,function(){
75+
server.listen(common.mustCall(function(){
6776
constreq=https.request({
68-
port: this.address().port,
77+
port: server.address().port,
6978
method: 'POST',
7079
rejectUnauthorized: false
7180
});
7281
req.on('error',common.noop);
7382
req.write('Hello');
7483
// req is in progress
75-
});
84+
}));
7685
});
7786

7887
test(functionserverResponseTimeout(cb){
79-
functionhandler(req,res){
80-
// just do nothing, we should get a timeout event.
81-
res.setTimeout(50,common.mustCall(function(){
82-
res.socket.destroy();
83-
server.close();
84-
cb();
88+
constserver=https.createServer(
89+
serverOptions,
90+
common.mustCall(function(req,res){
91+
// just do nothing, we should get a timeout event.
92+
consts=res.setTimeout(50,common.mustCall(function(socket){
93+
socket.destroy();
94+
server.close();
95+
cb();
96+
}));
97+
assert.ok(sinstanceofhttp.OutgoingMessage);
8598
}));
86-
}
87-
88-
letserver=https.createServer(serverOptions,common.mustCall(handler));
89-
server.listen(0,function(){
99+
server.listen(common.mustCall(function(){
90100
https.get({
91-
port: this.address().port,
101+
port: server.address().port,
92102
rejectUnauthorized: false
93-
}).on('error',common.noop);
94-
});
103+
}).on('error',common.mustCall());
104+
}));
95105
});
96106

97107
test(functionserverRequestNotTimeoutAfterEnd(cb){
98-
functionhandler(req,res){
99-
// just do nothing, we should get a timeout event.
100-
req.setTimeout(50,common.mustNotCall());
101-
res.on('timeout',common.mustCall(function(socket){}));
102-
}
103-
constserver=https.createServer(serverOptions,common.mustCall(handler));
104-
server.on('timeout',function(socket){
108+
constserver=https.createServer(
109+
serverOptions,
110+
common.mustCall(function(req,res){
111+
// just do nothing, we should get a timeout event.
112+
consts=req.setTimeout(50,common.mustNotCall());
113+
assert.ok(sinstanceofhttp.IncomingMessage);
114+
res.on('timeout',common.mustCall());
115+
}));
116+
server.on('timeout',common.mustCall(function(socket){
105117
socket.destroy();
106118
server.close();
107119
cb();
108-
});
109-
server.listen(0,function(){
120+
}));
121+
server.listen(common.mustCall(function(){
110122
https.get({
111-
port: this.address().port,
123+
port: server.address().port,
112124
rejectUnauthorized: false
113-
}).on('error',common.noop);
114-
});
125+
}).on('error',common.mustCall());
126+
}));
115127
});
116128

117129
test(functionserverResponseTimeoutWithPipeline(cb){
@@ -123,9 +135,10 @@ test(function serverResponseTimeoutWithPipeline(cb){
123135
constserver=https.createServer(serverOptions,function(req,res){
124136
if(req.url==='/2')
125137
secReceived=true;
126-
res.setTimeout(50,function(){
138+
consts=res.setTimeout(50,function(){
127139
caughtTimeout+=req.url;
128140
});
141+
assert.ok(sinstanceofhttp.OutgoingMessage);
129142
if(req.url==='/1')res.end();
130143
});
131144
server.on('timeout',function(socket){
@@ -135,9 +148,9 @@ test(function serverResponseTimeoutWithPipeline(cb){
135148
cb();
136149
}
137150
});
138-
server.listen(0,function(){
151+
server.listen(common.mustCall(function(){
139152
constoptions={
140-
port: this.address().port,
153+
port: server.address().port,
141154
allowHalfOpen: true,
142155
rejectUnauthorized: false
143156
};
@@ -146,30 +159,32 @@ test(function serverResponseTimeoutWithPipeline(cb){
146159
c.write('GET /2 HTTP/1.1\r\nHost: localhost\r\n\r\n');
147160
c.write('GET /3 HTTP/1.1\r\nHost: localhost\r\n\r\n');
148161
});
149-
});
162+
}));
150163
});
151164

152165
test(functionidleTimeout(cb){
153-
constserver=https.createServer(serverOptions,
154-
common.mustCall(function(req,res){
155-
req.on('timeout',common.mustNotCall());
156-
res.on('timeout',common.mustNotCall());
157-
res.end();
158-
}));
159-
server.setTimeout(50,common.mustCall(function(socket){
166+
constserver=https.createServer(
167+
serverOptions,
168+
common.mustCall(function(req,res){
169+
req.on('timeout',common.mustNotCall());
170+
res.on('timeout',common.mustNotCall());
171+
res.end();
172+
}));
173+
consts=server.setTimeout(50,common.mustCall(function(socket){
160174
socket.destroy();
161175
server.close();
162176
cb();
163177
}));
164-
server.listen(0,function(){
178+
assert.ok(sinstanceofhttps.Server);
179+
server.listen(common.mustCall(function(){
165180
constoptions={
166-
port: this.address().port,
181+
port: server.address().port,
167182
allowHalfOpen: true,
168183
rejectUnauthorized: false
169184
};
170185
tls.connect(options,function(){
171186
this.write('GET /1 HTTP/1.1\r\nHost: localhost\r\n\r\n');
172187
// Keep-Alive
173188
});
174-
});
189+
}));
175190
});

0 commit comments

Comments
(0)