| 1 | import * as Effect from "effect/Effect"; |
| 2 | import * as Semaphore from "effect/Semaphore"; |
| 3 | |
| 4 | /** |
| 5 | * The testing project allows 5 VPC networks, and `default` holds one. |
| 6 | * Tests that create their own network take a slot for their whole body |
| 7 | * (deploy through destroy), so a full `pnpm test test/GCP` run never |
| 8 | * exceeds the quota. One slot stays free for stacks that outlive a run |
| 9 | * (Cloud Run keeps Direct VPC egress subnets reserved for 1–2 hours after |
| 10 | * a service is deleted). |
| 11 | * |
| 12 | * Tests that only need *a* network should use the project's `default` |
| 13 | * network instead of creating one — the GCP counterpart of AWS tests |
| 14 | * reusing the default VPC. |
| 15 | */ |
| 16 | const slots = Semaphore.makeUnsafe(3); |
| 17 | |
| 18 | /** Run a network-creating test body while holding one VPC quota slot. */ |
| 19 | export const withNetworkSlot = <A, E, R>( |
| 20 | self: Effect.Effect<A, E, R>, |
| 21 | ): Effect.Effect<A, E, R> => slots.withPermits(1)(self); |
| 22 | |
| 23 | /** Like {@link withNetworkSlot}, for a test body that creates `count` networks. */ |
| 24 | export const withNetworkSlots = |
| 25 | (count: number) => |
| 26 | <A, E, R>(self: Effect.Effect<A, E, R>): Effect.Effect<A, E, R> => |
| 27 | slots.withPermits(count)(self); |
| 28 | |
| 29 | /** The project's pre-existing `default` network (auto-mode). */ |
| 30 | export const DEFAULT_NETWORK = "default"; |
| 31 | |
| 32 | /** Self link of the project's `default` network. */ |
| 33 | export const defaultNetworkSelfLink = (project: string) => |
| 34 | `https://www.googleapis.com/compute/v1/projects/${project}/global/networks/${DEFAULT_NETWORK}`; |