From 960c866cd26620df1ced229cc8a403c33d45da07 Mon Sep 17 00:00:00 2001 From: Tim Smart Date: Fri, 25 Sep 2026 02:44:37 +0000 Subject: [PATCH] test(cluster): cover teardown key collisions and owner handoff --- packages/cluster/test/Interruptors.test.ts | 21 ++++++++++++ .../cluster/test/integration/Workflow.test.ts | 33 ++++++++++++++++--- packages/workflow/test/WorkflowEngine.test.ts | 2 +- 3 files changed, 51 insertions(+), 5 deletions(-) diff --git a/packages/cluster/test/Interruptors.test.ts b/packages/cluster/test/Interruptors.test.ts index a9536901c4..10a5390759 100644 --- a/packages/cluster/test/Interruptors.test.ts +++ b/packages/cluster/test/Interruptors.test.ts @@ -40,4 +40,25 @@ describe("interruptors", () => { assert(Exit.isInterrupted(exit)) assert.isFalse(Teardown.isActive(address)) })) + it("does not classify a different entity with the same delimited key as active", () => { + const teardown = make() + const first = EntityAddress.make({ + entityType: EntityType.EntityType.make("tenant:a"), + entityId: EntityId.make("b"), + shardId: address.shardId + }) + const second = EntityAddress.make({ + entityType: EntityType.EntityType.make("tenant"), + entityId: EntityId.make("a:b"), + shardId: address.shardId + }) + teardown.acquireEntity(first) + try { + assert.isTrue(teardown.isActive(first)) + assert.isFalse(teardown.isActive(second)) + } finally { + teardown.releaseEntity(first) + } + assert.isFalse(teardown.isActive(first)) + }) }) diff --git a/packages/cluster/test/integration/Workflow.test.ts b/packages/cluster/test/integration/Workflow.test.ts index 8e254b4644..86475567f7 100644 --- a/packages/cluster/test/integration/Workflow.test.ts +++ b/packages/cluster/test/integration/Workflow.test.ts @@ -262,6 +262,7 @@ const ShutdownActivityWorkflow = Workflow.make({ success: Schema.String, idempotencyKey: ({ id }) => id }) +const ShutdownHandoffGate = DurableDeferred.make("ClusterIntegrationShutdownHandoffGate") const ShutdownSuspendActivityWorkflow = Workflow.make({ name: "ClusterIntegrationShutdownSuspendActivity", payload: { id: Schema.String }, @@ -276,6 +277,7 @@ const activityHandoffState = { wasPersisted: false, faultRelease: Effect.unsafeMakeLatch(true), runs: new Map(), + completedOn: new Map(), compensations: new Set(), resourceEvents: new Map>() } @@ -287,6 +289,7 @@ const resetActivityHandoffState = (id: string, faultReleaseOpen = true) => { activityHandoffState.wasPersisted = false activityHandoffState.faultRelease = Effect.unsafeMakeLatch(faultReleaseOpen) activityHandoffState.runs.delete(id) + activityHandoffState.completedOn.delete(id) activityHandoffState.compensations.delete(id) activityHandoffState.resourceEvents.set(id, []) } @@ -296,7 +299,8 @@ const shutdownLayer = ( Schema.Struct<{ id: typeof Schema.String }>, typeof Schema.String, typeof Schema.Never - > + >, + handoff = false ) => workflow.toLayer(({ id }) => Effect.gen(function*() { @@ -309,10 +313,15 @@ const shutdownLayer = ( ).pipe(Workflow.provideScope) yield* activityHandoffState.ready.open yield* activityHandoffState.start.await + if (handoff) yield* DurableDeferred.await(ShutdownHandoffGate) + const runner = handoff ? yield* Effect.serviceOption(Entity.CurrentRunnerAddress) : Option.none() return yield* Activity.make({ name: "ShutdownActivity", success: Schema.String, execute: Effect.sync(() => { + if (Option.isSome(runner)) { + activityHandoffState.completedOn.set(id, runner.value.host + ":" + runner.value.port) + } activityHandoffState.runs.set(id, (activityHandoffState.runs.get(id) ?? 0) + 1) return `completed:${id}` }) @@ -502,7 +511,10 @@ describe("cluster workflow integration", () => { const cluster = yield* make({ backend, config: { entityTerminationTimeout: 100 }, - entities: ShutdownActivityWorkflowLayer.pipe(Layer.provide(ClusterWorkflowEngine.layer), Layer.orDie) + entities: shutdownLayer(ShutdownActivityWorkflow, true).pipe( + Layer.provide(ClusterWorkflowEngine.layer), + Layer.orDie + ) }) yield* cluster.start(3) yield* cluster.waitForStableAssignments() @@ -513,8 +525,11 @@ describe("cluster workflow integration", () => { ) const owner = workflowOwner(cluster, executionId) assert.isDefined(owner) - const stopping = yield* cluster.stop(owner!).pipe(Effect.forkScoped) yield* activityHandoffState.start.open + yield* waitForSuspended(cluster, ShutdownActivityWorkflow, executionId) + assert.strictEqual(activityHandoffState.runs.get(id), undefined) + yield* cluster.stop(owner!) + yield* cluster.waitForStableAssignments() yield* cluster.waitUntil( "The shutdown activity workflow was not handed to another runner", Effect.sync(() => { @@ -522,9 +537,19 @@ describe("cluster workflow integration", () => { return next !== undefined && next !== owner }) ) + const nextOwner = workflowOwner(cluster, executionId) + assert.isDefined(nextOwner) + const token = DurableDeferred.tokenFromExecutionId(ShutdownHandoffGate, { + workflow: ShutdownActivityWorkflow, + executionId + }) + yield* withWorkflow(cluster, DurableDeferred.succeed(ShutdownHandoffGate, { token, value: undefined })) const result = yield* waitForComplete(cluster, ShutdownActivityWorkflow, executionId) - yield* Fiber.join(stopping) assert.deepStrictEqual(result.exit, Exit.succeed(`completed:${id}`)) + assert.strictEqual( + activityHandoffState.completedOn.get(id), + nextOwner!.address.host + ":" + nextOwner!.address.port + ) assert.strictEqual(activityHandoffState.runs.get(id), 1) assert.strictEqual((yield* cluster.messageCounts()).unprocessed, 0) })) diff --git a/packages/workflow/test/WorkflowEngine.test.ts b/packages/workflow/test/WorkflowEngine.test.ts index 863eec942f..f1c1ba2973 100644 --- a/packages/workflow/test/WorkflowEngine.test.ts +++ b/packages/workflow/test/WorkflowEngine.test.ts @@ -368,7 +368,7 @@ describe("deferred self-completion", () => { assert.deepStrictEqual(result, Exit.succeed("ok")) assert.deepStrictEqual(events, ["start-1", "cleanup-start", "cleanup-end", "end-1", "start-2", "end-2"]) } - }).pipe(Effect.provide(layer)) + }).pipe(Effect.ensuring(release.open), Effect.provide(layer)) }), 5_000) } })