Skip to content

Commit b717609

Browse files
trevnorrisaddaleax
authored andcommitted
test,async_hooks: match test-ttywrap.readstream
Match changes made to test-ttywrap.readstream for consistency. PR-URL: #13991 Fixes: #13984 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Refael Ackermann <[email protected]>
1 parent 1fc5c29 commit b717609

File tree

1 file changed

+31
-48
lines changed

1 file changed

+31
-48
lines changed
+31-48
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,47 @@
11
'use strict';
22

33
const common = require('../common');
4-
5-
const tty_fd = common.getTTYfd();
6-
if (tty_fd < 0)
7-
common.skip('no valid TTY fd available');
8-
94
const assert = require('assert');
5+
6+
// general hook test setup
107
const tick = require('./tick');
118
const initHooks = require('./init-hooks');
129
const { checkInvocations } = require('./hook-checks');
1310

14-
const ttyStream = (() => {
15-
try {
16-
return new (require('tty').WriteStream)(tty_fd);
17-
} catch (e) {
18-
return null;
19-
}
20-
})();
21-
if (ttyStream === null)
22-
common.skip('no valid TTY fd available');
23-
2411
const hooks = initHooks();
2512
hooks.enable();
2613

27-
const as = hooks.activitiesOfTypes('TTYWRAP');
28-
assert.strictEqual(as.length, 1);
29-
const tty = as[0];
30-
assert.strictEqual(tty.type, 'TTYWRAP');
31-
assert.strictEqual(typeof tty.uid, 'number');
32-
assert.strictEqual(typeof tty.triggerAsyncId, 'number');
33-
checkInvocations(tty, { init: 1 }, 'when tty created');
14+
if (!process.stdout.isTTY)
15+
return common.skip('no valid writable TTY available');
3416

35-
ttyStream
36-
.on('finish', common.mustCall(onfinish))
37-
.end(common.mustCall(onend));
17+
// test specific setup
18+
const checkInitOpts = { init: 1 };
19+
const checkEndedOpts = { init: 1, before: 1, after: 1, destroy: 1 };
3820

39-
checkInvocations(tty, { init: 1 }, 'when tty.end() was invoked ');
21+
// test code
22+
//
23+
// listen to stdin except on Windows
24+
const activities = hooks.activitiesOfTypes('TTYWRAP');
25+
assert.strictEqual(activities.length, 1);
4026

41-
function onend() {
42-
tick(2, common.mustCall(() =>
43-
checkInvocations(
44-
tty, { init: 1, before: 1, after: 1, destroy: 1 },
45-
'when tty ended ')
46-
));
47-
}
48-
49-
function onfinish() {
50-
tick(2, common.mustCall(() =>
51-
checkInvocations(
52-
tty, { init: 1, before: 1, after: 1, destroy: 1 },
53-
'when tty ended ')
54-
));
55-
}
56-
57-
process.on('exit', onexit);
58-
59-
function onexit() {
27+
const tty = activities[0];
28+
assert.strictEqual(tty.type, 'TTYWRAP');
29+
assert.strictEqual(typeof tty.uid, 'number');
30+
assert.strictEqual(typeof tty.triggerAsyncId, 'number');
31+
checkInvocations(tty, checkInitOpts, 'when tty created');
32+
33+
const delayedOnCloseHandler = common.mustCall(() => {
34+
checkInvocations(tty, checkEndedOpts, 'when tty ended');
35+
});
36+
process.stdout.on('error', (err) => assert.fail(err));
37+
process.stdout.on('close', common.mustCall(() =>
38+
tick(2, delayedOnCloseHandler)
39+
));
40+
process.stdout.destroy();
41+
checkInvocations(tty, checkInitOpts, 'when tty.end() was invoked');
42+
43+
process.on('exit', () => {
6044
hooks.disable();
6145
hooks.sanityCheck('TTYWRAP');
62-
checkInvocations(tty, { init: 1, before: 1, after: 1, destroy: 1 },
63-
'when process exits');
64-
}
46+
checkInvocations(tty, checkEndedOpts, 'when process exits');
47+
});

0 commit comments

Comments
 (0)