Add deterministic tests
This commit is contained in:
parent
6fbbd1e12f
commit
0ce82353e0
20 changed files with 1780 additions and 0 deletions
33
frontend/deterministic-tests/src/run-with-concurrency.ts
Normal file
33
frontend/deterministic-tests/src/run-with-concurrency.ts
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
export async function runWithConcurrency<T, R>(
|
||||
items: T[],
|
||||
concurrency: number,
|
||||
fn: (item: T) => Promise<R>
|
||||
): Promise<R[]> {
|
||||
const results: R[] = [];
|
||||
const errors: unknown[] = [];
|
||||
const executing = new Set<Promise<void>>();
|
||||
|
||||
for (let i = 0; i < items.length; i++) {
|
||||
const index = i;
|
||||
const p = fn(items[index])
|
||||
.then((result) => {
|
||||
results[index] = result;
|
||||
})
|
||||
.catch((error: unknown) => {
|
||||
errors.push(error);
|
||||
})
|
||||
.finally(() => executing.delete(p));
|
||||
executing.add(p);
|
||||
if (executing.size >= concurrency) {
|
||||
await Promise.race(executing);
|
||||
}
|
||||
}
|
||||
|
||||
// eslint-disable-next-line no-restricted-properties
|
||||
await Promise.all(executing);
|
||||
|
||||
if (errors.length > 0) {
|
||||
throw errors[0];
|
||||
}
|
||||
return results;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue