Browse Source

test: deflake crashReporter.getLastCrashReport test (#30276)

Jeremy Rose 3 years ago
parent
commit
fa464286ee
2 changed files with 17 additions and 2 deletions
  1. 4 2
      spec-main/api-crash-reporter-spec.ts
  2. 13 0
      spec-main/spec-helpers.ts

+ 4 - 2
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);

+ 13 - 0
spec-main/spec-helpers.ts

@@ -132,3 +132,16 @@ export function waitUntil (
     }, timeout);
   });
 }
+
+export async function repeatedly<T> (
+  fn: () => Promise<T>,
+  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})`); }
+  }
+}