|
1 | 1 | import { expect, it } from "@effect/vitest"; |
| 2 | +import * as Context from "effect/Context"; |
2 | 3 | import * as Deferred from "effect/Deferred"; |
3 | 4 | import * as Effect from "effect/Effect"; |
| 5 | +import * as Fiber from "effect/Fiber"; |
4 | 6 | import * as Option from "effect/Option"; |
5 | 7 | import * as Tracer from "effect/Tracer"; |
6 | 8 |
|
7 | | -import { forkParked, ServerActivation, withDetachedSpan } from "./serverActivation.ts"; |
| 9 | +import { forkParked, ServerActivation, forkScopedDetached } from "./serverActivation.ts"; |
8 | 10 |
|
9 | 11 | it.effect("forkParked detaches the activation wait and keeps the same root after release", () => |
10 | 12 | Effect.scoped( |
@@ -59,50 +61,58 @@ it.effect("proves a root is parked before returning and releases it with one gat |
59 | 61 | ), |
60 | 62 | ); |
61 | 63 |
|
62 | | -it.effect("withDetachedSpan re-roots instead of inheriting the ambient ParentSpan", () => |
63 | | - Effect.gen(function* () { |
64 | | - const ambient = Tracer.externalSpan({ |
65 | | - traceId: "00000000000000000000000000000001", |
66 | | - spanId: "0000000000000001", |
67 | | - sampled: true, |
68 | | - }); |
69 | | - |
70 | | - const detachedSpanId = yield* Effect.serviceOption(Tracer.ParentSpan).pipe( |
71 | | - Effect.map(Option.map((span) => span.spanId)), |
72 | | - withDetachedSpan, |
73 | | - Effect.provideService(Tracer.ParentSpan, ambient), |
74 | | - ); |
75 | | - expect(Option.isSome(detachedSpanId)).toBe(true); |
76 | | - if (Option.isSome(detachedSpanId)) { |
77 | | - expect(detachedSpanId.value).not.toBe("0000000000000001"); |
78 | | - } |
79 | | - |
80 | | - const stillHasParent = yield* Effect.serviceOption(Tracer.ParentSpan).pipe( |
81 | | - Effect.map(Option.map((span) => span.spanId)), |
82 | | - Effect.provideService(Tracer.ParentSpan, ambient), |
83 | | - ); |
84 | | - expect(Option.isSome(stillHasParent)).toBe(true); |
85 | | - if (Option.isSome(stillHasParent)) { |
86 | | - expect(stillHasParent.value).toBe("0000000000000001"); |
87 | | - } |
88 | | - }), |
| 64 | +it.effect("forkScopedDetached re-roots instead of inheriting the ambient ParentSpan", () => |
| 65 | + Effect.scoped( |
| 66 | + Effect.gen(function* () { |
| 67 | + const ambient = Tracer.externalSpan({ |
| 68 | + traceId: "00000000000000000000000000000001", |
| 69 | + spanId: "0000000000000001", |
| 70 | + sampled: true, |
| 71 | + }); |
| 72 | + |
| 73 | + const detachedSpanId = yield* Effect.serviceOption(Tracer.ParentSpan).pipe( |
| 74 | + Effect.map(Option.map((span) => span.spanId)), |
| 75 | + forkScopedDetached, |
| 76 | + Effect.flatMap(Fiber.join), |
| 77 | + Effect.provideService(Tracer.ParentSpan, ambient), |
| 78 | + ); |
| 79 | + expect(Option.isSome(detachedSpanId)).toBe(true); |
| 80 | + if (Option.isSome(detachedSpanId)) { |
| 81 | + expect(detachedSpanId.value).not.toBe("0000000000000001"); |
| 82 | + } |
| 83 | + |
| 84 | + const stillHasParent = yield* Effect.serviceOption(Tracer.ParentSpan).pipe( |
| 85 | + Effect.map(Option.map((span) => span.spanId)), |
| 86 | + Effect.provideService(Tracer.ParentSpan, ambient), |
| 87 | + ); |
| 88 | + expect(Option.isSome(stillHasParent)).toBe(true); |
| 89 | + if (Option.isSome(stillHasParent)) { |
| 90 | + expect(stillHasParent.value).toBe("0000000000000001"); |
| 91 | + } |
| 92 | + }), |
| 93 | + ), |
89 | 94 | ); |
90 | 95 |
|
91 | | -it.effect("withDetachedSpan keeps ParentSpan-requiring effects working under the fresh root", () => |
92 | | - Effect.gen(function* () { |
93 | | - const span = yield* Effect.service(Tracer.ParentSpan).pipe( |
94 | | - withDetachedSpan, |
95 | | - Effect.provideService( |
96 | | - Tracer.ParentSpan, |
97 | | - Tracer.externalSpan({ |
98 | | - traceId: "00000000000000000000000000000001", |
99 | | - spanId: "0000000000000001", |
100 | | - sampled: true, |
101 | | - }), |
102 | | - ), |
103 | | - ); |
104 | | - expect(span.spanId).not.toBe("0000000000000001"); |
105 | | - }), |
| 96 | +it.effect( |
| 97 | + "forkScopedDetached keeps ParentSpan-requiring effects working under the fresh root", |
| 98 | + () => |
| 99 | + Effect.scoped( |
| 100 | + Effect.gen(function* () { |
| 101 | + const span = yield* Effect.service(Tracer.ParentSpan).pipe( |
| 102 | + forkScopedDetached, |
| 103 | + Effect.flatMap(Fiber.join), |
| 104 | + Effect.provideService( |
| 105 | + Tracer.ParentSpan, |
| 106 | + Tracer.externalSpan({ |
| 107 | + traceId: "00000000000000000000000000000001", |
| 108 | + spanId: "0000000000000001", |
| 109 | + sampled: true, |
| 110 | + }), |
| 111 | + ), |
| 112 | + ); |
| 113 | + expect(span.spanId).not.toBe("0000000000000001"); |
| 114 | + }), |
| 115 | + ), |
106 | 116 | ); |
107 | 117 |
|
108 | 118 | it.effect("forkParked roots do not inherit the ambient ParentSpan", () => |
@@ -130,3 +140,61 @@ it.effect("forkParked roots do not inherit the ambient ParentSpan", () => |
130 | 140 | }), |
131 | 141 | ), |
132 | 142 | ); |
| 143 | + |
| 144 | +// Inspect actual context references, including Effect's overlay/base/cache roots. |
| 145 | +// Service lookup alone hides replaced spans that are still retained underneath. |
| 146 | +const retainsReference = (root: unknown, target: object): boolean => { |
| 147 | + const seen = new Set<object>(); |
| 148 | + const pending: unknown[] = [root]; |
| 149 | + while (pending.length > 0) { |
| 150 | + const value = pending.pop(); |
| 151 | + if (value === target) return true; |
| 152 | + if (typeof value !== "object" || value === null || seen.has(value)) continue; |
| 153 | + seen.add(value); |
| 154 | + pending.push(...(value instanceof Map ? value.values() : Object.values(value))); |
| 155 | + } |
| 156 | + return false; |
| 157 | +}; |
| 158 | + |
| 159 | +for (const gated of [false, true]) { |
| 160 | + it.effect(`forkParked drops retained parent references (${gated ? "gated" : "immediate"})`, () => |
| 161 | + Effect.gen(function* () { |
| 162 | + const ambient = Tracer.externalSpan({ traceId: "ambient", spanId: "ambient" }); |
| 163 | + const marker = Context.Service<{ readonly value: number }>("test/detached-marker"); |
| 164 | + const service = { value: 42 }; |
| 165 | + const observed = yield* Deferred.make<Context.Context<never>>(); |
| 166 | + const stopped = yield* Deferred.make<void>(); |
| 167 | + yield* Effect.scoped( |
| 168 | + Effect.gen(function* () { |
| 169 | + const observe = Effect.context<never>().pipe( |
| 170 | + Effect.flatMap((context) => Deferred.succeed(observed, context)), |
| 171 | + Effect.andThen(Effect.never), |
| 172 | + Effect.ensuring(Deferred.succeed(stopped, undefined)), |
| 173 | + ); |
| 174 | + yield* forkParked(gated ? Effect.void : observe).pipe( |
| 175 | + Effect.provideService(ServerActivation, gated ? observe : undefined), |
| 176 | + Effect.provideService(marker, service), |
| 177 | + Effect.provideService(Tracer.ParentSpan, ambient), |
| 178 | + ); |
| 179 | + const context = yield* Deferred.await(observed); |
| 180 | + expect(Context.getUnsafe(context, marker)).toBe(service); |
| 181 | + expect(retainsReference(context, ambient)).toBe(false); |
| 182 | + }), |
| 183 | + ); |
| 184 | + expect(yield* Deferred.isDone(stopped)).toBe(true); |
| 185 | + }), |
| 186 | + ); |
| 187 | +} |
| 188 | + |
| 189 | +it.effect("forkScopedDetached gives the child a detached context before it starts", () => |
| 190 | + Effect.scoped( |
| 191 | + Effect.gen(function* () { |
| 192 | + const ambient = Tracer.externalSpan({ traceId: "ambient", spanId: "ambient" }); |
| 193 | + yield* Effect.gen(function* () { |
| 194 | + const fiber = yield* forkScopedDetached(Effect.never); |
| 195 | + expect(retainsReference(fiber.context, ambient)).toBe(false); |
| 196 | + expect(yield* Tracer.ParentSpan).toBe(ambient); |
| 197 | + }).pipe(Effect.provideService(Tracer.ParentSpan, ambient)); |
| 198 | + }), |
| 199 | + ), |
| 200 | +); |
0 commit comments