Co-authored-by: codex <[email protected]> Co-authored-by: Cursor Agent <[email protected]>
50 lines
1.6 KiB
TypeScript
50 lines
1.6 KiB
TypeScript
import { assert, it } from "@effect/vitest";
|
|
import { Deferred, Effect, Fiber, Ref } from "effect";
|
|
import { TestClock } from "effect/testing";
|
|
|
|
import { makeCommandGate, ServerRuntimeStartupError } from "./serverRuntimeStartup.ts";
|
|
|
|
it.effect("enqueueCommand waits for readiness and then drains queued work", () =>
|
|
Effect.scoped(
|
|
Effect.gen(function* () {
|
|
const executionCount = yield* Ref.make(0);
|
|
const commandGate = yield* makeCommandGate;
|
|
|
|
const queuedCommandFiber = yield* commandGate
|
|
.enqueueCommand(Ref.updateAndGet(executionCount, (count) => count + 1))
|
|
.pipe(Effect.forkScoped);
|
|
|
|
yield* TestClock.adjust("50 millis");
|
|
assert.equal(yield* Ref.get(executionCount), 0);
|
|
|
|
yield* commandGate.signalCommandReady;
|
|
|
|
const result = yield* Fiber.join(queuedCommandFiber);
|
|
assert.equal(result, 1);
|
|
assert.equal(yield* Ref.get(executionCount), 1);
|
|
}),
|
|
),
|
|
);
|
|
|
|
it.effect("enqueueCommand fails queued work when readiness fails", () =>
|
|
Effect.scoped(
|
|
Effect.gen(function* () {
|
|
const commandGate = yield* makeCommandGate;
|
|
const failure = yield* Deferred.make<void, never>();
|
|
|
|
const queuedCommandFiber = yield* commandGate
|
|
.enqueueCommand(Deferred.await(failure).pipe(Effect.as("should-not-run")))
|
|
.pipe(Effect.forkScoped);
|
|
|
|
yield* commandGate.failCommandReady(
|
|
new ServerRuntimeStartupError({
|
|
message: "startup failed",
|
|
}),
|
|
);
|
|
|
|
const error = yield* Effect.flip(Fiber.join(queuedCommandFiber));
|
|
assert.equal(error.message, "startup failed");
|
|
}),
|
|
),
|
|
);
|