Skip to content

Commit

Permalink
TINY-11177: Make sure there is at least some interaction with the rem…
Browse files Browse the repository at this point in the history
…ote driver once every 4 minutes
  • Loading branch information
TheSpyder committed Jan 17, 2025
1 parent d018041 commit 81e54aa
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
4 changes: 3 additions & 1 deletion modules/server/src/main/ts/bedrock/auto/RemoteDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import * as deepmerge from 'deepmerge';
import { DeviceFarmClient, CreateTestGridUrlCommand } from '@aws-sdk/client-device-farm';
import { Driver, DriverSettings } from './Driver';

export const REMOTE_IDLE_TIMEOUT_SECONDS = 360;

const getFarmUrl = async (awsRegion: string, projectArn: string, expires = 5000): Promise<URL> => {
console.log('Creating DeviceFarmClient...');
const client = new DeviceFarmClient({region: awsRegion});
Expand Down Expand Up @@ -101,7 +103,7 @@ const addDriverSpecificOpts = (opts: WebdriverIO.RemoteOptions, settings: Driver
'LT:Options': {
username: settings.username,
accesskey: settings.accesskey,
idleTimeout: '360',
idleTimeout: REMOTE_IDLE_TIMEOUT_SECONDS,
tunnel: true,
console: true,
w3c: true,
Expand Down
24 changes: 22 additions & 2 deletions modules/server/src/main/ts/bedrock/server/Apis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { DriverMaster } from './DriverMaster';
import * as KeyEffects from './KeyEffects';
import * as MouseEffects from './MouseEffects';
import * as Routes from './Routes';
import {REMOTE_IDLE_TIMEOUT_SECONDS} from '../auto/RemoteDriver';

type Executor<D, T> = (driver: Browser) => (data: D) => Promise<T>;

Expand Down Expand Up @@ -75,7 +76,7 @@ export const create = (master: DriverMaster | null, maybeDriver: Attempt<any, Br
};
};

const sendKeepAlive = (driver: Browser) => Promise.resolve(void driver.execute(() => console.info('keep-alive', Date.now())));
const sendKeepAlive = (driver: Browser) => Promise.resolve(void driver.execute(() => console.info('server keep-alive', Date.now())));

const keepAliveAction = () => Attempt.cata(maybeDriver, () => Promise.resolve(),
(driver) => waitForDriverReady(maxInvalidAttempts, () => sendKeepAlive(driver)));
Expand Down Expand Up @@ -123,6 +124,25 @@ export const create = (master: DriverMaster | null, maybeDriver: Attempt<any, Br

const c = Controller.create(stickyFirstSession, overallTimeout, testfiles, loglevel);

const maybeSendKeepAlive: () => Promise<void> = (() => {
let lastKeepAlive = Date.now();
/*
* If we're within a minute of the remote driver timeout, send a keep-alive.
*
* In theory this is only required if no other action has been taken in the last x minutes,
* but this happens so rarely it should be fine.
*/
const keepAliveTimer = (Math.max(120, REMOTE_IDLE_TIMEOUT_SECONDS - 60)) * 1000;
return () => {
if (Date.now() - lastKeepAlive > keepAliveTimer) {
lastKeepAlive = Date.now();
return keepAliveAction();
} else {
return Promise.resolve();
}
};
})();

const routers = [
driverRouter('/keys', 'Keys', KeyEffects.executor, false),
driverRouter('/mouse', 'Mouse', MouseEffects.executor, true),
Expand All @@ -137,7 +157,7 @@ export const create = (master: DriverMaster | null, maybeDriver: Attempt<any, Br
}),
Routes.effect('POST', '/tests/results', (data: ResultsData) => {
c.recordTestResults(data.session, data.results);
return Promise.resolve();
return maybeSendKeepAlive();
}),
Routes.effect('POST', '/tests/done', (data: DoneData) => {
Coverage.writeCoverageData(data.coverage);
Expand Down

0 comments on commit 81e54aa

Please sign in to comment.