Skip to content

Commit bfcb90a

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

File tree

1 file changed

+41
-38
lines changed

1 file changed

+41
-38
lines changed

lib/chrome-driver.js

Lines changed: 41 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,32 @@ 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 (!this.process) {
62+
throw new Error('ChromeDriver has been stopped');
63+
}
64+
65+
// if (this.process.exitCode !== null) {
66+
// throw new Error(`ChromeDriver exited with code ${this.process.exitCode}`);
67+
// }
68+
69+
if (isRunning) {
70+
return;
71+
}
72+
73+
const elapsedTime = Date.now() - startTime;
74+
if (elapsedTime > this.startTimeout) {
75+
throw new Error(
76+
`ChromeDriver did not start within ${this.startTimeout}ms`
77+
);
78+
}
79+
80+
await delay(100);
81+
}
8182
};
8283

8384
ChromeDriver.prototype.setupLogs = function () {
@@ -120,12 +121,14 @@ ChromeDriver.prototype.stop = function () {
120121
this.clearLogs();
121122
};
122123

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));
124+
ChromeDriver.prototype.isRunning = async function () {
125+
try {
126+
const { value } = await got(this.statusUrl).json();
127+
return value && value.ready;
128+
} catch (err) {
129+
console.log('err', err);
130+
return false;
131+
}
129132
};
130133

131134
ChromeDriver.prototype.getLogs = function () {

0 commit comments

Comments
 (0)