describe('child_process', function() { it('should works in child_process', cb => { let cmd = fork(path.resolve(__dirname, './support/child_process.js'), [], { silent: true, }); let stdout = ''; let stderr = ''; cmd.stdout.on('data', buf => { let data = buf.toString(); stdout += data; if (data.includes('color')) cmd.stdin.write('orange\n'); }); cmd.stderr.on('data', buf => { stderr += buf.toString(); }); cmd.on('close', () => { assert(!stderr); assert(stdout.includes('color: \'orange\'')); cb(); }); }); });
it('restart chrome when chrome exit unexpected', async function () { this.timeout(8000); const runner = await launchWithHeadless(); process.kill(runner.chromeProcess.pid); await delay(2000); process.kill(runner.chromeProcess.pid); await delay(2000); process.kill(runner.chromeProcess.pid); await delay(2000); await runner.kill(); });
// exec chrome, resolves when debugger is ready async spawn() { if (!this.port) { this.port = await getRandomPort(); } const chromeProcess = childProcess.spawn(this.chromePath, this.flags, { detached: true, stdio: this.disableLogging ? ['ignore', 'ignore', 'ignore'] : ['ignore', this.chromeOutFd, this.chromeErrorFd] }); this.chromeProcess = chromeProcess; this.monitorChromeIsAlive(); this.handleProcessEvent(); fs.writeFileSync(this.pidFd, chromeProcess.pid.toString()); return new Promise((resolve) => { this.once('chromeAlive', resolve); }); }
monitorChromeIsAlive() { clearInterval(this.monitorTimmer); this.monitorTimmer = setInterval(async () => { const alive = await isPortOpen(this.port); if (alive) { this.emit('chromeAlive', this.port); } else { this.emit('chromeDead'); if (this.shouldRestartChrome) { if (this.chromeProcess) { this.chromeProcess.kill(); } await this.spawn(); this.emit('chromeRestarted'); } } }, this.monitorInterval); }
kill() { return new Promise((resolve, reject) => { if (this.chromeProcess) { this.chromeProcess.on('close', () => { this.destroyChromeDataDir(); resolve(); }); try { this.shouldRestartChrome = false; this.chromeProcess.kill(); delete this.chromeProcess; clearInterval(this.monitorTimmer); delete this.monitorTimmer; } catch (err) { reject(err); } } else { // fail silently as we did not start chrome resolve(); } }); }
it('should emit chromeAlive event then chromeRestarted event when chrome exit unexpected', function (done) { launchWithHeadless().then((runner) => { let chromeDead; runner.once('chromeDead', async () => { chromeDead = true; }); runner.once('chromeRestarted', async () => { if (chromeDead) { await runner.kill(); done(); } }); process.kill(runner.chromeProcess.pid); }); });