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

LiveStreamBudget.test.ts

47 lines2.1 KB

1import { it } from "@effect/vitest";
2import * as Deferred from "effect/Deferred";
3import * as Effect from "effect/Effect";
4import * as Queue from "effect/Queue";
5import * as Stream from "effect/Stream";
6import { describe, expect } from "vite-plus/test";
7​
8import { makeLiveStreamBudget, type RetainedLiveItem } from "./LiveStreamBudget.ts";
9​
10describe("LiveStreamBudget", () => {
11 it.effect("closes the source without releasing a batch still waiting for an ACK", () =>
12 Effect.scoped(
13 Effect.gen(function* () {
14 const budget = yield* makeLiveStreamBudget({ maxItems: 3 });
15 const queue = yield* Queue.unbounded<RetainedLiveItem<{ text: string }>>();
16 const sourceClosed = yield* Deferred.make<void>();
17 const first = yield* budget.retain({ text: "first" });
18 const second = yield* budget.retain({ text: "second" });
19 const third = yield* budget.retain({ text: "third" });
20 yield* Queue.offerAll(queue, [first, second, third]);
21 yield* Effect.scoped(
22 Effect.gen(function* () {
23 const pull = yield* Stream.toPull(
24 budget.deliver(
25 Stream.fromQueue(queue).pipe(
26 Stream.rechunk(1),
27 Stream.ensuring(Deferred.succeed(sourceClosed, undefined)),
28 ),
29 ),
30 );
31 expect(yield* pull).toEqual([{ text: "first" }]);
32 // The other two items are in the source's pull state, not its queue.
33 expect(yield* Queue.size(queue)).toBe(0);
34 expect((yield* budget.usage).retainedItems).toBe(3);
35 const overflow = yield* budget.retain({ text: "fourth" }).pipe(Effect.result);
36 expect(overflow._tag).toBe("Failure");
37 // Do not resume the consumer. Its source scope must close now.
38 yield* Deferred.await(sourceClosed);
39 yield* budget.closed;
40 expect((yield* budget.usage).retainedItems).toBe(1);
41 }),
42 );
43 expect(yield* budget.usage).toEqual({ retainedItems: 0, retainedSerializedBytes: 0 });
44 }),
45 ),
46 );
47});