Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

ProviderInstallationGate.test.ts

49 lines1.9 KB

1import { ProviderInstanceId } from "@t3tools/contracts";
2import { assert, it } from "@effect/vitest";
3import * as Deferred from "effect/Deferred";
4import * as Effect from "effect/Effect";
5import * as Fiber from "effect/Fiber";
6​
7import * as ProviderInstallationGate from "./ProviderInstallationGate.ts";
8​
9const INSTANCE_ID = ProviderInstanceId.make("codex");
10​
11it.effect("queues work that starts while an update waits for in-flight work", () =>
12 Effect.gen(function* () {
13 const gate = yield* ProviderInstallationGate.ProviderInstallationGate;
14 const inFlightStarted = yield* Deferred.make<void>();
15 const releaseInFlight = yield* Deferred.make<void>();
16 const log: Array<string> = [];
17​
18 const inFlight = yield* gate
19 .execute(
20 INSTANCE_ID,
21 Deferred.succeed(inFlightStarted, undefined).pipe(
22 Effect.andThen(Deferred.await(releaseInFlight)),
23 Effect.andThen(Effect.sync(() => log.push("in-flight work finished"))),
24 ),
25 )
26 .pipe(Effect.forkScoped);
27 yield* Deferred.await(inFlightStarted);
28 // Starting immediately runs each fiber until it blocks: the update while draining in-flight
29 // work, by which point it holds the turnstile, and later work at the gate.
30 const update = yield* gate
31 .update(
32 [INSTANCE_ID],
33 Effect.sync(() => log.push("update ran")),
34 )
35 .pipe(Effect.forkScoped({ startImmediately: true }));
36 const later = yield* gate
37 .execute(
38 INSTANCE_ID,
39 Effect.sync(() => log.push("later work ran")),
40 )
41 .pipe(Effect.forkScoped({ startImmediately: true }));
42 yield* Deferred.succeed(releaseInFlight, undefined);
43 yield* Fiber.join(inFlight);
44 yield* Fiber.join(update);
45 yield* Fiber.join(later);
46​
47 assert.deepStrictEqual(log, ["in-flight work finished", "update ran", "later work ran"]);
48 }).pipe(Effect.provide(ProviderInstallationGate.layer)),
49);