Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

http: add test case for req-res close ordering #36645

Merged
merged 1 commit into from
Jan 6, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
144 changes: 119 additions & 25 deletions test/parallel/test-http-req-res-close.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,128 @@ const common = require('../common');
const http = require('http');
const assert = require('assert');

const server = http.Server(common.mustCall((req, res) => {
let resClosed = false;

res.end();
let resFinished = false;
res.on('finish', common.mustCall(() => {
resFinished = true;
assert.strictEqual(resClosed, false);
assert.strictEqual(res.destroyed, false);
assert.strictEqual(resClosed, false);
// When the response is ended immediately, `req` should emit `close`
// after `res`
{
const server = http.Server(common.mustCall((req, res) => {
let resClosed = false;
let reqClosed = false;

res.end();
let resFinished = false;
res.on('finish', common.mustCall(() => {
resFinished = true;
assert.strictEqual(resClosed, false);
assert.strictEqual(res.destroyed, false);
assert.strictEqual(resClosed, false);
}));
assert.strictEqual(req.destroyed, false);
res.on('close', common.mustCall(() => {
resClosed = true;
assert.strictEqual(resFinished, true);
assert.strictEqual(reqClosed, false);
assert.strictEqual(res.destroyed, true);
}));
assert.strictEqual(req.destroyed, false);
req.on('end', common.mustCall(() => {
assert.strictEqual(req.destroyed, false);
}));
req.on('close', common.mustCall(() => {
reqClosed = true;
assert.strictEqual(resClosed, true);
assert.strictEqual(req.destroyed, true);
assert.strictEqual(req._readableState.ended, true);
}));
res.socket.on('close', () => server.close());
}));
assert.strictEqual(req.destroyed, false);
res.on('close', common.mustCall(() => {
resClosed = true;
assert.strictEqual(resFinished, true);
assert.strictEqual(res.destroyed, true);

server.listen(0, common.mustCall(() => {
http.get({ port: server.address().port }, common.mustCall());
}));
assert.strictEqual(req.destroyed, false);
req.on('end', common.mustCall(() => {
}

// When there's no `data` handler attached to `req`,
// `req` should emit `close` after `res`.
{
const server = http.Server(common.mustCall((req, res) => {
let resClosed = false;
let reqClosed = false;

// This time, don't end the response immediately
setTimeout(() => res.end(), 100);
let resFinished = false;
res.on('finish', common.mustCall(() => {
resFinished = true;
assert.strictEqual(resClosed, false);
assert.strictEqual(res.destroyed, false);
assert.strictEqual(resClosed, false);
}));
assert.strictEqual(req.destroyed, false);
res.on('close', common.mustCall(() => {
resClosed = true;
assert.strictEqual(resFinished, true);
assert.strictEqual(reqClosed, false);
assert.strictEqual(res.destroyed, true);
}));
assert.strictEqual(req.destroyed, false);
req.on('end', common.mustCall(() => {
assert.strictEqual(req.destroyed, false);
}));
req.on('close', common.mustCall(() => {
reqClosed = true;
assert.strictEqual(resClosed, true);
assert.strictEqual(req.destroyed, true);
assert.strictEqual(req._readableState.ended, true);
}));
res.socket.on('close', () => server.close());
}));
req.on('close', common.mustCall(() => {
assert.strictEqual(req.destroyed, true);
assert.strictEqual(req._readableState.ended, true);

server.listen(0, common.mustCall(() => {
http.get({ port: server.address().port }, common.mustCall());
}));
res.socket.on('close', () => server.close());
}));
}

// When a `data` handler is `attached` to `req`
// (i.e. the server is consuming data from it), `req` should emit `close`
// before `res`.
// https://github.com/nodejs/node/pull/33035 introduced this change in behavior.
// See https://github.com/nodejs/node/pull/33035#issuecomment-751482764
{
const server = http.Server(common.mustCall((req, res) => {
let resClosed = false;
let reqClosed = false;

server.listen(0, common.mustCall(() => {
http.get({ port: server.address().port }, common.mustCall());
}));
// Don't end the response immediately
setTimeout(() => res.end(), 100);
let resFinished = false;
req.on('data', () => {});
res.on('finish', common.mustCall(() => {
resFinished = true;
assert.strictEqual(resClosed, false);
assert.strictEqual(res.destroyed, false);
assert.strictEqual(resClosed, false);
}));
assert.strictEqual(req.destroyed, false);
res.on('close', common.mustCall(() => {
resClosed = true;
assert.strictEqual(resFinished, true);
assert.strictEqual(reqClosed, true);
assert.strictEqual(res.destroyed, true);
}));
assert.strictEqual(req.destroyed, false);
req.on('end', common.mustCall(() => {
assert.strictEqual(req.destroyed, false);
}));
req.on('close', common.mustCall(() => {
reqClosed = true;
assert.strictEqual(resClosed, false);
assert.strictEqual(req.destroyed, true);
assert.strictEqual(req._readableState.ended, true);
}));
res.socket.on('close', () => server.close());
}));

server.listen(0, common.mustCall(() => {
http.get({ port: server.address().port }, common.mustCall());
}));
}