|
| 1 | +import { describe, expect, it } from "@effect/vitest" |
| 2 | +import { Effect, Fiber, Layer, Ref } from "effect" |
| 3 | +import { S } from "effect-app" |
| 4 | +import { ConfigureInterruptibilityMiddleware } from "effect-app/middleware" |
| 5 | +import { Rpc, RpcGroup, RpcTest } from "effect/unstable/rpc" |
| 6 | +import { applyRequestTypeInterruptibility } from "../src/api/routing.js" |
| 7 | +import { ConfigureInterruptibilityMiddlewareLive, RequestType } from "../src/api/routing/middleware.js" |
| 8 | + |
| 9 | +const InterruptibilityRpcs = RpcGroup.make( |
| 10 | + Rpc |
| 11 | + .make("doCommand", { success: S.Void }) |
| 12 | + .annotate(RequestType, "command") |
| 13 | + .middleware(ConfigureInterruptibilityMiddleware), |
| 14 | + Rpc |
| 15 | + .make("doQuery", { success: S.Void }) |
| 16 | + .annotate(RequestType, "query") |
| 17 | + .middleware(ConfigureInterruptibilityMiddleware) |
| 18 | +) |
| 19 | + |
| 20 | +const makeImplLayer = (commandDone: Ref.Ref<boolean>, queryDone: Ref.Ref<boolean>) => |
| 21 | + InterruptibilityRpcs.toLayer({ |
| 22 | + doCommand: () => |
| 23 | + applyRequestTypeInterruptibility( |
| 24 | + "command", |
| 25 | + Effect.sleep("120 millis").pipe(Effect.andThen(Ref.set(commandDone, true))) |
| 26 | + ), |
| 27 | + doQuery: () => |
| 28 | + applyRequestTypeInterruptibility( |
| 29 | + "query", |
| 30 | + Effect.sleep("120 millis").pipe(Effect.andThen(Ref.set(queryDone, true))) |
| 31 | + ) |
| 32 | + }) |
| 33 | + |
| 34 | +describe("routing interruptibility", () => { |
| 35 | + it.live( |
| 36 | + "e2e: command continues after client interrupt, query does not", |
| 37 | + () => |
| 38 | + Effect.gen(function*() { |
| 39 | + const commandDone = yield* Ref.make(false) |
| 40 | + const queryDone = yield* Ref.make(false) |
| 41 | + |
| 42 | + const client = yield* RpcTest |
| 43 | + .makeClient(InterruptibilityRpcs) |
| 44 | + .pipe( |
| 45 | + Effect.provide( |
| 46 | + Layer.mergeAll(makeImplLayer(commandDone, queryDone), ConfigureInterruptibilityMiddlewareLive) |
| 47 | + ) |
| 48 | + ) |
| 49 | + |
| 50 | + const commandFiber = yield* Effect.forkDetach(client.doCommand()) |
| 51 | + yield* Effect.sleep("20 millis") |
| 52 | + yield* Fiber.interrupt(commandFiber) |
| 53 | + yield* Effect.sleep("180 millis") |
| 54 | + expect(yield* Ref.get(commandDone)).toBe(true) |
| 55 | + |
| 56 | + const queryFiber = yield* Effect.forkDetach(client.doQuery()) |
| 57 | + yield* Effect.sleep("20 millis") |
| 58 | + yield* Fiber.interrupt(queryFiber) |
| 59 | + yield* Effect.sleep("180 millis") |
| 60 | + expect(yield* Ref.get(queryDone)).toBe(false) |
| 61 | + }) |
| 62 | + ) |
| 63 | +}) |
0 commit comments