1import * as Effect from "effect/Effect";
2import * as Semaphore from "effect/Semaphore";
3
4// GuardDuty permits one detector per account and Region. The lifecycle,
5// detector-resource, and bindings files all create that singleton while
6// alchemy-test executes files concurrently in one process. Hold this lease
7// from each file's root beforeAll through its root afterAll so detector
8// observation, fixture setup, and cleanup cannot overlap across the files.
9const guardDutyTestLock = Semaphore.makeUnsafe(1);
10
11export const makeGuardDutyTestLease = () => {
12 let held = false;
13
14 return {
15 acquire: Effect.uninterruptibleMask((restore) =>
16 restore(guardDutyTestLock.take(1)).pipe(
17 Effect.tap(() =>
18 Effect.sync(() => {
19 held = true;
20 }),
21 ),
22 Effect.asVoid,
23 ),
24 ),
25 release: Effect.suspend(() => {
26 if (!held) return Effect.void;
27 held = false;
28 return guardDutyTestLock.release(1).pipe(Effect.asVoid);
29 }),
30 };
31};