Skip to content
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: add tests for end event of stream.Duplex #21325

Closed
wants to merge 3 commits into from

Conversation

shisama
Copy link
Contributor

@shisama shisama commented Jun 14, 2018

Added tests to check the stream will automatically end the writable side
when readable side ends when allowHalfOpen option is false.

Checklist
  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • commit message follows commit guidelines

Added tests to check the stream will automatically end the writable side
when readable side ends when allowHalfOpen option is false.
@nodejs-github-bot nodejs-github-bot added the test Issues and PRs related to the tests. label Jun 14, 2018
@trivikr trivikr added the stream Issues and PRs related to the stream subsystem. label Jun 14, 2018
@ryzokuken
Copy link
Contributor

And do they fail? Or perhaps increase coverage? I fail to see why you just added extra tests.

@shisama
Copy link
Contributor Author

shisama commented Jun 14, 2018

@ryzokuken
Doc says :

allowHalfOpen <boolean> If set to false, then the stream will automatically end the writable side when the readable side ends. Default: true.

There was no test cases that allowHalfOpen is false and I added tests.
Also, these tests increase coverage.

@addaleax
Copy link
Member

@addaleax addaleax added the author ready PRs that have at least one approval, no pending requests for changes, and a CI started. label Jun 14, 2018
const Duplex = require('stream').Duplex;
{
const stream = new Duplex();
assert(stream.allowHalfOpen);
Copy link
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
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.

const stream = new Duplex();
assert(stream.allowHalfOpen);
stream.on('finish', common.mustNotCall());
assert.strictEqual(stream.emit('end'), false);
Copy link
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
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
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
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 common = require('../common');
const assert = require('assert');
const Duplex = require('stream').Duplex;
{
Copy link
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?

@jasnell
Copy link
Member

jasnell commented Jun 21, 2018

jasnell pushed a commit that referenced this pull request Jun 22, 2018
Added tests to check the stream will automatically end the writable side
when readable side ends when allowHalfOpen option is false.

PR-URL: #21325
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
Reviewed-By: Trivikram Kamat <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Ujjwal Sharma <[email protected]>
@jasnell
Copy link
Member

jasnell commented Jun 22, 2018

Landed in 383b1b6

@jasnell jasnell closed this Jun 22, 2018
targos pushed a commit that referenced this pull request Jun 24, 2018
Added tests to check the stream will automatically end the writable side
when readable side ends when allowHalfOpen option is false.

PR-URL: #21325
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
Reviewed-By: Trivikram Kamat <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Ujjwal Sharma <[email protected]>
@targos targos mentioned this pull request Jul 3, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
author ready PRs that have at least one approval, no pending requests for changes, and a CI started. stream Issues and PRs related to the stream subsystem. test Issues and PRs related to the tests.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants