1import * as Effect from "effect/Effect";
2import * as Semaphore from "effect/Semaphore";
3
4// AWS permits only one ACCOUNT_UNUSED_ACCESS analyzer per account and Region.
5// The test runner executes files concurrently in one process, so hold this
6// semaphore across each suite's beforeAll → afterAll lifecycle.
7const accessAnalyzerTestLock = Semaphore.makeUnsafe(1);
8
9export const makeAccessAnalyzerTestLease = () => {
10 let held = false;
11
12 return {
13 acquire: Effect.uninterruptibleMask((restore) =>
14 restore(accessAnalyzerTestLock.take(1)).pipe(
15 Effect.tap(() =>
16 Effect.sync(() => {
17 held = true;
18 }),
19 ),
20 Effect.asVoid,
21 ),
22 ),
23 release: Effect.suspend(() => {
24 if (!held) return Effect.void;
25 held = false;
26 return accessAnalyzerTestLock.release(1).pipe(Effect.asVoid);
27 }),
28 };
29};