11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst common = require('../common');
31cb0ef41Sopenharmony_ciconst assert = require('assert');
41cb0ef41Sopenharmony_ciconst child_process = require('child_process');
51cb0ef41Sopenharmony_ciconst cluster = require('cluster');
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ciif (!process.argv[2]) {
81cb0ef41Sopenharmony_ci  // It seems Windows only allocate new console window for
91cb0ef41Sopenharmony_ci  // attaching processes spawned by detached processes. i.e.
101cb0ef41Sopenharmony_ci  // - If process D is spawned by process C with `detached: true`,
111cb0ef41Sopenharmony_ci  //   and process W is spawned by process D with `detached: false`,
121cb0ef41Sopenharmony_ci  //   W will get a new black console window popped up.
131cb0ef41Sopenharmony_ci  // - If D is spawned by C with `detached: false` or W is spawned
141cb0ef41Sopenharmony_ci  //   by D with `detached: true`, no console window will pop up for W.
151cb0ef41Sopenharmony_ci  //
161cb0ef41Sopenharmony_ci  // So, we have to spawn a detached process first to run the actual test.
171cb0ef41Sopenharmony_ci  const primary = child_process.spawn(
181cb0ef41Sopenharmony_ci    process.argv[0],
191cb0ef41Sopenharmony_ci    [process.argv[1], '--cluster'],
201cb0ef41Sopenharmony_ci    { detached: true, stdio: ['ignore', 'ignore', 'ignore', 'ipc'] });
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_ci  const messageHandlers = {
231cb0ef41Sopenharmony_ci    workerOnline: common.mustCall(),
241cb0ef41Sopenharmony_ci    mainWindowHandle: common.mustCall((msg) => {
251cb0ef41Sopenharmony_ci      assert.match(msg.value, /0\s*/);
261cb0ef41Sopenharmony_ci    }),
271cb0ef41Sopenharmony_ci    workerExit: common.mustCall((msg) => {
281cb0ef41Sopenharmony_ci      assert.strictEqual(msg.code, 0);
291cb0ef41Sopenharmony_ci      assert.strictEqual(msg.signal, null);
301cb0ef41Sopenharmony_ci    })
311cb0ef41Sopenharmony_ci  };
321cb0ef41Sopenharmony_ci
331cb0ef41Sopenharmony_ci  primary.on('message', (msg) => {
341cb0ef41Sopenharmony_ci    const handler = messageHandlers[msg.type];
351cb0ef41Sopenharmony_ci    assert.ok(handler);
361cb0ef41Sopenharmony_ci    handler(msg);
371cb0ef41Sopenharmony_ci  });
381cb0ef41Sopenharmony_ci
391cb0ef41Sopenharmony_ci  primary.on('exit', common.mustCall((code, signal) => {
401cb0ef41Sopenharmony_ci    assert.strictEqual(code, 0);
411cb0ef41Sopenharmony_ci    assert.strictEqual(signal, null);
421cb0ef41Sopenharmony_ci  }));
431cb0ef41Sopenharmony_ci
441cb0ef41Sopenharmony_ci} else if (cluster.isPrimary) {
451cb0ef41Sopenharmony_ci  cluster.setupPrimary({
461cb0ef41Sopenharmony_ci    silent: true,
471cb0ef41Sopenharmony_ci    windowsHide: true
481cb0ef41Sopenharmony_ci  });
491cb0ef41Sopenharmony_ci
501cb0ef41Sopenharmony_ci  const worker = cluster.fork();
511cb0ef41Sopenharmony_ci  worker.on('exit', (code, signal) => {
521cb0ef41Sopenharmony_ci    process.send({ type: 'workerExit', code: code, signal: signal });
531cb0ef41Sopenharmony_ci  });
541cb0ef41Sopenharmony_ci
551cb0ef41Sopenharmony_ci  worker.on('online', (msg) => {
561cb0ef41Sopenharmony_ci    process.send({ type: 'workerOnline' });
571cb0ef41Sopenharmony_ci
581cb0ef41Sopenharmony_ci    let output = '0';
591cb0ef41Sopenharmony_ci    if (process.platform === 'win32') {
601cb0ef41Sopenharmony_ci      output = child_process.execSync(
611cb0ef41Sopenharmony_ci        'powershell -NoProfile -c ' +
621cb0ef41Sopenharmony_ci        `"(Get-Process -Id ${worker.process.pid}).MainWindowHandle"`,
631cb0ef41Sopenharmony_ci        { windowsHide: true, encoding: 'utf8' });
641cb0ef41Sopenharmony_ci    }
651cb0ef41Sopenharmony_ci
661cb0ef41Sopenharmony_ci    process.send({ type: 'mainWindowHandle', value: output });
671cb0ef41Sopenharmony_ci    worker.send('shutdown');
681cb0ef41Sopenharmony_ci  });
691cb0ef41Sopenharmony_ci
701cb0ef41Sopenharmony_ci} else {
711cb0ef41Sopenharmony_ci  cluster.worker.on('message', (msg) => {
721cb0ef41Sopenharmony_ci    cluster.worker.disconnect();
731cb0ef41Sopenharmony_ci  });
741cb0ef41Sopenharmony_ci}
75