From d6cc067507c2cbbf34d853e96ce02bd21a3d3dc0 Mon Sep 17 00:00:00 2001 From: Fedor Indutny <79877362+indutny-signal@users.noreply.github.com> Date: Mon, 18 Sep 2023 22:09:28 +0200 Subject: [PATCH] Auto-retry starting app in mock tests --- ts/test-mock/bootstrap.ts | 42 ++++++++++++++++++++++++++------------- 1 file changed, 28 insertions(+), 14 deletions(-) diff --git a/ts/test-mock/bootstrap.ts b/ts/test-mock/bootstrap.ts index faf9f31e2ee7..86e88939740f 100644 --- a/ts/test-mock/bootstrap.ts +++ b/ts/test-mock/bootstrap.ts @@ -63,6 +63,8 @@ for (const firstName of CONTACT_FIRST_NAMES) { const MAX_CONTACTS = CONTACT_NAMES.length; +const DEFAULT_START_APP_TIMEOUT = 10 * durations.SECOND; + export type BootstrapOptions = Readonly<{ extraConfig?: Record; benchmark?: boolean; @@ -260,7 +262,7 @@ export class Bootstrap { await app.close(); } - public async startApp(): Promise { + public async startApp(timeout = DEFAULT_START_APP_TIMEOUT): Promise { assert( this.storagePath !== undefined, 'Bootstrap has to be initialized first, see: bootstrap.init()' @@ -269,21 +271,33 @@ export class Bootstrap { debug('starting the app'); const { port } = this.server.address(); + const config = await this.generateConfig(port); - const app = new App({ - main: ELECTRON, - args: [CI_SCRIPT], - config: await this.generateConfig(port), - }); - - await app.start(); - - this.lastApp = app; - app.on('close', () => { - if (this.lastApp === app) { - this.lastApp = undefined; + let app: App | undefined; + while (!app) { + const startedApp = new App({ + main: ELECTRON, + args: [CI_SCRIPT], + config, + }); + try { + // eslint-disable-next-line no-await-in-loop + await pTimeout(startedApp.start(), timeout); + } catch (error) { + // eslint-disable-next-line no-console + console.error('Failed to start the app on time, retrying', error); + continue; } - }); + + this.lastApp = startedApp; + startedApp.on('close', () => { + if (this.lastApp === startedApp) { + this.lastApp = undefined; + } + }); + + app = startedApp; + } return app; }