1'use strict'; 2const common = require('../common'); 3const assert = require('assert'); 4const child_process = require('child_process'); 5const cluster = require('cluster'); 6 7if (!process.argv[2]) { 8 // It seems Windows only allocate new console window for 9 // attaching processes spawned by detached processes. i.e. 10 // - If process D is spawned by process C with `detached: true`, 11 // and process W is spawned by process D with `detached: false`, 12 // W will get a new black console window popped up. 13 // - If D is spawned by C with `detached: false` or W is spawned 14 // by D with `detached: true`, no console window will pop up for W. 15 // 16 // So, we have to spawn a detached process first to run the actual test. 17 const master = child_process.spawn( 18 process.argv[0], 19 [process.argv[1], '--cluster'], 20 { detached: true, stdio: ['ignore', 'ignore', 'ignore', 'ipc'] }); 21 22 const messageHandlers = { 23 workerOnline: common.mustCall((msg) => { 24 }), 25 mainWindowHandle: common.mustCall((msg) => { 26 assert.ok(/0\s*/.test(msg.value)); 27 }), 28 workerExit: common.mustCall((msg) => { 29 assert.strictEqual(msg.code, 0); 30 assert.strictEqual(msg.signal, null); 31 }) 32 }; 33 34 master.on('message', (msg) => { 35 const handler = messageHandlers[msg.type]; 36 assert.ok(handler); 37 handler(msg); 38 }); 39 40 master.on('exit', common.mustCall((code, signal) => { 41 assert.strictEqual(code, 0); 42 assert.strictEqual(signal, null); 43 })); 44 45} else if (cluster.isMaster) { 46 cluster.setupMaster({ 47 silent: true, 48 windowsHide: true 49 }); 50 51 const worker = cluster.fork(); 52 worker.on('exit', (code, signal) => { 53 process.send({ type: 'workerExit', code: code, signal: signal }); 54 }); 55 56 worker.on('online', (msg) => { 57 process.send({ type: 'workerOnline' }); 58 59 let output = '0'; 60 if (process.platform === 'win32') { 61 output = child_process.execSync( 62 'powershell -NoProfile -c ' + 63 `"(Get-Process -Id ${worker.process.pid}).MainWindowHandle"`, 64 { windowsHide: true, encoding: 'utf8' }); 65 } 66 67 process.send({ type: 'mainWindowHandle', value: output }); 68 worker.send('shutdown'); 69 }); 70 71} else { 72 cluster.worker.on('message', (msg) => { 73 cluster.worker.disconnect(); 74 }); 75} 76