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

Add a test about socketOnDrain where needPause is false #17654

Closed
wants to merge 6 commits into from
Closed
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
52 changes: 52 additions & 0 deletions test/parallel/test-http-hightwatermark.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
'use strict';
const common = require('../common');
const assert = require('assert');
const net = require('net');
const http = require('http');

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a description of the test here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your review.
I added a description. Please check it again.

// These test cases to check socketOnDrain where needPause becomes false.
// When send large response enough to exceed highWaterMark, it expect the socket
// to be paused and res.write would be failed.
// And it should be resumed when outgoingData falls below highWaterMark.

let requestReceived = 0;

const server = http.createServer(function(req, res) {
const id = ++requestReceived;
const enoughToDrain = req.connection.writableHighWaterMark;
const body = 'x'.repeat(enoughToDrain);

if (id === 1) {
// Case of needParse = false
req.connection.once('pause', common.mustCall(() => {
assert(req.connection._paused, '_paused must be true because it exceeds' +
'highWaterMark by second request');
}));
} else {
// Case of needParse = true
const resume = req.connection.parser.resume.bind(req.connection.parser);
req.connection.parser.resume = common.mustCall((...args) => {
const paused = req.connection._paused;
assert(!paused, '_paused must be false because it become false by ' +
'socketOnDrain when outgoingData falls below ' +
'highWaterMark');
return resume(...args);
});
}
assert(!res.write(body), 'res.write must return false because it will ' +
'exceed highWaterMark on this call');
res.end();
}).on('listening', () => {
const c = net.createConnection(server.address().port, () => {
c.write('GET / HTTP/1.1\r\n\r\n');
c.write('GET / HTTP/1.1\r\n\r\n');
c.end();
});

c.on('data', () => {});
c.on('end', () => {
server.close();
});
});

server.listen(0);