Skip to content

Commit 1f29963

Browse files
committed
test,http: fix http dump test
Make sure the dump test actually verify what is happening and it is not flaky. See: #19139 PR-URL: #19823 Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 3217c70 commit 1f29963

File tree

2 files changed

+40
-22
lines changed

2 files changed

+40
-22
lines changed

lib/_http_server.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -560,7 +560,7 @@ function resOnFinish(req, res, socket, state, server) {
560560
// if the user never called req.read(), and didn't pipe() or
561561
// .resume() or .on('data'), then we call req._dump() so that the
562562
// bytes will be pulled off the wire.
563-
if (!req._consuming && !req._readableState.resumeScheduled)
563+
if (!req._readableState.resumeScheduled)
564564
req._dump();
565565

566566
res.detachSocket(socket);

test/sequential/test-http-dump-req-when-res-ends.js

Lines changed: 39 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,53 +2,71 @@
22

33
const common = require('../common');
44
const http = require('http');
5+
const assert = require('assert');
56
const fs = require('fs');
67

7-
const server = http.createServer(function(req, res) {
8-
// this checks if the request gets dumped
8+
let resEnd = null;
9+
10+
const server = http.createServer(common.mustCall(function(req, res) {
11+
// This checks if the request gets dumped
12+
// resume will be triggered by res.end().
913
req.on('resume', common.mustCall(function() {
10-
console.log('resume called');
14+
// There is no 'data' event handler anymore
15+
// it gets automatically removed when dumping the request.
16+
assert.strictEqual(req.listenerCount('data'), 0);
1117

1218
req.on('data', common.mustCallAtLeast(function(d) {
19+
// Leaving the console.log explicitly, so that
20+
// we can know how many chunks we have received.
1321
console.log('data', d);
1422
}, 1));
1523
}));
1624

17-
// end is not called as we are just exhausting
18-
// the in-memory buffer
19-
req.on('end', common.mustNotCall);
20-
21-
// this 'data' handler will be removed when dumped
22-
req.on('data', common.mustNotCall);
25+
// We explicitly pause the stream
26+
// so that the following on('data') does not cause
27+
// a resume.
28+
req.pause();
29+
req.on('data', function() {});
2330

24-
// start sending the response
31+
// Start sending the response.
2532
res.flushHeaders();
2633

27-
setTimeout(function() {
28-
res.end('hello world');
29-
}, common.platformTimeout(100));
30-
});
34+
resEnd = function() {
35+
setImmediate(function() {
36+
res.end('hello world');
37+
});
38+
};
39+
}));
3140

32-
server.listen(0, function() {
41+
server.listen(0, common.mustCall(function() {
3342
const req = http.request({
3443
method: 'POST',
3544
port: server.address().port
3645
});
3746

3847
// Send the http request without waiting
39-
// for the body
48+
// for the body.
4049
req.flushHeaders();
4150

4251
req.on('response', common.mustCall(function(res) {
43-
// pipe the body as soon as we get the headers of the
44-
// response back
45-
fs.createReadStream(__filename).pipe(req);
52+
// Pipe the body as soon as we get the headers of the
53+
// response back.
54+
const readFileStream = fs.createReadStream(__filename);
55+
readFileStream.on('end', resEnd);
56+
57+
readFileStream.pipe(req);
4658

4759
res.resume();
4860

49-
// wait for the response
61+
// Wait for the response.
5062
res.on('end', function() {
5163
server.close();
5264
});
5365
}));
54-
});
66+
67+
req.on('error', function() {
68+
// An error can happen if there is some data still
69+
// being sent, as the other side is calling .destroy()
70+
// this is safe to ignore.
71+
});
72+
}));

0 commit comments

Comments
 (0)