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 <ruben@bridgewater.de>
This commit is contained in:
parent
945eb1bb69
commit
0a26280388
43
test/pseudo-tty/test-async-wrap-getasyncid-tty.js
Normal file
43
test/pseudo-tty/test-async-wrap-getasyncid-tty.js
Normal file
@ -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');
|
||||||
|
}
|
0
test/pseudo-tty/test-async-wrap-getasyncid-tty.out
Normal file
0
test/pseudo-tty/test-async-wrap-getasyncid-tty.out
Normal file
@ -26,16 +26,20 @@ common.crashOnUnhandledRejection();
|
|||||||
hooks.disable();
|
hooks.disable();
|
||||||
delete providers.NONE; // Should never be used.
|
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
|
// TODO(jasnell): Test for these
|
||||||
delete providers.HTTP2SESSION;
|
delete providers.HTTP2SESSION;
|
||||||
delete providers.HTTP2STREAM;
|
delete providers.HTTP2STREAM;
|
||||||
delete providers.HTTP2PING;
|
delete providers.HTTP2PING;
|
||||||
delete providers.HTTP2SETTINGS;
|
delete providers.HTTP2SETTINGS;
|
||||||
|
|
||||||
const obj_keys = Object.keys(providers);
|
const objKeys = Object.keys(providers);
|
||||||
if (obj_keys.length > 0)
|
if (objKeys.length > 0)
|
||||||
process._rawDebug(obj_keys);
|
process._rawDebug(objKeys);
|
||||||
assert.strictEqual(obj_keys.length, 0);
|
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');
|
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 binding = process.binding('udp_wrap');
|
||||||
const handle = new binding.UDP();
|
const handle = new binding.UDP();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user