diff --git a/packages/platform/cloudflare/test/integration/KeepAliveRecovery.test.ts b/packages/platform/cloudflare/test/integration/KeepAliveRecovery.test.ts new file mode 100644 index 0000000000..66627ec2ea --- /dev/null +++ b/packages/platform/cloudflare/test/integration/KeepAliveRecovery.test.ts @@ -0,0 +1,44 @@ +import { assert, describe, it } from "@effect/vitest" +import { Effect } from "effect" +import { makeCluster } from "./harness.ts" + +// After a restart nothing in these tests messages the entity before the +// rebuild is observed: the entity Durable Object has to wake on its own. +describe("Cloudflare cluster integration | KeepAlive recovery", () => { + it.live("rebuilds an entity holding keepAlive after an isolate restart", () => + Effect.gen(function*() { + const cluster = yield* makeCluster + + yield* cluster.fetchJson("/pinned/pin?id=recover") + yield* cluster.waitUntil( + "Entity.keepAlive(true) did not establish a hold on the entity Durable Object", + Effect.map(cluster.fetchJson("/holds?type=Pinned&id=recover"), (result) => result.holds === 1) + ) + + yield* cluster.restart + + yield* cluster.waitUntil( + "The keep-alive entity was not rebuilt after the isolate restart", + Effect.map(cluster.fetchJson("/state"), (state) => (state.builds["Pinned/recover"] ?? 0) === 1) + ) + yield* cluster.waitUntil( + "The rebuilt keep-alive entity did not hold its Durable Object again", + Effect.map(cluster.fetchJson("/holds?type=Pinned&id=recover"), (result) => result.holds === 1) + ) + + // Releasing keep-alive ends the recovery: the next restart leaves the + // entity cold. + yield* cluster.fetchJson("/pinned/unpin?id=recover") + yield* cluster.waitUntil( + "Releasing keepAlive did not unpin the entity Durable Object", + Effect.map(cluster.fetchJson("/holds?type=Pinned&id=recover"), (result) => result.holds === 0) + ) + yield* cluster.restart + yield* Effect.sleep("2 seconds") + const state = yield* cluster.fetchJson("/state") + assert.isUndefined( + state.builds["Pinned/recover"], + "The entity was rebuilt after a restart although it had released keepAlive" + ) + }), 60_000) +}) diff --git a/packages/platform/cloudflare/test/integration/Recovery.test.ts b/packages/platform/cloudflare/test/integration/Recovery.test.ts new file mode 100644 index 0000000000..c7d673f12a --- /dev/null +++ b/packages/platform/cloudflare/test/integration/Recovery.test.ts @@ -0,0 +1,38 @@ +import { assert, describe, it } from "@effect/vitest" +import { Effect } from "effect" +import { makeCluster } from "./harness.ts" + +// After a restart nothing in these tests messages the entity before the +// replay is observed: the entity Durable Object has to wake on its own. +describe("Cloudflare cluster integration | Recovery", () => { + it.effect( + "replays an unprocessed persisted tell after an isolate restart without new contact", + () => + Effect.gen(function*() { + const cluster = yield* makeCluster + + yield* cluster.fetchJson("/blocker/hold?id=cold-replay&op=first") + yield* cluster.waitUntil( + "The persisted Hold request did not start before the restart", + Effect.map(cluster.fetchJson("/state"), (state) => (state.entered["cold-replay/first"] ?? 0) === 1) + ) + + yield* cluster.restart + + // Module state is wiped by the restart, so an entry here can only come + // from the entity Durable Object replaying its unprocessed row. + yield* cluster.waitUntil( + "The unprocessed persisted tell was not replayed after the isolate restart", + Effect.map(cluster.fetchJson("/state"), (state) => (state.entered["cold-replay/first"] ?? 0) === 1) + ) + yield* cluster.fetchJson("/blocker/open?id=cold-replay&op=first") + yield* cluster.waitUntil( + "The replayed Hold request did not run to completion", + Effect.map(cluster.fetchJson("/state"), (state) => (state.completed["cold-replay/first"] ?? 0) === 1) + ) + const read = yield* cluster.fetchJson("/blocker/get?id=cold-replay") + assert.strictEqual(read.value, 1, "The replayed request did not update entity state exactly once") + }), + 60_000 + ) +}) diff --git a/packages/platform/cloudflare/test/integration/fixtures/worker.ts b/packages/platform/cloudflare/test/integration/fixtures/worker.ts index 91af381a0b..2b34075102 100644 --- a/packages/platform/cloudflare/test/integration/fixtures/worker.ts +++ b/packages/platform/cloudflare/test/integration/fixtures/worker.ts @@ -315,10 +315,18 @@ const Pinned = Entity.make("Pinned", [ Rpc.make("Unpin", { success: Schema.String }) ]) -const PinnedLayer = Pinned.toLayer({ - Pin: () => Effect.as(Entity.keepAlive(true), "pinned"), - Unpin: () => Effect.as(Entity.keepAlive(false), "unpinned") -}) +// Builds are counted per entity id, so a restart test can see whether a +// keep-alive entity was rebuilt without messaging it. +const PinnedLayer = Pinned.toLayer( + Effect.gen(function*() { + const address = yield* Entity.CurrentAddress + bump(counts.builds, `Pinned/${address.entityId}`) + return Pinned.of({ + Pin: () => Effect.as(Entity.keepAlive(true), "pinned"), + Unpin: () => Effect.as(Entity.keepAlive(false), "unpinned") + }) + }) +) const Holder = Entity.make("Holder", [ Rpc.make("Get", { success: Schema.Number }), @@ -539,6 +547,7 @@ const makeAppLayer = (env: Record) => CronLayer ).pipe( Layer.provideMerge(CloudflareCluster.layer({ + keepAliveHeartbeat: "1 second", entities: [Counter, Blocker, Serial, Flaky, Relay, Pinned, Holder, Journal, Coordinated], entityNamespace: env.CLUSTER_ENTITY, workflowNamespace: env.CLUSTER_WORKFLOW,