Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions packages/shared/src/DrainableWorker.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { it } from "@effect/vitest";
import { describe, expect } from "vite-plus/test";
import * as Deferred from "effect/Deferred";
import * as Effect from "effect/Effect";
import * as Exit from "effect/Exit";
import * as Scope from "effect/Scope";

import { makeDrainableWorker } from "./DrainableWorker.ts";

Expand Down Expand Up @@ -54,4 +56,22 @@ describe("makeDrainableWorker", () => {
}),
),
);

it.live("drain does not hang on work enqueued after the worker shut down", () =>
Effect.gen(function* () {
const scope = yield* Scope.make();
const worker = yield* makeDrainableWorker((_item: string) => Effect.void).pipe(
Scope.provide(scope),
);

yield* Scope.close(scope, Exit.void);

// The queue is shut down: the offer drops the item and reports false.
// The dropped item must not count toward drain, or drain hangs forever.
yield* worker.enqueue("late");

const drained = yield* worker.drain.pipe(Effect.timeout("2 seconds"), Effect.as("drained"));
expect(drained).toBe("drained");
}),
);
});
7 changes: 5 additions & 2 deletions packages/shared/src/DrainableWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,12 @@ export const makeDrainableWorker = <A, E, R>(
Effect.tx,
);

const enqueue = (element: A): Effect.Effect<boolean, never, never> =>
const enqueue: DrainableWorker<A>["enqueue"] = (element: A) =>
TxQueue.offer(queue, element).pipe(
Effect.tap(() => TxRef.update(outstanding, (n) => n + 1)),
// A shut-down queue drops the item and reports false; only track
// items that were actually accepted or drain() can hang forever.
Effect.tap((offered) => (offered ? TxRef.update(outstanding, (n) => n + 1) : Effect.void)),
Effect.asVoid,
Effect.tx,
);

Expand Down
Loading