Skip to content

Commit 0a26280

Browse files
committed
test: really test the ttywrap bits of getasyncid
Follow-up from #18800 Code that tries to exercise tty fds must be placed in `/pseudo-tty/`. PR-URL: #18886 Reviewed-By: Ruben Bridgewater <[email protected]>
1 parent 945eb1b commit 0a26280

File tree

3 files changed

+51
-42
lines changed

3 files changed

+51
-42
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
'use strict';
2+
3+
// see also test/sequential/test-async-wrap-getasyncid.js
4+
5+
const common = require('../common');
6+
const assert = require('assert');
7+
const tty_wrap = process.binding('tty_wrap');
8+
const { TTYWRAP } = process.binding('async_wrap').Providers;
9+
const providers = { TTYWRAP };
10+
11+
// Make sure that the TTYWRAP Provider is tested.
12+
{
13+
const hooks = require('async_hooks').createHook({
14+
init(id, type) {
15+
if (type === 'NONE')
16+
throw new Error('received a provider type of NONE');
17+
delete providers[type];
18+
},
19+
}).enable();
20+
process.on('beforeExit', common.mustCall(() => {
21+
process.removeAllListeners('uncaughtException');
22+
hooks.disable();
23+
24+
const objKeys = Object.keys(providers);
25+
if (objKeys.length > 0)
26+
process._rawDebug(objKeys);
27+
assert.strictEqual(objKeys.length, 0);
28+
}));
29+
}
30+
31+
function testInitialized(req, ctor_name) {
32+
assert.strictEqual(typeof req.getAsyncId, 'function');
33+
assert(Number.isSafeInteger(req.getAsyncId()));
34+
assert(req.getAsyncId() > 0);
35+
assert.strictEqual(req.constructor.name, ctor_name);
36+
}
37+
38+
{
39+
const ttyFd = common.getTTYfd();
40+
41+
const handle = new tty_wrap.TTY(ttyFd, false);
42+
testInitialized(handle, 'TTY');
43+
}

test/pseudo-tty/test-async-wrap-getasyncid-tty.out

Whitespace-only changes.

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

Lines changed: 8 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,20 @@ common.crashOnUnhandledRejection();
2626
hooks.disable();
2727
delete providers.NONE; // Should never be used.
2828

29+
// See test/pseudo-tty/test-async-wrap-getasyncid-tty.js
30+
// Requires an 'actual' tty fd to be available.
31+
delete providers.TTYWRAP;
32+
2933
// TODO(jasnell): Test for these
3034
delete providers.HTTP2SESSION;
3135
delete providers.HTTP2STREAM;
3236
delete providers.HTTP2PING;
3337
delete providers.HTTP2SETTINGS;
3438

35-
const obj_keys = Object.keys(providers);
36-
if (obj_keys.length > 0)
37-
process._rawDebug(obj_keys);
38-
assert.strictEqual(obj_keys.length, 0);
39+
const objKeys = Object.keys(providers);
40+
if (objKeys.length > 0)
41+
process._rawDebug(objKeys);
42+
assert.strictEqual(objKeys.length, 0);
3943
}));
4044
}
4145

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

259-
260-
{
261-
// Do our best to grab a tty fd.
262-
function getTTYfd() {
263-
const tty = require('tty');
264-
let ttyFd = [0, 1, 2].find(tty.isatty);
265-
if (ttyFd === undefined) {
266-
try {
267-
ttyFd = fs.openSync('/dev/tty');
268-
} catch (e) {
269-
// There aren't any tty fd's available to use.
270-
return -1;
271-
}
272-
}
273-
return ttyFd;
274-
}
275-
276-
const ttyFd = getTTYfd();
277-
if (ttyFd >= 0) {
278-
const tty_wrap = process.binding('tty_wrap');
279-
// fd may still be invalid, so guard against it.
280-
const handle = (() => {
281-
try {
282-
return new tty_wrap.TTY(ttyFd, false);
283-
} catch (e) {
284-
return null;
285-
}
286-
})();
287-
if (handle !== null)
288-
testInitialized(handle, 'TTY');
289-
else
290-
delete providers.TTYWRAP;
291-
} else {
292-
delete providers.TTYWRAP;
293-
}
294-
}
295-
296-
297263
{
298264
const binding = process.binding('udp_wrap');
299265
const handle = new binding.UDP();

0 commit comments

Comments
 (0)