From bfcb90a4b01042073def668170942a6d06af8269 Mon Sep 17 00:00:00 2001 From: Sebastian Malton Date: Mon, 8 Feb 2021 16:03:23 -0500 Subject: [PATCH] ChromeDriver should fail fast if child process exits of its own accord --- lib/chrome-driver.js | 79 +++++++++++++++++++++++--------------------- 1 file changed, 41 insertions(+), 38 deletions(-) diff --git a/lib/chrome-driver.js b/lib/chrome-driver.js index e1d167137..857e03268 100644 --- a/lib/chrome-driver.js +++ b/lib/chrome-driver.js @@ -3,6 +3,10 @@ const path = require('path'); const { default: got } = require('got'); const split = require('split'); +function delay(duration) { + return new Promise((resolve) => global.setTimeout(resolve, duration)); +} + function ChromeDriver( host, port, @@ -20,19 +24,18 @@ function ChromeDriver( this.path = require.resolve('electron-chromedriver/chromedriver'); this.urlBase = '/'; - this.statusUrl = - 'http://' + this.host + ':' + this.port + this.urlBase + 'status'; + this.statusUrl = `http://${this.host}:${this.port}${this.urlBase}status`; this.logLines = []; } ChromeDriver.prototype.start = function () { if (this.process) throw new Error('ChromeDriver already started'); - const args = [this.path, '--port=' + this.port, '--url-base=' + this.urlBase]; + const args = [this.path, `--port=${this.port}`, `--url-base=${this.urlBase}`]; if (this.chromeDriverLogPath) { args.push('--verbose'); - args.push('--log-path=' + this.chromeDriverLogPath); + args.push(`--log-path=${this.chromeDriverLogPath}`); } const options = { cwd: this.workingDirectory, @@ -50,34 +53,32 @@ ChromeDriver.prototype.start = function () { return this.waitUntilRunning(); }; -ChromeDriver.prototype.waitUntilRunning = function () { - const self = this; - return new Promise(function (resolve, reject) { - const startTime = Date.now(); - const checkIfRunning = function () { - self.isRunning(function (running) { - if (!self.process) { - return reject(Error('ChromeDriver has been stopped')); - } - - if (running) { - return resolve(); - } - - const elapsedTime = Date.now() - startTime; - if (elapsedTime > self.startTimeout) { - return reject( - Error( - 'ChromeDriver did not start within ' + self.startTimeout + 'ms' - ) - ); - } - - global.setTimeout(checkIfRunning, 100); - }); - }; - checkIfRunning(); - }); +ChromeDriver.prototype.waitUntilRunning = async function () { + const startTime = Date.now(); + for (;;) { + const isRunning = await this.isRunning(); + + if (!this.process) { + throw new Error('ChromeDriver has been stopped'); + } + + // if (this.process.exitCode !== null) { + // throw new Error(`ChromeDriver exited with code ${this.process.exitCode}`); + // } + + if (isRunning) { + return; + } + + const elapsedTime = Date.now() - startTime; + if (elapsedTime > this.startTimeout) { + throw new Error( + `ChromeDriver did not start within ${this.startTimeout}ms` + ); + } + + await delay(100); + } }; ChromeDriver.prototype.setupLogs = function () { @@ -120,12 +121,14 @@ ChromeDriver.prototype.stop = function () { this.clearLogs(); }; -ChromeDriver.prototype.isRunning = function (callback) { - const cb = false; - got(this.statusUrl) - .json() - .then(({ value }) => callback(value && value.ready)) - .catch(() => callback(cb)); +ChromeDriver.prototype.isRunning = async function () { + try { + const { value } = await got(this.statusUrl).json(); + return value && value.ready; + } catch (err) { + console.log('err', err); + return false; + } }; ChromeDriver.prototype.getLogs = function () {