forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest-child-process-stdio-reuse-readable-stdio.js
More file actions
33 lines (28 loc) · 1.09 KB
/
test-child-process-stdio-reuse-readable-stdio.js
File metadata and controls
33 lines (28 loc) · 1.09 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
'use strict';
const common = require('../common');
if (common.isWindows) {
// https://github.com/nodejs/node/issues/48300
common.skip('Does not work with cygwin quirks on Windows');
}
const assert = require('assert');
const { spawn } = require('child_process');
// Check that, once a child process has ended, it’s safe to read from a pipe
// that the child had used as input.
// We simulate that using cat | (head -n1; ...)
const p1 = spawn('cat', { stdio: ['pipe', 'pipe', 'inherit'] });
const p2 = spawn('head', ['-n1'], { stdio: [p1.stdout, 'pipe', 'inherit'] });
// First, write the line that gets passed through p2, making 'head' exit.
p1.stdin.write('hello\n');
p2.stdout.setEncoding('utf8');
p2.stdout.on('data', common.mustCall((chunk) => {
assert.strictEqual(chunk, 'hello\n');
}));
p2.on('exit', common.mustCall(() => {
// We can now use cat’s output, because 'head' is no longer reading from it.
p1.stdin.end('world\n');
p1.stdout.setEncoding('utf8');
p1.stdout.on('data', common.mustCall((chunk) => {
assert.strictEqual(chunk, 'world\n');
}));
p1.stdout.resume();
}));