-
-
Notifications
You must be signed in to change notification settings - Fork 35.4k
Expand file tree
/
Copy pathtest-child-process-stdin-close-event.js
More file actions
72 lines (62 loc) · 1.8 KB
/
test-child-process-stdin-close-event.js
File metadata and controls
72 lines (62 loc) · 1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
'use strict';
const common = require('../common');
const assert = require('assert');
const { spawn } = require('child_process');
if (common.isWindows) {
common.skip('Not applicable on Windows');
}
function spawnChild(script) {
return spawn(process.execPath, ['-e', script], {
stdio: ['pipe', 'ignore', 'ignore'],
});
}
function runTest({ script, setup }, done) {
const child = spawnChild(script);
const timeout = setTimeout(() => {
assert.fail('stdin close event was not emitted');
}, 2000);
let closed = false;
let exited = false;
function maybeDone() {
if (!closed || !exited) return;
clearTimeout(timeout);
done();
}
child.stdin.once('close', common.mustCall(() => {
closed = true;
child.kill();
maybeDone();
}));
child.once('exit', common.mustCall(() => {
exited = true;
maybeDone();
}));
setup(child);
}
runTest({
script: 'setTimeout(() => require("fs").closeSync(0), 50); setTimeout(() => {}, 2000)',
setup: () => {},
}, common.mustCall(() => {
runTest({
script: 'setTimeout(() => require("fs").closeSync(0), 200); setTimeout(() => {}, 2000)',
setup: (child) => {
const handle = child.stdin?._handle;
assert.strictEqual(typeof handle?.watchPeerClose, 'function');
handle.watchPeerClose(true, common.mustNotCall());
},
}, common.mustCall(() => {
runTest({
script: 'setTimeout(() => {}, 2000)',
setup: (child) => {
const handle = child.stdin?._handle;
assert.strictEqual(typeof handle?.watchPeerClose, 'function');
child.stdin.once('close', common.mustCall(() => {
assert.doesNotThrow(() => {
handle.watchPeerClose(true, common.mustNotCall());
});
}));
child.stdin.destroy();
},
}, common.mustCall(() => {}));
}));
}));