0
0
mirror of https://github.com/nodejs/node.git synced 2024-11-21 21:19:50 +01:00
nodejs/test/parallel/test-stdio-pipe-redirect.js
Rich Trott 55ceaec111 tools,benchmark,lib,test: enable no-case-declarations lint rule
PR-URL: https://github.com/nodejs/node/pull/41385
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Reviewed-By: Tierney Cyren <hello@bnb.im>
Reviewed-By: Ricky Zhou <0x19951125@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
2022-01-05 07:42:19 -08:00

43 lines
1.1 KiB
JavaScript

'use strict';
const common = require('../common');
if (!common.isMainThread)
common.skip("Workers don't have process-like stdio");
// Test if Node handles redirecting one child process stdout to another
// process stdin without crashing.
const spawn = require('child_process').spawn;
const writeSize = 100;
const totalDots = 10000;
const who = process.argv.length <= 2 ? 'parent' : process.argv[2];
switch (who) {
case 'parent': {
const consumer = spawn(process.argv0, [process.argv[1], 'consumer'], {
stdio: ['pipe', 'ignore', 'inherit'],
});
const producer = spawn(process.argv0, [process.argv[1], 'producer'], {
stdio: ['pipe', consumer.stdin, 'inherit'],
});
process.stdin.on('data', () => {});
producer.on('exit', process.exit);
break;
}
case 'producer': {
const buffer = Buffer.alloc(writeSize, '.');
let written = 0;
const write = () => {
if (written < totalDots) {
written += writeSize;
process.stdout.write(buffer, write);
}
};
write();
break;
}
case 'consumer':
process.stdin.on('data', () => {});
break;
}