|
1 | | -import { describe, expect, test } from "bun:test" |
| 1 | +import { describe, expect } from "bun:test" |
2 | 2 | import { Deferred, Effect, Exit, Fiber, Ref, Scope } from "effect" |
3 | 3 | import { Runner } from "@/effect/runner" |
4 | 4 | import { it } from "../lib/effect" |
@@ -198,58 +198,52 @@ describe("Runner", () => { |
198 | 198 | }), |
199 | 199 | ) |
200 | 200 |
|
201 | | - test("cancel does not deadlock when replacement work starts before interrupted run exits", async () => { |
202 | | - function defer() { |
203 | | - let resolve!: () => void |
204 | | - const promise = new Promise<void>((done) => { |
205 | | - resolve = done |
206 | | - }) |
207 | | - return { promise, resolve } |
208 | | - } |
| 201 | + it.live( |
| 202 | + "cancel does not deadlock when replacement work starts before interrupted run exits", |
| 203 | + Effect.gen(function* () { |
| 204 | + const s = yield* Scope.Scope |
| 205 | + const hit = yield* Deferred.make<void>() |
| 206 | + const hold = yield* Deferred.make<void>() |
| 207 | + const done = yield* Deferred.make<void>() |
| 208 | + |
| 209 | + yield* Effect.gen(function* () { |
| 210 | + const runner = Runner.make<string>(s) |
| 211 | + const first = Effect.never.pipe( |
| 212 | + Effect.onInterrupt(() => Deferred.succeed(hit, undefined)), |
| 213 | + Effect.ensuring(Deferred.await(hold)), |
| 214 | + Effect.as("first"), |
| 215 | + ) |
209 | 216 |
|
210 | | - function fail(ms: number, msg: string) { |
211 | | - return new Promise<never>((_, reject) => { |
212 | | - setTimeout(() => reject(new Error(msg)), ms) |
213 | | - }) |
214 | | - } |
| 217 | + const a = yield* runner.ensureRunning(first).pipe(Effect.exit, Effect.forkChild) |
| 218 | + yield* Effect.sleep("10 millis") |
215 | 219 |
|
216 | | - const s = await Effect.runPromise(Scope.make()) |
217 | | - const hit = defer() |
218 | | - const hold = defer() |
219 | | - const done = defer() |
220 | | - try { |
221 | | - const runner = Runner.make<string>(s) |
222 | | - const first = Effect.never.pipe( |
223 | | - Effect.onInterrupt(() => Effect.sync(() => hit.resolve())), |
224 | | - Effect.ensuring(Effect.promise(() => hold.promise)), |
225 | | - Effect.as("first"), |
| 220 | + const stop = yield* runner.cancel.pipe(Effect.forkChild) |
| 221 | + yield* Deferred.await(hit).pipe(Effect.timeout("250 millis")) |
| 222 | + |
| 223 | + const b = yield* runner.ensureRunning(Deferred.await(done).pipe(Effect.as("second"))).pipe(Effect.forkChild) |
| 224 | + yield* Effect.yieldNow |
| 225 | + expect(runner.busy).toBe(true) |
| 226 | + |
| 227 | + yield* Deferred.succeed(hold, undefined) |
| 228 | + const stopExit = yield* Fiber.await(stop).pipe(Effect.timeout("250 millis")) |
| 229 | + expect(Exit.isSuccess(stopExit)).toBe(true) |
| 230 | + |
| 231 | + expect(runner.busy).toBe(true) |
| 232 | + yield* Deferred.succeed(done, undefined) |
| 233 | + expect(yield* Fiber.join(b).pipe(Effect.timeout("250 millis"))).toBe("second") |
| 234 | + expect(runner.busy).toBe(false) |
| 235 | + |
| 236 | + const exit = yield* Fiber.join(a) |
| 237 | + expect(Exit.isFailure(exit)).toBe(true) |
| 238 | + }).pipe( |
| 239 | + Effect.ensuring( |
| 240 | + Effect.all([Deferred.succeed(hold, undefined), Deferred.succeed(done, undefined)], { discard: true }).pipe( |
| 241 | + Effect.ignore, |
| 242 | + ), |
| 243 | + ), |
226 | 244 | ) |
227 | | - |
228 | | - const a = Effect.runPromiseExit(runner.ensureRunning(first)) |
229 | | - await Bun.sleep(10) |
230 | | - |
231 | | - const stop = Effect.runPromise(runner.cancel) |
232 | | - await Promise.race([hit.promise, fail(250, "cancel did not interrupt running work")]) |
233 | | - |
234 | | - const b = Effect.runPromise(runner.ensureRunning(Effect.promise(() => done.promise).pipe(Effect.as("second")))) |
235 | | - expect(runner.busy).toBe(true) |
236 | | - |
237 | | - hold.resolve() |
238 | | - await Promise.race([stop, fail(250, "cancel deadlocked while replacement run was active")]) |
239 | | - |
240 | | - expect(runner.busy).toBe(true) |
241 | | - done.resolve() |
242 | | - expect(await b).toBe("second") |
243 | | - expect(runner.busy).toBe(false) |
244 | | - |
245 | | - const exit = await a |
246 | | - expect(Exit.isFailure(exit)).toBe(true) |
247 | | - } finally { |
248 | | - hold.resolve() |
249 | | - done.resolve() |
250 | | - await Promise.race([Effect.runPromise(Scope.close(s, Exit.void)), fail(1000, "runner scope did not close")]) |
251 | | - } |
252 | | - }) |
| 245 | + }), |
| 246 | + ) |
253 | 247 |
|
254 | 248 | // --- shell semantics --- |
255 | 249 |
|
|