• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2const common = require('../common');
3if (!common.isMainThread)
4  common.skip("Workers don't have process-like stdio");
5
6// Test if Node handles redirecting one child process stdout to another
7// process stdin without crashing.
8const spawn = require('child_process').spawn;
9
10const writeSize = 100;
11const totalDots = 10000;
12
13const who = process.argv.length <= 2 ? 'parent' : process.argv[2];
14
15switch (who) {
16  case 'parent':
17    const consumer = spawn(process.argv0, [process.argv[1], 'consumer'], {
18      stdio: ['pipe', 'ignore', 'inherit'],
19    });
20    const producer = spawn(process.argv0, [process.argv[1], 'producer'], {
21      stdio: ['pipe', consumer.stdin, 'inherit'],
22    });
23    process.stdin.on('data', () => {});
24    producer.on('exit', process.exit);
25    break;
26  case 'producer':
27    const buffer = Buffer.alloc(writeSize, '.');
28    let written = 0;
29    const write = () => {
30      if (written < totalDots) {
31        written += writeSize;
32        process.stdout.write(buffer, write);
33      }
34    };
35    write();
36    break;
37  case 'consumer':
38    process.stdin.on('data', () => {});
39    break;
40}
41