Open History to see the last commit
1import { assert, it } from "@effect/vitest";
2import { Deferred, Effect, Fiber, Option, Ref } from "effect";
3
4import { AnalyticsService } from "./telemetry/Services/AnalyticsService.ts";
5import { ProjectionSnapshotQuery } from "./orchestration/Services/ProjectionSnapshotQuery.ts";
6import {
7 launchStartupHeartbeat,
8 makeCommandGate,
9 ServerRuntimeStartupError,
10} from "./serverRuntimeStartup.ts";
11
12it.effect("enqueueCommand waits for readiness and then drains queued work", () =>
13 Effect.scoped(
14 Effect.gen(function* () {
15 const executionCount = yield* Ref.make(0);
16 const commandGate = yield* makeCommandGate;
17
18 const queuedCommandFiber = yield* commandGate
19 .enqueueCommand(Ref.updateAndGet(executionCount, (count) => count + 1))
20 .pipe(Effect.forkScoped);
21
22 yield* Effect.yieldNow;
23 assert.equal(yield* Ref.get(executionCount), 0);
24
25 yield* commandGate.signalCommandReady;
26
27 const result = yield* Fiber.join(queuedCommandFiber);
28 assert.equal(result, 1);
29 assert.equal(yield* Ref.get(executionCount), 1);
30 }),
31 ),
32);
33
34it.effect("enqueueCommand fails queued work when readiness fails", () =>
35 Effect.scoped(
36 Effect.gen(function* () {
37 const commandGate = yield* makeCommandGate;
38 const failure = yield* Deferred.make<void, never>();
39
40 const queuedCommandFiber = yield* commandGate
41 .enqueueCommand(Deferred.await(failure).pipe(Effect.as("should-not-run")))
42 .pipe(Effect.forkScoped);
43
44 yield* commandGate.failCommandReady(
45 new ServerRuntimeStartupError({
46 message: "startup failed",
47 }),
48 );
49
50 const error = yield* Effect.flip(Fiber.join(queuedCommandFiber));
51 assert.equal(error.message, "startup failed");
52 }),
53 ),
54);
55
56it.effect("launchStartupHeartbeat does not block the caller while counts are loading", () =>
57 Effect.scoped(
58 Effect.gen(function* () {
59 const releaseCounts = yield* Deferred.make<void, never>();
60
61 yield* launchStartupHeartbeat.pipe(
62 Effect.provideService(ProjectionSnapshotQuery, {
63 getSnapshot: () => Effect.die("unused"),
64 getCounts: () =>
65 Deferred.await(releaseCounts).pipe(
66 Effect.as({
67 projectCount: 2,
68 threadCount: 3,
69 }),
70 ),
71 getActiveProjectByWorkspaceRoot: () => Effect.succeed(Option.none()),
72 getFirstActiveThreadIdByProjectId: () => Effect.succeed(Option.none()),
73 getThreadCheckpointContext: () => Effect.succeed(Option.none()),
74 }),
75 Effect.provideService(AnalyticsService, {
76 record: () => Effect.void,
77 flush: Effect.void,
78 }),
79 );
80 }),
81 ),
82);