Skip to content

Commit dc67289

Browse files
committed
ChromeDriver should fail fast if child process exits of its own accord
1 parent f4d097a commit dc67289

File tree

1 file changed

+38
-38
lines changed

1 file changed

+38
-38
lines changed

lib/chrome-driver.js

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ const path = require('path');
33
const { default: got } = require('got');
44
const split = require('split');
55

6+
function delay(duration) {
7+
return new Promise(resolve => global.setTimeout(resolve, duration));
8+
}
9+
610
function ChromeDriver(
711
host,
812
port,
@@ -20,19 +24,18 @@ function ChromeDriver(
2024

2125
this.path = require.resolve('electron-chromedriver/chromedriver');
2226
this.urlBase = '/';
23-
this.statusUrl =
24-
'http://' + this.host + ':' + this.port + this.urlBase + 'status';
27+
this.statusUrl = `http://${this.host}:${this.port}${this.urlBase}status`;
2528
this.logLines = [];
2629
}
2730

2831
ChromeDriver.prototype.start = function () {
2932
if (this.process) throw new Error('ChromeDriver already started');
3033

31-
const args = [this.path, '--port=' + this.port, '--url-base=' + this.urlBase];
34+
const args = [this.path, `--port=${this.port}`, `--url-base=${this.urlBase}`];
3235

3336
if (this.chromeDriverLogPath) {
3437
args.push('--verbose');
35-
args.push('--log-path=' + this.chromeDriverLogPath);
38+
args.push(`--log-path=${this.chromeDriverLogPath}`);
3639
}
3740
const options = {
3841
cwd: this.workingDirectory,
@@ -50,34 +53,30 @@ ChromeDriver.prototype.start = function () {
5053
return this.waitUntilRunning();
5154
};
5255

53-
ChromeDriver.prototype.waitUntilRunning = function () {
54-
const self = this;
55-
return new Promise(function (resolve, reject) {
56-
const startTime = Date.now();
57-
const checkIfRunning = function () {
58-
self.isRunning(function (running) {
59-
if (!self.process) {
60-
return reject(Error('ChromeDriver has been stopped'));
61-
}
62-
63-
if (running) {
64-
return resolve();
65-
}
66-
67-
const elapsedTime = Date.now() - startTime;
68-
if (elapsedTime > self.startTimeout) {
69-
return reject(
70-
Error(
71-
'ChromeDriver did not start within ' + self.startTimeout + 'ms'
72-
)
73-
);
74-
}
75-
76-
global.setTimeout(checkIfRunning, 100);
77-
});
78-
};
79-
checkIfRunning();
80-
});
56+
ChromeDriver.prototype.waitUntilRunning = async function () {
57+
const startTime = Date.now();
58+
for (;;) {
59+
const isRunning = await this.isRunning();
60+
61+
if (!self.process) {
62+
throw new Error('ChromeDriver has been stopped');
63+
}
64+
65+
if (self.process.exitCode !== null) {
66+
throw new Error(`ChromeDriver exited with code ${self.process.exitCode}`);
67+
}
68+
69+
if (isRunning) {
70+
return;
71+
}
72+
73+
const elapsedTime = Date.now() - startTime;
74+
if (elapsedTime > self.startTimeout) {
75+
throw new Error(`ChromeDriver did not start within ${self.startTimeout}ms`);
76+
}
77+
78+
await delay(100);
79+
}
8180
};
8281

8382
ChromeDriver.prototype.setupLogs = function () {
@@ -120,12 +119,13 @@ ChromeDriver.prototype.stop = function () {
120119
this.clearLogs();
121120
};
122121

123-
ChromeDriver.prototype.isRunning = function (callback) {
124-
const cb = false;
125-
got(this.statusUrl)
126-
.json()
127-
.then(({ value }) => callback(value && value.ready))
128-
.catch(() => callback(cb));
122+
ChromeDriver.prototype.isRunning = function () {
123+
try {
124+
const { value } = got(this.statusUrl).json();
125+
return value && value.ready;
126+
} catch (_) {
127+
return false;
128+
}
129129
};
130130

131131
ChromeDriver.prototype.getLogs = function () {

0 commit comments

Comments
 (0)