2017-12-01 16:08:01 +00:00
|
|
|
'use strict';
|
|
|
|
const common = require('../common');
|
|
|
|
const assert = require('assert');
|
|
|
|
const child_process = require('child_process');
|
|
|
|
const cluster = require('cluster');
|
|
|
|
|
|
|
|
if (!process.argv[2]) {
|
2020-10-03 20:01:57 +00:00
|
|
|
// It seems Windows only allocate new console window for
|
|
|
|
// attaching processes spawned by detached processes. i.e.
|
|
|
|
// - If process D is spawned by process C with `detached: true`,
|
|
|
|
// and process W is spawned by process D with `detached: false`,
|
|
|
|
// W will get a new black console window popped up.
|
|
|
|
// - If D is spawned by C with `detached: false` or W is spawned
|
|
|
|
// by D with `detached: true`, no console window will pop up for W.
|
|
|
|
//
|
|
|
|
// So, we have to spawn a detached process first to run the actual test.
|
2020-12-10 21:53:44 +00:00
|
|
|
const primary = child_process.spawn(
|
2017-12-01 16:08:01 +00:00
|
|
|
process.argv[0],
|
|
|
|
[process.argv[1], '--cluster'],
|
|
|
|
{ detached: true, stdio: ['ignore', 'ignore', 'ignore', 'ipc'] });
|
|
|
|
|
|
|
|
const messageHandlers = {
|
2022-10-20 13:13:32 +00:00
|
|
|
workerOnline: common.mustCall(),
|
2017-12-01 16:08:01 +00:00
|
|
|
mainWindowHandle: common.mustCall((msg) => {
|
2021-08-29 08:14:22 +00:00
|
|
|
assert.match(msg.value, /0\s*/);
|
2017-12-01 16:08:01 +00:00
|
|
|
}),
|
|
|
|
workerExit: common.mustCall((msg) => {
|
|
|
|
assert.strictEqual(msg.code, 0);
|
|
|
|
assert.strictEqual(msg.signal, null);
|
|
|
|
})
|
|
|
|
};
|
|
|
|
|
2020-12-10 21:53:44 +00:00
|
|
|
primary.on('message', (msg) => {
|
2017-12-01 16:08:01 +00:00
|
|
|
const handler = messageHandlers[msg.type];
|
|
|
|
assert.ok(handler);
|
|
|
|
handler(msg);
|
|
|
|
});
|
|
|
|
|
2020-12-10 21:53:44 +00:00
|
|
|
primary.on('exit', common.mustCall((code, signal) => {
|
2017-12-01 16:08:01 +00:00
|
|
|
assert.strictEqual(code, 0);
|
|
|
|
assert.strictEqual(signal, null);
|
|
|
|
}));
|
|
|
|
|
2020-12-10 21:53:44 +00:00
|
|
|
} else if (cluster.isPrimary) {
|
|
|
|
cluster.setupPrimary({
|
2018-10-07 01:09:29 +00:00
|
|
|
silent: true,
|
2017-12-01 16:08:01 +00:00
|
|
|
windowsHide: true
|
|
|
|
});
|
|
|
|
|
|
|
|
const worker = cluster.fork();
|
|
|
|
worker.on('exit', (code, signal) => {
|
|
|
|
process.send({ type: 'workerExit', code: code, signal: signal });
|
|
|
|
});
|
|
|
|
|
|
|
|
worker.on('online', (msg) => {
|
|
|
|
process.send({ type: 'workerOnline' });
|
|
|
|
|
|
|
|
let output = '0';
|
|
|
|
if (process.platform === 'win32') {
|
|
|
|
output = child_process.execSync(
|
|
|
|
'powershell -NoProfile -c ' +
|
|
|
|
`"(Get-Process -Id ${worker.process.pid}).MainWindowHandle"`,
|
|
|
|
{ windowsHide: true, encoding: 'utf8' });
|
|
|
|
}
|
|
|
|
|
|
|
|
process.send({ type: 'mainWindowHandle', value: output });
|
|
|
|
worker.send('shutdown');
|
|
|
|
});
|
|
|
|
|
|
|
|
} else {
|
|
|
|
cluster.worker.on('message', (msg) => {
|
|
|
|
cluster.worker.disconnect();
|
|
|
|
});
|
|
|
|
}
|