Skip to content

Commit 0b337cb

Browse files
apapirovskicjihrig
authored andcommitted
test: fix flaky test-http2-server-rst-stream.js
PR-URL: #16690Fixes: #16688 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Matteo Collina <[email protected]>
1 parent 5077faf commit 0b337cb

File tree

1 file changed

+35
-58
lines changed

1 file changed

+35
-58
lines changed

‎test/parallel/test-http2-server-rst-stream.js‎

Lines changed: 35 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -5,75 +5,52 @@ if (!common.hasCrypto)
55
common.skip('missing crypto');
66
constassert=require('assert');
77
consthttp2=require('http2');
8+
constCountdown=require('../common/countdown');
89

910
const{
10-
HTTP2_HEADER_METHOD,
11-
HTTP2_HEADER_PATH,
12-
HTTP2_METHOD_POST,
1311
NGHTTP2_CANCEL,
1412
NGHTTP2_NO_ERROR,
1513
NGHTTP2_PROTOCOL_ERROR,
1614
NGHTTP2_REFUSED_STREAM,
1715
NGHTTP2_INTERNAL_ERROR
1816
}=http2.constants;
1917

20-
consterrCheck=common.expectsError({code: 'ERR_HTTP2_STREAM_ERROR'},6);
18+
consttests=[
19+
['rstStream',NGHTTP2_NO_ERROR,false],
20+
['rstWithNoError',NGHTTP2_NO_ERROR,false],
21+
['rstWithProtocolError',NGHTTP2_PROTOCOL_ERROR,true],
22+
['rstWithCancel',NGHTTP2_CANCEL,false],
23+
['rstWithRefuse',NGHTTP2_REFUSED_STREAM,true],
24+
['rstWithInternalError',NGHTTP2_INTERNAL_ERROR,true]
25+
];
26+
27+
constserver=http2.createServer();
28+
server.on('stream',(stream,headers)=>{
29+
constmethod=headers['rstmethod'];
30+
stream[method]();
31+
});
32+
33+
server.listen(0,common.mustCall(()=>{
34+
constclient=http2.connect(`http://localhost:${server.address().port}`);
35+
36+
constcountdown=newCountdown(tests.length,common.mustCall(()=>{
37+
client.destroy();
38+
server.close();
39+
}));
2140

22-
functioncheckRstCode(rstMethod,expectRstCode){
23-
constserver=http2.createServer();
24-
server.on('stream',(stream,headers,flags)=>{
25-
stream.respond({
26-
'content-type': 'text/html',
27-
':status': 200
41+
tests.forEach((test)=>{
42+
constreq=client.request({
43+
':method': 'POST',
44+
rstmethod: test[0]
2845
});
29-
stream.write('test');
30-
if(rstMethod==='rstStream')
31-
stream[rstMethod](expectRstCode);
32-
else
33-
stream[rstMethod]();
34-
35-
if(expectRstCode!==NGHTTP2_NO_ERROR&&
36-
expectRstCode!==NGHTTP2_CANCEL){
37-
stream.on('error',common.mustCall(errCheck));
38-
}else{
39-
stream.on('error',common.mustNotCall());
40-
}
41-
});
42-
43-
server.listen(0,common.mustCall(()=>{
44-
constport=server.address().port;
45-
constclient=http2.connect(`http://localhost:${port}`);
46-
47-
constheaders={
48-
[HTTP2_HEADER_PATH]: '/',
49-
[HTTP2_HEADER_METHOD]: HTTP2_METHOD_POST
50-
};
51-
constreq=client.request(headers);
52-
53-
req.setEncoding('utf8');
54-
req.on('streamClosed',common.mustCall((actualRstCode)=>{
55-
assert.strictEqual(
56-
expectRstCode,actualRstCode,`${rstMethod} is not match rstCode`);
57-
server.close();
58-
client.destroy();
46+
req.on('streamClosed',common.mustCall((code)=>{
47+
assert.strictEqual(code,test[1]);
48+
countdown.dec();
5949
}));
60-
req.on('data',common.mustCall());
6150
req.on('aborted',common.mustCall());
62-
req.on('end',common.mustCall());
63-
64-
if(expectRstCode!==NGHTTP2_NO_ERROR&&
65-
expectRstCode!==NGHTTP2_CANCEL){
66-
req.on('error',common.mustCall(errCheck));
67-
}else{
51+
if(test[2])
52+
req.on('error',common.mustCall());
53+
else
6854
req.on('error',common.mustNotCall());
69-
}
70-
71-
}));
72-
}
73-
74-
checkRstCode('rstStream',NGHTTP2_NO_ERROR);
75-
checkRstCode('rstWithNoError',NGHTTP2_NO_ERROR);
76-
checkRstCode('rstWithProtocolError',NGHTTP2_PROTOCOL_ERROR);
77-
checkRstCode('rstWithCancel',NGHTTP2_CANCEL);
78-
checkRstCode('rstWithRefuse',NGHTTP2_REFUSED_STREAM);
79-
checkRstCode('rstWithInternalError',NGHTTP2_INTERNAL_ERROR);
55+
});
56+
}));

0 commit comments

Comments
(0)