Skip to content

Commit bdddb82

Browse files
LekoMylesBorins
authored andcommitted
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: #17654 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Anatoli Papirovski <[email protected]>
1 parent 4b0c875 commit bdddb82

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed
+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
'use strict';
2+
const common = require('../common');
3+
const assert = require('assert');
4+
const net = require('net');
5+
const http = require('http');
6+
7+
// These test cases to check socketOnDrain where needPause becomes false.
8+
// When send large response enough to exceed highWaterMark, it expect the socket
9+
// to be paused and res.write would be failed.
10+
// And it should be resumed when outgoingData falls below highWaterMark.
11+
12+
let requestReceived = 0;
13+
14+
const server = http.createServer(function(req, res) {
15+
const id = ++requestReceived;
16+
const enoughToDrain = req.connection.writableHighWaterMark;
17+
const body = 'x'.repeat(enoughToDrain);
18+
19+
if (id === 1) {
20+
// Case of needParse = false
21+
req.connection.once('pause', common.mustCall(() => {
22+
assert(req.connection._paused, '_paused must be true because it exceeds' +
23+
'highWaterMark by second request');
24+
}));
25+
} else {
26+
// Case of needParse = true
27+
const resume = req.connection.parser.resume.bind(req.connection.parser);
28+
req.connection.parser.resume = common.mustCall((...args) => {
29+
const paused = req.connection._paused;
30+
assert(!paused, '_paused must be false because it become false by ' +
31+
'socketOnDrain when outgoingData falls below ' +
32+
'highWaterMark');
33+
return resume(...args);
34+
});
35+
}
36+
assert(!res.write(body), 'res.write must return false because it will ' +
37+
'exceed highWaterMark on this call');
38+
res.end();
39+
}).on('listening', () => {
40+
const c = net.createConnection(server.address().port, () => {
41+
c.write('GET / HTTP/1.1\r\n\r\n');
42+
c.write('GET / HTTP/1.1\r\n\r\n');
43+
c.end();
44+
});
45+
46+
c.on('data', () => {});
47+
c.on('end', () => {
48+
server.close();
49+
});
50+
});
51+
52+
server.listen(0);

0 commit comments

Comments
 (0)