From fa464286ee10c091295b9d222e2845256d84332b Mon Sep 17 00:00:00 2001 From: Jeremy Rose Date: Wed, 28 Jul 2021 08:56:15 -0700 Subject: [PATCH] test: deflake crashReporter.getLastCrashReport test (#30276) --- spec-main/api-crash-reporter-spec.ts | 6 ++++-- spec-main/spec-helpers.ts | 13 +++++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/spec-main/api-crash-reporter-spec.ts b/spec-main/api-crash-reporter-spec.ts index 3bdc528e7d3e..8f8f6af7dd0d 100644 --- a/spec-main/api-crash-reporter-spec.ts +++ b/spec-main/api-crash-reporter-spec.ts @@ -3,7 +3,7 @@ import * as childProcess from 'child_process'; import * as http from 'http'; import * as Busboy from 'busboy'; import * as path from 'path'; -import { ifdescribe, ifit, defer, startRemoteControlApp, delay } from './spec-helpers'; +import { ifdescribe, ifit, defer, startRemoteControlApp, delay, repeatedly } from './spec-helpers'; import { app } from 'electron/main'; import { crashReporter } from 'electron/common'; import { AddressInfo } from 'net'; @@ -401,7 +401,9 @@ ifdescribe(!isLinuxOnArm && !process.mas && !process.env.DISABLE_CRASH_REPORTER_ }); await waitForCrash(); // 3. get the crash from getLastCrashReport. - const firstReport = await remotely(() => require('electron').crashReporter.getLastCrashReport()); + const firstReport = await repeatedly( + () => remotely(() => require('electron').crashReporter.getLastCrashReport()) + ); expect(firstReport).to.not.be.null(); expect(firstReport.date).to.be.an.instanceOf(Date); expect((+new Date()) - (+firstReport.date)).to.be.lessThan(30000); diff --git a/spec-main/spec-helpers.ts b/spec-main/spec-helpers.ts index c644f402b537..434f68636f20 100644 --- a/spec-main/spec-helpers.ts +++ b/spec-main/spec-helpers.ts @@ -132,3 +132,16 @@ export function waitUntil ( }, timeout); }); } + +export async function repeatedly ( + fn: () => Promise, + opts?: { until?: (x: T) => boolean, timeLimit?: number } +) { + const { until = (x: T) => !!x, timeLimit = 10000 } = opts ?? {}; + const begin = +new Date(); + while (true) { + const ret = await fn(); + if (until(ret)) { return ret; } + if (+new Date() - begin > timeLimit) { throw new Error(`repeatedly timed out (limit=${timeLimit})`); } + } +}