Open History to see the last commit
1/**
2 * Bound offline-oracle observation and exhaust test-owned cleanup.
3 *
4 * A checkpoint timeout says that an expected event was not observed; it does
5 * not cancel the underlying operation. Cleanup therefore tries every action.
6 * If the scenario already has a primary failure, cleanup failures are reported
7 * without replacing it. Otherwise they form one AggregateError after all
8 * resources have had a release attempt.
9 */
10export async function atOracleCheckpoint<T>(
11 promise: Promise<T>,
12 label: string,
13 timeout = 1000,
14): Promise<T> {
15 let timer: ReturnType<typeof setTimeout> | undefined
16 try {
17 return await Promise.race([
18 promise,
19 new Promise<never>((_resolve, reject) => {
20 timer = setTimeout(
21 () =>
22 reject(new Error(`Offline oracle checkpoint timed out: ${label}`)),
23 timeout,
24 )
25 }),
26 ])
27 } finally {
28 clearTimeout(timer)
29 }
30}
31
32export async function cleanupOfflineOracle(
33 actions: Array<() => void | Promise<unknown>>,
34 hasPrimaryFailure: boolean,
35): Promise<void> {
36 const failures: Array<unknown> = []
37 for (const [index, action] of actions.entries()) {
38 try {
39 await atOracleCheckpoint(
40 Promise.resolve().then(action),
41 `cleanup stage ${index}`,
42 250,
43 )
44 } catch (error) {
45 failures.push(error)
46 }
47 }
48 if (failures.length > 0) {
49 if (hasPrimaryFailure)
50 console.warn(
51 `Offline oracle cleanup failed after the primary failure:`,
52 failures,
53 )
54 else throw new AggregateError(failures, `Offline oracle cleanup failed`)
55 }
56}