Skip to content
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
14 changes: 10 additions & 4 deletions test/parallel/test-http-readable-data-event.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,21 @@ const http = require('http');
const helloWorld = 'Hello World!';
const helloAgainLater = 'Hello again later!';

let next = null;

const server = http.createServer((req, res) => {
res.writeHead(200, {
'Content-Length': '' + (helloWorld.length + helloAgainLater.length)
});
res.write(helloWorld);

// we need to make sure the data is flushed
setTimeout(() => {
// before writing again
next = () => {
res.end(helloAgainLater);
}, common.platformTimeout(10));
Copy link
Member

Choose a reason for hiding this comment

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

Bonus points for making an instance of platformTimeout() unnecessary.

next = () => {};
};

res.write(helloWorld);
}).listen(0, function() {
const opts = {
hostname: 'localhost',
Expand All @@ -27,14 +32,15 @@ const server = http.createServer((req, res) => {
const expectedRead = [helloWorld, null, helloAgainLater, null];

const req = http.request(opts, (res) => {
res.on('error', common.mustNotCall);
res.on('error', common.mustNotCall());
Copy link
Member

Choose a reason for hiding this comment

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

👍


res.on('readable', common.mustCall(() => {
let data;

do {
data = res.read();
assert.strictEqual(data, expectedRead.shift());
next();
Copy link
Member

Choose a reason for hiding this comment

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

I suggest to remove the next and use a boolean to verify that res.end(helloAgainLater) was not yet called. That should be simpler but does the same as far as I read the code.

// Here
if (!streamEnded) {
  res.end(helloAgainLater);
  streamEnded = true;
}
// At the top of the file
let streamEnded = false;

Copy link
Member Author

Choose a reason for hiding this comment

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

It’s not what the test is supposed to verify. The intent is that we need to wait for the first chunk to be received before sending the second.

Copy link
Member

Choose a reason for hiding this comment

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

The next call can be moved after the loop but it's also ok as is.

} while (data !== null);
}, 2));

Expand Down