From bdddb825956996be5afd3a4b9f59cd65cb27da11 Mon Sep 17 00:00:00 2001 From: Leko Date: Thu, 14 Dec 2017 01:33:35 +0900 Subject: [PATCH] test: check socketOnDrain where needPause is false Adds a tests that checks if we can start reading again from a socket backing an incoming http request. PR-URL: https://github.com/nodejs/node/pull/17654 Reviewed-By: Matteo Collina Reviewed-By: Anatoli Papirovski --- test/parallel/test-http-hightwatermark.js | 52 +++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 test/parallel/test-http-hightwatermark.js diff --git a/test/parallel/test-http-hightwatermark.js b/test/parallel/test-http-hightwatermark.js new file mode 100644 index 00000000000000..b2432227a9d5f5 --- /dev/null +++ b/test/parallel/test-http-hightwatermark.js @@ -0,0 +1,52 @@ +'use strict'; +const common = require('../common'); +const assert = require('assert'); +const net = require('net'); +const http = require('http'); + +// 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);