Use async/await in env var tests and improve polling

Fixes #120077
This commit is contained in:
Daniel Imms
2021-12-15 12:28:36 -08:00
parent 36bb6a6848
commit b562ce7067
2 changed files with 136 additions and 103 deletions

View File

@@ -4,6 +4,7 @@
*--------------------------------------------------------------------------------------------*/
import * as assert from 'assert';
import { EOL } from 'os';
import * as vscode from 'vscode';
import { TestFS } from './memfs';
@@ -149,3 +150,35 @@ export function suiteRepeat(n: number, description: string, callback: (this: any
suite(`${description} (iteration ${i})`, callback);
}
}
export async function poll<T>(
fn: () => Thenable<T>,
acceptFn: (result: T) => boolean,
timeoutMessage: string,
retryCount: number = 200,
retryInterval: number = 100 // millis
): Promise<T> {
let trial = 1;
let lastError: string = '';
while (true) {
if (trial > retryCount) {
throw new Error(`Timeout: ${timeoutMessage} after ${(retryCount * retryInterval) / 1000} seconds.\r${lastError}`);
}
let result;
try {
result = await fn();
if (acceptFn(result)) {
return result;
} else {
lastError = 'Did not pass accept function';
}
} catch (e: any) {
lastError = Array.isArray(e.stack) ? e.stack.join(EOL) : e.stack;
}
await new Promise(resolve => setTimeout(resolve, retryInterval));
trial++;
}
}