test: deflake crashReporter.getLastCrashReport test (#30276)

This commit is contained in:
Jeremy Rose 2021-07-28 08:56:15 -07:00 committed by GitHub
parent d63980edeb
commit fa464286ee
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 2 deletions

View file

@ -3,7 +3,7 @@ import * as childProcess from 'child_process';
import * as http from 'http'; import * as http from 'http';
import * as Busboy from 'busboy'; import * as Busboy from 'busboy';
import * as path from 'path'; 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 { app } from 'electron/main';
import { crashReporter } from 'electron/common'; import { crashReporter } from 'electron/common';
import { AddressInfo } from 'net'; import { AddressInfo } from 'net';
@ -401,7 +401,9 @@ ifdescribe(!isLinuxOnArm && !process.mas && !process.env.DISABLE_CRASH_REPORTER_
}); });
await waitForCrash(); await waitForCrash();
// 3. get the crash from getLastCrashReport. // 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).to.not.be.null();
expect(firstReport.date).to.be.an.instanceOf(Date); expect(firstReport.date).to.be.an.instanceOf(Date);
expect((+new Date()) - (+firstReport.date)).to.be.lessThan(30000); expect((+new Date()) - (+firstReport.date)).to.be.lessThan(30000);

View file

@ -132,3 +132,16 @@ export function waitUntil (
}, timeout); }, 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})`); }
}
}