[ftr/leadfoot] retry session creation before timeout

This commit is contained in:
spalger 2017-06-19 12:48:19 -07:00
parent 72fe011ca9
commit a736754f14

View file

@ -4,7 +4,30 @@ import Server from 'leadfoot/Server';
import { initVerboseRemoteLogging } from './verbose_remote_logging';
const MINUTE = 1000 * 60;
const SECOND = 1000;
const MINUTE = 60 * SECOND;
let attemptCounter = 0;
async function attemptToCreateCommand(log, server, chromedriverApi) {
const attemptId = ++attemptCounter;
log.debug('[leadfoot:command] Creating session');
const session = await server.createSession({ browserName: 'chrome' });
if (attemptId !== attemptCounter) return; // abort
log.debug('[leadfoot:command] Registerying session for teardown');
chromedriverApi.beforeStop(async () => session.quit());
if (attemptId !== attemptCounter) return; // abort
log.debug('[leadfoot:command] Completing session capabilities');
await server._fillCapabilities(session);
if (attemptId !== attemptCounter) return; // abort
// command looks like a promise beacuse it has a `.then()` function
// so we wrap it in an object to prevent async/await from trying to
// unwrap/resolve it
return { command: new Command(session) };
}
export async function initLeadfootCommand({ log, chromedriverApi }) {
return await Promise.race([
@ -32,14 +55,19 @@ export async function initLeadfootCommand({ log, chromedriverApi }) {
// to the session and have registered it for teardown before stopping the
// chromedriverApi.
server.fixSessionCapabilities = false;
const session = await server.createSession({ browserName: 'chrome' });
chromedriverApi.beforeStop(async () => session.quit());
await server._fillCapabilities(session);
// command looks like a promise beacuse it has a `.then()` function
// so we wrap it in an object to prevent async/await from trying to
// unwrap/resolve it
return { command: new Command(session) };
while (true) {
const command = await Promise.race([
delay(30 * SECOND),
attemptToCreateCommand(server, chromedriverApi)
]);
if (!command) {
continue;
}
return command;
}
})()
]);
}