Skip to content

Commit 3d9bc01

Browse files
refackaddaleax
authored andcommitted
test,async_hooks: stabilize tests on Windows
PR-URL: #13381 Reviewed-By: Andreas Madsen <[email protected]>
1 parent 09eb588 commit 3d9bc01

4 files changed

+39
-30
lines changed

test/async-hooks/test-emit-before-after.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ switch (process.argv[2]) {
1616
}
1717

1818
const c1 = spawnSync(process.execPath, [__filename, 'test_invalid_async_id']);
19-
assert.strictEqual(c1.stderr.toString().split('\n')[0],
19+
assert.strictEqual(c1.stderr.toString().split(/[\r\n]+/g)[0],
2020
'Error: before(): asyncId or triggerAsyncId is less than ' +
2121
'zero (asyncId: -1, triggerAsyncId: -1)');
2222
assert.strictEqual(c1.status, 1);
2323

2424
const c2 = spawnSync(process.execPath, [__filename, 'test_invalid_trigger_id']);
25-
assert.strictEqual(c2.stderr.toString().split('\n')[0],
25+
assert.strictEqual(c2.stderr.toString().split(/[\r\n]+/g)[0],
2626
'Error: before(): asyncId or triggerAsyncId is less than ' +
2727
'zero (asyncId: 1, triggerAsyncId: -1)');
2828
assert.strictEqual(c2.status, 1);

test/async-hooks/test-graph.signal.js

+5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
'use strict';
22

33
const common = require('../common');
4+
if (common.isWindows) {
5+
common.skip('no signals on Windows');
6+
return;
7+
}
8+
49
const initHooks = require('./init-hooks');
510
const verifyGraph = require('./verify-graph');
611
const exec = require('child_process').exec;

test/async-hooks/test-signalwrap.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
'use strict';
2-
32
const common = require('../common');
3+
4+
if (common.isWindows) return common.skip('no signals in Windows');
5+
46
const assert = require('assert');
57
const initHooks = require('./init-hooks');
68
const { checkInvocations } = require('./hook-checks');
+29-27
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,44 @@
11
'use strict';
2-
32
const common = require('../common');
43
const assert = require('assert');
4+
5+
// general hook test setup
56
const tick = require('./tick');
67
const initHooks = require('./init-hooks');
78
const { checkInvocations } = require('./hook-checks');
89

910
const hooks = initHooks();
1011
hooks.enable();
1112

12-
const ReadStream = require('tty').ReadStream;
13-
const ttyStream = new ReadStream(0);
14-
15-
const as = hooks.activitiesOfTypes('TTYWRAP');
16-
assert.strictEqual(as.length, 1);
17-
const tty = as[0];
13+
// test specific setup
14+
const { ReadStream } = require('tty');
15+
const checkInitOpts = { init: 1 };
16+
const checkEndedOpts = { init: 1, before: 1, after: 1, destroy: 1 };
17+
18+
// test code
19+
//
20+
// listen to stdin except on Windows
21+
const targetFD = common.isWindows ? 1 : 0;
22+
const ttyStream = new ReadStream(targetFD);
23+
const activities = hooks.activitiesOfTypes('TTYWRAP');
24+
assert.strictEqual(activities.length, 1);
25+
const tty = activities[0];
1826
assert.strictEqual(tty.type, 'TTYWRAP');
1927
assert.strictEqual(typeof tty.uid, 'number');
2028
assert.strictEqual(typeof tty.triggerAsyncId, 'number');
21-
checkInvocations(tty, { init: 1 }, 'when tty created');
22-
23-
ttyStream.end(common.mustCall(onend));
24-
25-
checkInvocations(tty, { init: 1 }, 'when tty.end() was invoked ');
26-
27-
function onend() {
28-
tick(2, common.mustCall(() =>
29-
checkInvocations(
30-
tty, { init: 1, before: 1, after: 1, destroy: 1 },
31-
'when tty ended ')
32-
));
33-
}
34-
35-
process.on('exit', onexit);
36-
37-
function onexit() {
29+
checkInvocations(tty, checkInitOpts, 'when tty created');
30+
const delayedOnCloseHandler = common.mustCall(() => {
31+
checkInvocations(tty, checkEndedOpts, 'when tty ended');
32+
});
33+
ttyStream.on('error', (err) => assert.fail(err));
34+
ttyStream.on('close', common.mustCall(() =>
35+
tick(2, delayedOnCloseHandler)
36+
));
37+
ttyStream.destroy();
38+
checkInvocations(tty, checkInitOpts, 'when tty.end() was invoked');
39+
40+
process.on('exit', () => {
3841
hooks.disable();
3942
hooks.sanityCheck('TTYWRAP');
40-
checkInvocations(tty, { init: 1, before: 1, after: 1, destroy: 1 },
41-
'when process exits');
42-
}
43+
checkInvocations(tty, checkEndedOpts, 'when process exits');
44+
});

0 commit comments

Comments
 (0)