Skip to content

Commit

Permalink
test: really test the ttywrap bits of getasyncid
Browse files Browse the repository at this point in the history
Follow-up from #18800

Code that tries to exercise tty fds must be placed in `/pseudo-tty/`.

PR-URL: #18886
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
  • Loading branch information
Fishrock123 committed Feb 22, 2018
1 parent 945eb1b commit 0a26280
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 42 deletions.
43 changes: 43 additions & 0 deletions test/pseudo-tty/test-async-wrap-getasyncid-tty.js
@@ -0,0 +1,43 @@
'use strict';

// see also test/sequential/test-async-wrap-getasyncid.js

const common = require('../common');
const assert = require('assert');
const tty_wrap = process.binding('tty_wrap');
const { TTYWRAP } = process.binding('async_wrap').Providers;
const providers = { TTYWRAP };

// Make sure that the TTYWRAP Provider is tested.
{
const hooks = require('async_hooks').createHook({
init(id, type) {
if (type === 'NONE')
throw new Error('received a provider type of NONE');
delete providers[type];
},
}).enable();
process.on('beforeExit', common.mustCall(() => {
process.removeAllListeners('uncaughtException');
hooks.disable();

const objKeys = Object.keys(providers);
if (objKeys.length > 0)
process._rawDebug(objKeys);
assert.strictEqual(objKeys.length, 0);
}));
}

function testInitialized(req, ctor_name) {
assert.strictEqual(typeof req.getAsyncId, 'function');
assert(Number.isSafeInteger(req.getAsyncId()));
assert(req.getAsyncId() > 0);
assert.strictEqual(req.constructor.name, ctor_name);
}

{
const ttyFd = common.getTTYfd();

const handle = new tty_wrap.TTY(ttyFd, false);
testInitialized(handle, 'TTY');
}
Empty file.
50 changes: 8 additions & 42 deletions test/sequential/test-async-wrap-getasyncid.js
Expand Up @@ -26,16 +26,20 @@ common.crashOnUnhandledRejection();
hooks.disable();
delete providers.NONE; // Should never be used.

// See test/pseudo-tty/test-async-wrap-getasyncid-tty.js
// Requires an 'actual' tty fd to be available.
delete providers.TTYWRAP;

// TODO(jasnell): Test for these
delete providers.HTTP2SESSION;
delete providers.HTTP2STREAM;
delete providers.HTTP2PING;
delete providers.HTTP2SETTINGS;

const obj_keys = Object.keys(providers);
if (obj_keys.length > 0)
process._rawDebug(obj_keys);
assert.strictEqual(obj_keys.length, 0);
const objKeys = Object.keys(providers);
if (objKeys.length > 0)
process._rawDebug(objKeys);
assert.strictEqual(objKeys.length, 0);
}));
}

Expand Down Expand Up @@ -256,44 +260,6 @@ if (common.hasCrypto) { // eslint-disable-line node-core/crypto-check
tls_wrap.wrap(tcp._externalStream, credentials.context, true), 'TLSWrap');
}


{
// Do our best to grab a tty fd.
function getTTYfd() {
const tty = require('tty');
let ttyFd = [0, 1, 2].find(tty.isatty);
if (ttyFd === undefined) {
try {
ttyFd = fs.openSync('/dev/tty');
} catch (e) {
// There aren't any tty fd's available to use.
return -1;
}
}
return ttyFd;
}

const ttyFd = getTTYfd();
if (ttyFd >= 0) {
const tty_wrap = process.binding('tty_wrap');
// fd may still be invalid, so guard against it.
const handle = (() => {
try {
return new tty_wrap.TTY(ttyFd, false);
} catch (e) {
return null;
}
})();
if (handle !== null)
testInitialized(handle, 'TTY');
else
delete providers.TTYWRAP;
} else {
delete providers.TTYWRAP;
}
}


{
const binding = process.binding('udp_wrap');
const handle = new binding.UDP();
Expand Down

0 comments on commit 0a26280

Please sign in to comment.