11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst common = require('../common');
31cb0ef41Sopenharmony_ciif (!common.isMainThread)
41cb0ef41Sopenharmony_ci  common.skip("Workers don't have process-like stdio");
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ci// Test if Node handles accessing process.stdin if it is a redirected
71cb0ef41Sopenharmony_ci// pipe without deadlocking
81cb0ef41Sopenharmony_ciconst { spawn, spawnSync } = require('child_process');
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_ciconst numTries = 5;
111cb0ef41Sopenharmony_ciconst who = process.argv.length <= 2 ? 'runner' : process.argv[2];
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_ciswitch (who) {
141cb0ef41Sopenharmony_ci  case 'runner':
151cb0ef41Sopenharmony_ci    for (let num = 0; num < numTries; ++num) {
161cb0ef41Sopenharmony_ci      spawnSync(process.argv0,
171cb0ef41Sopenharmony_ci                [process.argv[1], 'parent'],
181cb0ef41Sopenharmony_ci                { 'stdio': 'inherit' });
191cb0ef41Sopenharmony_ci    }
201cb0ef41Sopenharmony_ci    break;
211cb0ef41Sopenharmony_ci  case 'parent': {
221cb0ef41Sopenharmony_ci    const middle = spawn(process.argv0,
231cb0ef41Sopenharmony_ci                         [process.argv[1], 'middle'],
241cb0ef41Sopenharmony_ci                         { 'stdio': 'pipe' });
251cb0ef41Sopenharmony_ci    middle.stdout.on('data', () => {});
261cb0ef41Sopenharmony_ci    break;
271cb0ef41Sopenharmony_ci  }
281cb0ef41Sopenharmony_ci  case 'middle':
291cb0ef41Sopenharmony_ci    spawn(process.argv0,
301cb0ef41Sopenharmony_ci          [process.argv[1], 'bottom'],
311cb0ef41Sopenharmony_ci          { 'stdio': [ process.stdin,
321cb0ef41Sopenharmony_ci                       process.stdout,
331cb0ef41Sopenharmony_ci                       process.stderr ] });
341cb0ef41Sopenharmony_ci    break;
351cb0ef41Sopenharmony_ci  case 'bottom':
361cb0ef41Sopenharmony_ci    process.stdin; // eslint-disable-line no-unused-expressions
371cb0ef41Sopenharmony_ci    break;
381cb0ef41Sopenharmony_ci}
39