-
Notifications
You must be signed in to change notification settings - Fork 29.8k
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
test: test server response that waits for request end #29649
Conversation
Subsystem prefix in the commit message should be |
@nodejs/http PTAL |
724bc0c
to
7492ec1
Compare
This needs investigation. I cannot reproduce with 'use strict';
const assert = require('assert');
const http = require('http');
const testData = 'Hello, World!\n';
const server = http.createServer(function(req, res) {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end(testData);
req.once('end', function() {
res.end(testData);
});
});
server.listen(8080, function() {
const req = http.request({ port: 8080 }, function(res) {
res.setEncoding('utf8');
let data = '';
res.on('data', function(chunk) {
data += chunk;
});
res.on('end', function() {
assert.strictEqual(data, testData);
server.close();
});
});
req.end();
}); Works as expected. |
@lpinca Your test 'use strict';
const assert = require('assert');
const http = require('http');
const testData = 'Hello, World!\n';
const server = http.createServer(function(req, res) {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
// res.end(testData);
req.once('end', function() {
res.end(testData);
});
});
server.listen(8080, function() {
const req = http.request({ port: 8080 }, function(res) {
res.setEncoding('utf8');
let data = '';
res.on('data', function(chunk) {
data += chunk;
});
res.on('end', function() {
assert.strictEqual(data, testData);
server.close();
});
});
req.end();
}); |
Actually, that's because the request was never resumed, ugh req.resume(); seems to make it work again The bug I'm looking to reproduce calls the request "end" event, but fails to write the ending to the response. |
I'm going to spend some time better adapting #29609 |
7492ec1
to
316d1ed
Compare
Added a chunked test case that passes, and a Content-Length test case that does not (but clearly should) |
316d1ed
to
704ecbd
Compare
704ecbd
to
5458e34
Compare
In chunked mode, Node.js works as expected (in all versions). In Content-Length mode, Node.js fails: Prior to 206ae31, Node.js failed only Starting 206ae31, Node.js fails both |
@awwright I copied your tests and again I can't reproduce with Detailsconst assert = require('assert');
const http = require('http');
const testData = 'Hello, World!\n';
{
const server = http.createServer(function(req, res) {
req.setEncoding('utf8');
let data = '';
req.on('data', function(chunk) {
data += chunk;
});
req.on('end', function() {
assert.strictEqual(data, testData);
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.write(testData);
res.end();
});
});
server.listen(function() {
const req = http.request(
{
port: server.address().port,
method: 'PUT',
headers: { Connection: 'close' }
},
function(res) {
res.setEncoding('utf8');
let data = '';
res.on('data', function(chunk) {
data += chunk;
});
res.on('end', function() {
assert.strictEqual(data, testData);
server.close();
});
}
);
req.write(testData);
req.end();
});
}
{
const length = String(testData.length);
const server = http.createServer(function(req, res) {
assert.strictEqual(req.headers['content-length'], length);
req.setEncoding('utf8');
let data = '';
req.on('data', function(chunk) {
data += chunk;
});
req.once('end', function() {
assert.strictEqual(data, testData);
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.setHeader('Content-Length', testData.length);
res.write(testData);
res.end();
});
});
server.listen(function() {
const req = http.request(
{
port: server.address().port,
method: 'PUT',
headers: {
Connection: 'close',
'Content-Length': length
}
},
function(res) {
assert.strictEqual(res.headers['content-length'], length);
res.setEncoding('utf8');
let data = '';
res.on('data', function(chunk) {
data += chunk;
});
res.on('end', function() {
assert.strictEqual(data, testData);
server.close();
});
}
);
req.write(testData);
req.end();
});
} |
@lpinca I assume Nonetheless, is my assessment correct that this test should pass? |
Yes, that's why in #29649 (comment) I said this needs investigation. |
@Trott I suppose #29649 (that seems to be included in #29836) would pass, but makeDuplexPair is also implemented in and at least a couple packages: https://yarnpkg.com/en/package/duplexpair https://yarnpkg.com/en/package/native-duplexpair (edit) Actually this would fix "tls", to whatever extent that's impacted |
I didn't realize this was the test case I wrote up, let's try this again... #29836 is a decedent commit of this PR so it seems to cause the test to pass; but few other libraries still use the old code; e.g. https://yarnpkg.com/en/package/native-duplexpair; and it would need to be updated in the Writable documentation why I need to handle an empty write as a special case. Also, historical versions of my package would continue to fail, even though it was written for Node.js 12.0.0. Not the end of the world, but it makes e.g. |
Updated PR with doc changes. Please take a look and see if you have any further suggestion. |
@ronag It seems unintuitive to me, but with that explanation (that _read isn't really a callback, more like an event, so note a special case where it doesn't get called), I don't really have any reason to object. |
@awwright: I didn't quite follow that :). If you make a commit or PR I'm more than happy to cherry pick your suggestion. |
If nothing is buffered then _read will not be called and the callback will not be invoked, effectivly deadlocking. Fixes: #29758 PR-URL: #29836 Refs: #29649 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Minwoo Jung <minwoo@nodesource.com>
Included in #29836 |
If nothing is buffered then _read will not be called and the callback will not be invoked, effectivly deadlocking. Fixes: #29758 PR-URL: #29836 Refs: #29649 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Minwoo Jung <minwoo@nodesource.com>
This is a test that, by all indications, should pass, and would have passed prior to 206ae31
See #29609 for information.