Skip to content
Closed
Changes from 1 commit
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
Next Next commit
test: add tests for end event of stream.Duplex
Added tests to check the stream will automatically end the writable side
when readable side ends when allowHalfOpen option is false.
  • Loading branch information
Masashi Hirano committed Jun 13, 2018
commit e0354aff4c3acc8a739232a19bc91cf7fffcedde
30 changes: 30 additions & 0 deletions test/parallel/test-stream-duplex-end.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
'use strict';

const common = require('../common');
const assert = require('assert');
const Duplex = require('stream').Duplex;
{
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Nit: can please add a new line before this?

const stream = new Duplex();
assert(stream.allowHalfOpen);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Can you use assert.strictEqual() here.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@cjihrig Fixed it. Thank you for your comment.

stream.on('finish', common.mustNotCall());
assert.strictEqual(stream.emit('end'), false);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I wonder if it's better to use stream.push(null) instead of emitting 'end' directly. We can still verify that there is no listener for 'end' event with stream.listenerCount('end') if wanted.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@lpinca Thanks for your review. Would you please tell me why it is better to use stream.push(null) instead of stream.emit('end'). I tried to use stream.push(null) but test didn't emit 'end' and the function onend didn't run.
https://coverage.nodejs.org/coverage-7551de1fc2fbf24e/root/_stream_duplex.js.html#L100

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

That's how the 'end' event is emitted in a real use case. readable.push(null) is used to signal that the stream has ended, you don't emit 'end' manually.

If you switch the stream in flowing mode with stream.resume() and then call stream.push(null) it should emit 'end'. You probably also have to add an implementation for _read().

const stream = new Duplex({ read() {} });
stream.resume();
stream.push(null);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I fixed it and the codes you indicated emit 'end'. Thank you for your comments.

}

{
const stream = new Duplex({
allowHalfOpen: false
});
assert.strictEqual(stream.allowHalfOpen, false);
stream.on('finish', common.mustCall());
assert(stream.emit('end'));
}

{
const stream = new Duplex({
allowHalfOpen: false
});
assert.strictEqual(stream.allowHalfOpen, false);
stream._writableState.ended = true;
stream.on('finish', common.mustNotCall());
assert(stream.emit('end'));
}