test: fix flaky test-http2-server-rst-stream.js
PR-URL: https://github.com/nodejs/node/pull/16690 Fixes: https://github.com/nodejs/node/issues/16688 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
This commit is contained in:
parent
9ad994befb
commit
3a977fc8c0
@ -5,11 +5,9 @@ if (!common.hasCrypto)
|
|||||||
common.skip('missing crypto');
|
common.skip('missing crypto');
|
||||||
const assert = require('assert');
|
const assert = require('assert');
|
||||||
const http2 = require('http2');
|
const http2 = require('http2');
|
||||||
|
const Countdown = require('../common/countdown');
|
||||||
|
|
||||||
const {
|
const {
|
||||||
HTTP2_HEADER_METHOD,
|
|
||||||
HTTP2_HEADER_PATH,
|
|
||||||
HTTP2_METHOD_POST,
|
|
||||||
NGHTTP2_CANCEL,
|
NGHTTP2_CANCEL,
|
||||||
NGHTTP2_NO_ERROR,
|
NGHTTP2_NO_ERROR,
|
||||||
NGHTTP2_PROTOCOL_ERROR,
|
NGHTTP2_PROTOCOL_ERROR,
|
||||||
@ -17,63 +15,42 @@ const {
|
|||||||
NGHTTP2_INTERNAL_ERROR
|
NGHTTP2_INTERNAL_ERROR
|
||||||
} = http2.constants;
|
} = http2.constants;
|
||||||
|
|
||||||
const errCheck = common.expectsError({ code: 'ERR_HTTP2_STREAM_ERROR' }, 6);
|
const tests = [
|
||||||
|
['rstStream', NGHTTP2_NO_ERROR, false],
|
||||||
|
['rstWithNoError', NGHTTP2_NO_ERROR, false],
|
||||||
|
['rstWithProtocolError', NGHTTP2_PROTOCOL_ERROR, true],
|
||||||
|
['rstWithCancel', NGHTTP2_CANCEL, false],
|
||||||
|
['rstWithRefuse', NGHTTP2_REFUSED_STREAM, true],
|
||||||
|
['rstWithInternalError', NGHTTP2_INTERNAL_ERROR, true]
|
||||||
|
];
|
||||||
|
|
||||||
function checkRstCode(rstMethod, expectRstCode) {
|
const server = http2.createServer();
|
||||||
const server = http2.createServer();
|
server.on('stream', (stream, headers) => {
|
||||||
server.on('stream', (stream, headers, flags) => {
|
const method = headers['rstmethod'];
|
||||||
stream.respond({
|
stream[method]();
|
||||||
'content-type': 'text/html',
|
});
|
||||||
':status': 200
|
|
||||||
});
|
|
||||||
stream.write('test');
|
|
||||||
if (rstMethod === 'rstStream')
|
|
||||||
stream[rstMethod](expectRstCode);
|
|
||||||
else
|
|
||||||
stream[rstMethod]();
|
|
||||||
|
|
||||||
if (expectRstCode !== NGHTTP2_NO_ERROR &&
|
server.listen(0, common.mustCall(() => {
|
||||||
expectRstCode !== NGHTTP2_CANCEL) {
|
const client = http2.connect(`http://localhost:${server.address().port}`);
|
||||||
stream.on('error', common.mustCall(errCheck));
|
|
||||||
} else {
|
|
||||||
stream.on('error', common.mustNotCall());
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
server.listen(0, common.mustCall(() => {
|
|
||||||
const port = server.address().port;
|
|
||||||
const client = http2.connect(`http://localhost:${port}`);
|
|
||||||
|
|
||||||
const headers = {
|
|
||||||
[HTTP2_HEADER_PATH]: '/',
|
|
||||||
[HTTP2_HEADER_METHOD]: HTTP2_METHOD_POST
|
|
||||||
};
|
|
||||||
const req = client.request(headers);
|
|
||||||
|
|
||||||
req.setEncoding('utf8');
|
|
||||||
req.on('streamClosed', common.mustCall((actualRstCode) => {
|
|
||||||
assert.strictEqual(
|
|
||||||
expectRstCode, actualRstCode, `${rstMethod} is not match rstCode`);
|
|
||||||
server.close();
|
|
||||||
client.destroy();
|
|
||||||
}));
|
|
||||||
req.on('data', common.mustCall());
|
|
||||||
req.on('aborted', common.mustCall());
|
|
||||||
req.on('end', common.mustCall());
|
|
||||||
|
|
||||||
if (expectRstCode !== NGHTTP2_NO_ERROR &&
|
|
||||||
expectRstCode !== NGHTTP2_CANCEL) {
|
|
||||||
req.on('error', common.mustCall(errCheck));
|
|
||||||
} else {
|
|
||||||
req.on('error', common.mustNotCall());
|
|
||||||
}
|
|
||||||
|
|
||||||
|
const countdown = new Countdown(tests.length, common.mustCall(() => {
|
||||||
|
client.destroy();
|
||||||
|
server.close();
|
||||||
}));
|
}));
|
||||||
}
|
|
||||||
|
|
||||||
checkRstCode('rstStream', NGHTTP2_NO_ERROR);
|
tests.forEach((test) => {
|
||||||
checkRstCode('rstWithNoError', NGHTTP2_NO_ERROR);
|
const req = client.request({
|
||||||
checkRstCode('rstWithProtocolError', NGHTTP2_PROTOCOL_ERROR);
|
':method': 'POST',
|
||||||
checkRstCode('rstWithCancel', NGHTTP2_CANCEL);
|
rstmethod: test[0]
|
||||||
checkRstCode('rstWithRefuse', NGHTTP2_REFUSED_STREAM);
|
});
|
||||||
checkRstCode('rstWithInternalError', NGHTTP2_INTERNAL_ERROR);
|
req.on('streamClosed', common.mustCall((code) => {
|
||||||
|
assert.strictEqual(code, test[1]);
|
||||||
|
countdown.dec();
|
||||||
|
}));
|
||||||
|
req.on('aborted', common.mustCall());
|
||||||
|
if (test[2])
|
||||||
|
req.on('error', common.mustCall());
|
||||||
|
else
|
||||||
|
req.on('error', common.mustNotCall());
|
||||||
|
});
|
||||||
|
}));
|
||||||
|
Loading…
x
Reference in New Issue
Block a user