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

tty: add ref() so process.stdin.ref() etc. work #7360

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions src/tty_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ void TTYWrap::Initialize(Local<Object> target,

env->SetProtoMethod(t, "close", HandleWrap::Close);
env->SetProtoMethod(t, "unref", HandleWrap::Unref);
env->SetProtoMethod(t, "ref", HandleWrap::Ref);
env->SetProtoMethod(t, "hasRef", HandleWrap::HasRef);

StreamWrap::AddMethods(env, t, StreamBase::kFlagNoShutdown);
Expand Down
5 changes: 5 additions & 0 deletions test/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@ On how to run tests in this direcotry, see
<td>Yes</td>
<td>Various tests that are able to be run in parallel.</td>
</tr>
<tr>
<td>pseudo-tty</td>
<td>Yes</td>
<td>Tests that require stdin/stdout/stderr to be a TTY.</td>
</tr>
<tr>
<td>pummel</td>
<td>No</td>
Expand Down
33 changes: 0 additions & 33 deletions test/parallel/test-handle-wrap-isrefed-tty.js

This file was deleted.

3 changes: 3 additions & 0 deletions test/parallel/test-handle-wrap-isrefed.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,6 @@ function makeAssert(message) {
timer._handle.close(
common.mustCall(() => assert(timer._handle.hasRef(), false)));
}


// see also test/pseudo-tty/test-handle-wrap-isrefed-tty.js
27 changes: 27 additions & 0 deletions test/pseudo-tty/ref_keeps_node_running.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
'use strict';
require('../common');

const { TTY, isTTY } = process.binding('tty_wrap');
const strictEqual = require('assert').strictEqual;

strictEqual(isTTY(0), true, 'fd 0 is not a TTY');

const handle = new TTY(0);
handle.readStart();
handle.onread = () => {};

function isHandleActive(handle) {
return process._getActiveHandles().some((active) => active === handle);
}

strictEqual(isHandleActive(handle), true, 'TTY handle not initially active');

handle.unref();

strictEqual(isHandleActive(handle), false, 'TTY handle active after unref()');

handle.ref();

strictEqual(isHandleActive(handle), true, 'TTY handle inactive after ref()');

handle.unref();
Empty file.
27 changes: 27 additions & 0 deletions test/pseudo-tty/test-handle-wrap-isrefed-tty.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
'use strict';

// see also test/parallel/test-handle-wrap-isrefed.js

const common = require('../common');
const strictEqual = require('assert').strictEqual;

function makeAssert(message) {
return function(actual, expected) {
strictEqual(actual, expected, message);
};
}

const assert = makeAssert('hasRef() not working on tty_wrap');

const ReadStream = require('tty').ReadStream;
const tty = new ReadStream(0);
const isTTY = process.binding('tty_wrap').isTTY;
assert(isTTY(0), true);
assert(Object.getPrototypeOf(tty._handle).hasOwnProperty('hasRef'), true);
assert(tty._handle.hasRef(), true);
tty.unref();
assert(tty._handle.hasRef(), false);
tty.ref();
assert(tty._handle.hasRef(), true);
Copy link
Member

Choose a reason for hiding this comment

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

I would prefer to use assert.strictEqual directly everywhere in this file; passing a fixed message for all assert.strictEquals actually removes information from a possible error message.

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 feel like it might be better to leave it as is for consistency with parallel/test-handle-wrap-isrefed.js. This test case is basically one part of that file that needed to be separated out as it needs a TTY.

Copy link
Member

Choose a reason for hiding this comment

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

I agree with @addaleax ... we've been making an effort to convert as many tests as possible to using the strict assertion form where possible. As long as we're in this file making changes, we should go ahead and make the switch here. We can fix up the other tests for consistency separately

tty._handle.close(
common.mustCall(() => assert(tty._handle.hasRef(), false)));
Empty file.