Skip to content

Commit 0b337cb

Browse files
apapirovskicjihrig
authored andcommitted
test: fix flaky test-http2-server-rst-stream.js
PR-URL: #16690 Fixes: #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

+35-58
Original file line numberDiff line numberDiff line change
@@ -5,75 +5,52 @@ if (!common.hasCrypto)
55
common.skip('missing crypto');
66
const assert = require('assert');
77
const http2 = require('http2');
8+
const Countdown = 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-
const errCheck = common.expectsError({ code: 'ERR_HTTP2_STREAM_ERROR' }, 6);
18+
const tests = [
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+
const server = http2.createServer();
28+
server.on('stream', (stream, headers) => {
29+
const method = headers['rstmethod'];
30+
stream[method]();
31+
});
32+
33+
server.listen(0, common.mustCall(() => {
34+
const client = http2.connect(`http://localhost:${server.address().port}`);
35+
36+
const countdown = new Countdown(tests.length, common.mustCall(() => {
37+
client.destroy();
38+
server.close();
39+
}));
2140

22-
function checkRstCode(rstMethod, expectRstCode) {
23-
const server = 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+
const req = 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-
const port = server.address().port;
45-
const client = http2.connect(`http://localhost:${port}`);
46-
47-
const headers = {
48-
[HTTP2_HEADER_PATH]: '/',
49-
[HTTP2_HEADER_METHOD]: HTTP2_METHOD_POST
50-
};
51-
const req = 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)