Skip to content

Commit eca333a

Browse files
billywhizzaddaleax
authored andcommitted
test: refactor test after review
PR-URL: #18999 Reviewed-By: Shingo Inoue <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 0789eec commit eca333a

File tree

1 file changed

+12
-20
lines changed

1 file changed

+12
-20
lines changed

‎test/parallel/test-http-client-spurious-aborted.js‎

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,37 +3,26 @@
33
constcommon=require('../common');
44
consthttp=require('http');
55
constassert=require('assert');
6-
constfs=require('fs');
6+
const{ Writable }=require('stream');
77
constCountdown=require('../common/countdown');
88

9-
functioncleanup(fname){
10-
try{
11-
if(fs.statSync(fname))fs.unlinkSync(fname);
12-
}catch(err){}
13-
}
14-
159
constN=2;
16-
constfname='/dev/null';
1710
letabortRequest=true;
1811

1912
constserver=http.Server(common.mustCall((req,res)=>{
2013
constheaders={'Content-Type': 'text/plain'};
2114
headers['Content-Length']=50;
2215
constsocket=res.socket;
2316
res.writeHead(200,headers);
24-
setTimeout(()=>res.write('aaaaaaaaaa'),100);
25-
setTimeout(()=>res.write('bbbbbbbbbb'),200);
26-
setTimeout(()=>res.write('cccccccccc'),300);
27-
setTimeout(()=>res.write('dddddddddd'),400);
17+
res.write('aaaaaaaaaabbbbbbbbbbccccccccccdddddddddd');
2818
if(abortRequest){
29-
setTimeout(()=>socket.destroy(),600);
19+
process.nextTick(()=>socket.destroy());
3020
}else{
31-
setTimeout(()=>res.end('eeeeeeeeee'),1000);
21+
process.nextTick(()=>res.end('eeeeeeeeee'));
3222
}
3323
},N));
3424

3525
server.listen(0,common.mustCall(()=>{
36-
cleanup(fname);
3726
download();
3827
}));
3928

@@ -53,13 +42,17 @@ function download(){
5342
assert.strictEqual(res.statusCode,200);
5443
assert.strictEqual(res.headers.connection,'close');
5544
letaborted=false;
56-
constfstream=fs.createWriteStream(fname);
57-
res.pipe(fstream);
45+
constwritable=newWritable({
46+
write(chunk,encoding,callback){
47+
callback();
48+
}
49+
});
50+
res.pipe(writable);
5851
const_handle=res.socket._handle;
5952
_handle._close=res.socket._handle.close;
6053
_handle.close=function(callback){
6154
_handle._close();
62-
// set readable to true event though request is complete
55+
// set readable to true even though request is complete
6356
if(res.complete)res.readable=true;
6457
callback();
6558
};
@@ -70,9 +63,8 @@ function download(){
7063
aborted=true;
7164
});
7265
res.on('error',common.mustNotCall());
73-
fstream.on('finish',()=>{
66+
writable.on('finish',()=>{
7467
assert.strictEqual(aborted,abortRequest);
75-
cleanup(fname);
7668
finishCountdown.dec();
7769
if(finishCountdown.remaining===0)return;
7870
abortRequest=false;// next one should be a good response

0 commit comments

Comments
(0)