Add retry logic for flaky git operations

Fix #132218
This commit is contained in:
Rob Lourens 2021-10-12 17:08:11 -07:00
parent 3bf7202085
commit 83fec0220c
2 changed files with 24 additions and 5 deletions

View file

@ -6,18 +6,17 @@
import * as cp from 'child_process';
import minimist = require('minimist');
import { Application } from '../../../../automation';
import { afterSuite, beforeSuite } from '../../utils';
import { afterSuite, beforeSuite, retry } from '../../utils';
export function setup(opts: minimist.ParsedArgs) {
// https://github.com/microsoft/vscode/issues/115244
// https://github.com/microsoft/vscode/issues/132218
(process.platform === 'win32' ? describe.skip : describe)('Search', () => {
describe('Search', () => {
beforeSuite(opts);
after(function () {
const app = this.app as Application;
cp.execSync('git checkout . --quiet', { cwd: app.workspacePathOrFolder });
cp.execSync('git reset --hard HEAD --quiet', { cwd: app.workspacePathOrFolder });
retry(async () => cp.execSync('git checkout . --quiet', { cwd: app.workspacePathOrFolder }), 0, 5);
retry(async () => cp.execSync('git reset --hard HEAD --quiet', { cwd: app.workspacePathOrFolder }), 0, 5);
});
afterSuite(opts);

View file

@ -68,3 +68,23 @@ export function timeout(i: number) {
}, i);
});
}
export interface ITask<T> {
(): T;
}
export async function retry<T>(task: ITask<Promise<T>>, delay: number, retries: number): Promise<T> {
let lastError: Error | undefined;
for (let i = 0; i < retries; i++) {
try {
return await task();
} catch (error) {
lastError = error;
await timeout(delay);
}
}
throw lastError;
}