|
1 | 1 | import type { EntityRegistration } from "@effect/platform-cloudflare/internal/entityRegistry" |
2 | 2 | import { makeEntityRuntime } from "@effect/platform-cloudflare/internal/entityRuntime" |
3 | 3 | import { assert, describe, it } from "@effect/vitest" |
4 | | -import { Cause, Context, Effect, Exit, Metric, Option, Schedule, Schema, Stream, Tracer } from "effect" |
| 4 | +import { |
| 5 | + Cause, |
| 6 | + Context, |
| 7 | + Deferred, |
| 8 | + Effect, |
| 9 | + Exit, |
| 10 | + Fiber, |
| 11 | + Metric, |
| 12 | + Option, |
| 13 | + Schedule, |
| 14 | + Schema, |
| 15 | + Scope, |
| 16 | + Stream, |
| 17 | + Tracer |
| 18 | +} from "effect" |
5 | 19 | import { ClusterMetrics, Entity, EntityAddress, EntityId, EntityType, ShardId } from "effect/unstable/cluster" |
6 | 20 | import { Rpc, RpcSchema } from "effect/unstable/rpc" |
7 | 21 |
|
@@ -131,6 +145,72 @@ describe("EntityRuntime", () => { |
131 | 145 | assert.deepStrictEqual(replies.map((reply) => reply.exit.value), ["pong", "pong"]) |
132 | 146 | })) |
133 | 147 |
|
| 148 | + it.effect("shares an asynchronous handler build between concurrent first requests", () => |
| 149 | + Effect.gen(function*() { |
| 150 | + const Concurrent = Entity.make("Concurrent", [ |
| 151 | + Rpc.make("Ping", { success: Schema.String }) |
| 152 | + ]) |
| 153 | + const concurrentAddress = new EntityAddress.EntityAddress({ |
| 154 | + shardId: ShardId.make("default", 1), |
| 155 | + entityType: EntityType.make("Concurrent"), |
| 156 | + entityId: EntityId.make("42") |
| 157 | + }) |
| 158 | + const context = Context.empty() |
| 159 | + const metricContext = Context.merge( |
| 160 | + context, |
| 161 | + Metric.CurrentMetricAttributes.context({ type: Concurrent.type }) |
| 162 | + ) |
| 163 | + const releaseBuild = Deferred.makeUnsafe<void>() |
| 164 | + let builds = 0 |
| 165 | + let finalizers = 0 |
| 166 | + const registration: EntityRegistration = { |
| 167 | + entity: Concurrent, |
| 168 | + build: Effect.gen(function*() { |
| 169 | + const scope = Option.getOrThrow(yield* Effect.serviceOption(Scope.Scope)) |
| 170 | + builds++ |
| 171 | + yield* Scope.addFinalizer( |
| 172 | + scope, |
| 173 | + Effect.sync(() => { |
| 174 | + finalizers++ |
| 175 | + }) |
| 176 | + ) |
| 177 | + yield* Deferred.await(releaseBuild) |
| 178 | + return Concurrent.of({ Ping: () => Effect.succeed("pong") }) |
| 179 | + }), |
| 180 | + options: { concurrency: "unbounded" }, |
| 181 | + context |
| 182 | + } |
| 183 | + const runtime = yield* makeEntityRuntime(registration, concurrentAddress, () => "reply") |
| 184 | + const first = yield* Effect.forkChild( |
| 185 | + runtime.run({ ...request, address: concurrentAddress } as any, Option.none(), false, () => Effect.void) |
| 186 | + ) |
| 187 | + const second = yield* Effect.forkChild( |
| 188 | + runtime.run( |
| 189 | + { |
| 190 | + ...request, |
| 191 | + requestId: "0198bd72-6a83-72f1-8d87-5e9b5cf1e003", |
| 192 | + address: concurrentAddress |
| 193 | + } as any, |
| 194 | + Option.none(), |
| 195 | + false, |
| 196 | + () => Effect.void |
| 197 | + ) |
| 198 | + ) |
| 199 | + |
| 200 | + yield* Effect.yieldNow |
| 201 | + yield* Deferred.succeed(releaseBuild, undefined) |
| 202 | + yield* Fiber.join(first) |
| 203 | + yield* Fiber.join(second) |
| 204 | + |
| 205 | + assert.strictEqual(builds, 1) |
| 206 | + assert.strictEqual(ClusterMetrics.entities.valueUnsafe(metricContext).value, BigInt(1)) |
| 207 | + assert.strictEqual(finalizers, 0) |
| 208 | + |
| 209 | + yield* runtime.invalidate() |
| 210 | + assert.strictEqual(ClusterMetrics.entities.valueUnsafe(metricContext).value, BigInt(0)) |
| 211 | + assert.strictEqual(finalizers, 1) |
| 212 | + })) |
| 213 | + |
134 | 214 | it.effect("resumes ask stream sequence from lastSentChunk and ends with WithExit", () => |
135 | 215 | Effect.gen(function*() { |
136 | 216 | const Streaming = Entity.make("User", [ |
|
0 commit comments