From 0a262803882adcdcd8dad8a80153e434206290fa Mon Sep 17 00:00:00 2001 From: Jeremiah Senkpiel Date: Tue, 20 Feb 2018 13:26:04 -0500 Subject: [PATCH] test: really test the ttywrap bits of getasyncid Follow-up from https://github.com/nodejs/node/pull/18800 Code that tries to exercise tty fds must be placed in `/pseudo-tty/`. PR-URL: https://github.com/nodejs/node/pull/18886 Reviewed-By: Ruben Bridgewater --- .../test-async-wrap-getasyncid-tty.js | 43 ++++++++++++++++ .../test-async-wrap-getasyncid-tty.out | 0 test/sequential/test-async-wrap-getasyncid.js | 50 +++---------------- 3 files changed, 51 insertions(+), 42 deletions(-) create mode 100644 test/pseudo-tty/test-async-wrap-getasyncid-tty.js create mode 100644 test/pseudo-tty/test-async-wrap-getasyncid-tty.out diff --git a/test/pseudo-tty/test-async-wrap-getasyncid-tty.js b/test/pseudo-tty/test-async-wrap-getasyncid-tty.js new file mode 100644 index 00000000000..d931a7fdb06 --- /dev/null +++ b/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'); +} diff --git a/test/pseudo-tty/test-async-wrap-getasyncid-tty.out b/test/pseudo-tty/test-async-wrap-getasyncid-tty.out new file mode 100644 index 00000000000..e69de29bb2d diff --git a/test/sequential/test-async-wrap-getasyncid.js b/test/sequential/test-async-wrap-getasyncid.js index ce9465d94af..bb0ad3d0ef4 100644 --- a/test/sequential/test-async-wrap-getasyncid.js +++ b/test/sequential/test-async-wrap-getasyncid.js @@ -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); })); } @@ -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();