runner.test.ts 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. import { describe, expect } from "bun:test"
  2. import { Cause, Deferred, Effect, Exit, Fiber, Latch, Ref, Scope } from "effect"
  3. import { Runner } from "@/effect/runner"
  4. import { it } from "../lib/effect"
  5. const waitForState = <A, E>(runner: Runner.Runner<A, E>, tag: Runner.State<A, E>["_tag"]) =>
  6. Effect.gen(function* () {
  7. while (runner.state._tag !== tag) yield* Effect.yieldNow
  8. }).pipe(Effect.timeout("1 second"))
  9. describe("Runner", () => {
  10. // --- ensureRunning semantics ---
  11. it.live(
  12. "ensureRunning starts work and returns result",
  13. Effect.gen(function* () {
  14. const s = yield* Scope.Scope
  15. const runner = Runner.make<string>(s)
  16. const result = yield* runner.ensureRunning(Effect.succeed("hello"))
  17. expect(result).toBe("hello")
  18. expect(runner.state._tag).toBe("Idle")
  19. expect(runner.busy).toBe(false)
  20. }),
  21. )
  22. it.live(
  23. "ensureRunning propagates work failures",
  24. Effect.gen(function* () {
  25. const s = yield* Scope.Scope
  26. const runner = Runner.make<string, string>(s)
  27. const exit = yield* runner.ensureRunning(Effect.fail("boom")).pipe(Effect.exit)
  28. expect(Exit.isFailure(exit)).toBe(true)
  29. expect(runner.state._tag).toBe("Idle")
  30. }),
  31. )
  32. it.live(
  33. "concurrent callers share the same run",
  34. Effect.gen(function* () {
  35. const s = yield* Scope.Scope
  36. const runner = Runner.make<string>(s)
  37. const calls = yield* Ref.make(0)
  38. const work = Effect.gen(function* () {
  39. yield* Ref.update(calls, (n) => n + 1)
  40. yield* Effect.sleep("10 millis")
  41. return "shared"
  42. })
  43. const [a, b] = yield* Effect.all([runner.ensureRunning(work), runner.ensureRunning(work)], {
  44. concurrency: "unbounded",
  45. })
  46. expect(a).toBe("shared")
  47. expect(b).toBe("shared")
  48. expect(yield* Ref.get(calls)).toBe(1)
  49. }),
  50. )
  51. it.live(
  52. "concurrent callers all receive same error",
  53. Effect.gen(function* () {
  54. const s = yield* Scope.Scope
  55. const runner = Runner.make<string, string>(s)
  56. const work = Effect.gen(function* () {
  57. yield* Effect.sleep("10 millis")
  58. return yield* Effect.fail("boom")
  59. })
  60. const [a, b] = yield* Effect.all(
  61. [runner.ensureRunning(work).pipe(Effect.exit), runner.ensureRunning(work).pipe(Effect.exit)],
  62. { concurrency: "unbounded" },
  63. )
  64. expect(Exit.isFailure(a)).toBe(true)
  65. expect(Exit.isFailure(b)).toBe(true)
  66. }),
  67. )
  68. it.live(
  69. "ensureRunning can be called again after previous run completes",
  70. Effect.gen(function* () {
  71. const s = yield* Scope.Scope
  72. const runner = Runner.make<string>(s)
  73. expect(yield* runner.ensureRunning(Effect.succeed("first"))).toBe("first")
  74. expect(yield* runner.ensureRunning(Effect.succeed("second"))).toBe("second")
  75. }),
  76. )
  77. it.live(
  78. "second ensureRunning ignores new work if already running",
  79. Effect.gen(function* () {
  80. const s = yield* Scope.Scope
  81. const runner = Runner.make<string>(s)
  82. const ran = yield* Ref.make<string[]>([])
  83. const first = Effect.gen(function* () {
  84. yield* Ref.update(ran, (a) => [...a, "first"])
  85. yield* Effect.sleep("50 millis")
  86. return "first-result"
  87. })
  88. const second = Effect.gen(function* () {
  89. yield* Ref.update(ran, (a) => [...a, "second"])
  90. return "second-result"
  91. })
  92. const [a, b] = yield* Effect.all([runner.ensureRunning(first), runner.ensureRunning(second)], {
  93. concurrency: "unbounded",
  94. })
  95. expect(a).toBe("first-result")
  96. expect(b).toBe("first-result")
  97. expect(yield* Ref.get(ran)).toEqual(["first"])
  98. }),
  99. )
  100. // --- cancel semantics ---
  101. it.live(
  102. "cancel interrupts running work",
  103. Effect.gen(function* () {
  104. const s = yield* Scope.Scope
  105. const runner = Runner.make<string>(s)
  106. const started = yield* Deferred.make<void>()
  107. const fiber = yield* runner
  108. .ensureRunning(
  109. Effect.gen(function* () {
  110. yield* Deferred.succeed(started, void 0)
  111. return yield* Effect.never.pipe(Effect.as("never"))
  112. }),
  113. )
  114. .pipe(Effect.forkChild)
  115. yield* Deferred.await(started)
  116. expect(runner.busy).toBe(true)
  117. expect(runner.state._tag).toBe("Running")
  118. yield* runner.cancel
  119. expect(runner.busy).toBe(false)
  120. const exit = yield* Fiber.await(fiber)
  121. expect(Exit.isFailure(exit)).toBe(true)
  122. }),
  123. )
  124. it.live(
  125. "cancel on idle is a no-op",
  126. Effect.gen(function* () {
  127. const s = yield* Scope.Scope
  128. const runner = Runner.make<string>(s)
  129. yield* runner.cancel
  130. expect(runner.busy).toBe(false)
  131. }),
  132. )
  133. it.live(
  134. "cancel with onInterrupt resolves callers gracefully",
  135. Effect.gen(function* () {
  136. const s = yield* Scope.Scope
  137. const runner = Runner.make<string>(s, { onInterrupt: Effect.succeed("fallback") })
  138. const fiber = yield* runner.ensureRunning(Effect.never.pipe(Effect.as("never"))).pipe(Effect.forkChild)
  139. yield* waitForState(runner, "Running")
  140. yield* runner.cancel
  141. const exit = yield* Fiber.await(fiber)
  142. expect(Exit.isSuccess(exit)).toBe(true)
  143. if (Exit.isSuccess(exit)) expect(exit.value).toBe("fallback")
  144. }),
  145. )
  146. it.live(
  147. "cancel with queued callers resolves all",
  148. Effect.gen(function* () {
  149. const s = yield* Scope.Scope
  150. const runner = Runner.make<string>(s, { onInterrupt: Effect.succeed("fallback") })
  151. const a = yield* runner.ensureRunning(Effect.never.pipe(Effect.as("x"))).pipe(Effect.forkChild)
  152. yield* waitForState(runner, "Running")
  153. const b = yield* runner.ensureRunning(Effect.succeed("y")).pipe(Effect.forkChild)
  154. yield* Effect.yieldNow
  155. yield* runner.cancel
  156. const [exitA, exitB] = yield* Effect.all([Fiber.await(a), Fiber.await(b)])
  157. expect(Exit.isSuccess(exitA)).toBe(true)
  158. expect(Exit.isSuccess(exitB)).toBe(true)
  159. if (Exit.isSuccess(exitA)) expect(exitA.value).toBe("fallback")
  160. if (Exit.isSuccess(exitB)) expect(exitB.value).toBe("fallback")
  161. }),
  162. )
  163. it.live(
  164. "work can be started after cancel",
  165. Effect.gen(function* () {
  166. const s = yield* Scope.Scope
  167. const runner = Runner.make<string>(s)
  168. const fiber = yield* runner.ensureRunning(Effect.never.pipe(Effect.as("x"))).pipe(Effect.forkChild)
  169. yield* waitForState(runner, "Running")
  170. yield* runner.cancel
  171. yield* Fiber.await(fiber)
  172. const result = yield* runner.ensureRunning(Effect.succeed("after-cancel"))
  173. expect(result).toBe("after-cancel")
  174. }),
  175. )
  176. it.live(
  177. "cancel does not deadlock when replacement work starts before interrupted run exits",
  178. Effect.gen(function* () {
  179. const s = yield* Scope.Scope
  180. const hit = yield* Deferred.make<void>()
  181. const hold = yield* Deferred.make<void>()
  182. const done = yield* Deferred.make<void>()
  183. yield* Effect.gen(function* () {
  184. const runner = Runner.make<string>(s)
  185. const first = Effect.never.pipe(
  186. Effect.onInterrupt(() => Deferred.succeed(hit, undefined)),
  187. Effect.ensuring(Deferred.await(hold)),
  188. Effect.as("first"),
  189. )
  190. const a = yield* runner.ensureRunning(first).pipe(Effect.exit, Effect.forkChild)
  191. yield* waitForState(runner, "Running")
  192. const stop = yield* runner.cancel.pipe(Effect.forkChild)
  193. yield* Deferred.await(hit).pipe(Effect.timeout("250 millis"))
  194. const b = yield* runner.ensureRunning(Deferred.await(done).pipe(Effect.as("second"))).pipe(Effect.forkChild)
  195. yield* Effect.yieldNow
  196. expect(runner.busy).toBe(true)
  197. yield* Deferred.succeed(hold, undefined)
  198. const stopExit = yield* Fiber.await(stop).pipe(Effect.timeout("250 millis"))
  199. expect(Exit.isSuccess(stopExit)).toBe(true)
  200. expect(runner.busy).toBe(true)
  201. yield* Deferred.succeed(done, undefined)
  202. expect(yield* Fiber.join(b).pipe(Effect.timeout("250 millis"))).toBe("second")
  203. expect(runner.busy).toBe(false)
  204. const exit = yield* Fiber.join(a)
  205. expect(Exit.isFailure(exit)).toBe(true)
  206. }).pipe(
  207. Effect.ensuring(
  208. Effect.all([Deferred.succeed(hold, undefined), Deferred.succeed(done, undefined)], { discard: true }).pipe(
  209. Effect.ignore,
  210. ),
  211. ),
  212. )
  213. }),
  214. )
  215. // --- shell semantics ---
  216. it.live(
  217. "shell runs exclusively",
  218. Effect.gen(function* () {
  219. const s = yield* Scope.Scope
  220. const runner = Runner.make<string>(s)
  221. const result = yield* runner.startShell(Effect.succeed("shell-done"))
  222. expect(result).toBe("shell-done")
  223. expect(runner.busy).toBe(false)
  224. }),
  225. )
  226. it.live(
  227. "shell rejects when run is active",
  228. Effect.gen(function* () {
  229. const s = yield* Scope.Scope
  230. const runner = Runner.make<string>(s)
  231. const started = yield* Deferred.make<void>()
  232. const fiber = yield* runner
  233. .ensureRunning(
  234. Effect.gen(function* () {
  235. yield* Deferred.succeed(started, undefined)
  236. return yield* Effect.never.pipe(Effect.as("x"))
  237. }),
  238. )
  239. .pipe(Effect.forkChild)
  240. yield* Deferred.await(started).pipe(Effect.timeout("250 millis"))
  241. yield* Effect.gen(function* () {
  242. while (runner.state._tag !== "Running") yield* Effect.yieldNow
  243. }).pipe(Effect.timeout("250 millis"))
  244. const exit = yield* runner.startShell(Effect.succeed("nope")).pipe(Effect.exit)
  245. expect(Exit.isFailure(exit)).toBe(true)
  246. yield* runner.cancel
  247. yield* Fiber.await(fiber).pipe(Effect.timeout("250 millis"))
  248. }),
  249. )
  250. it.live(
  251. "shell rejects when another shell is running",
  252. Effect.gen(function* () {
  253. const s = yield* Scope.Scope
  254. const runner = Runner.make<string>(s)
  255. const gate = yield* Deferred.make<void>()
  256. const sh = yield* runner.startShell(Deferred.await(gate).pipe(Effect.as("first"))).pipe(Effect.forkChild)
  257. yield* waitForState(runner, "Shell")
  258. const exit = yield* runner.startShell(Effect.succeed("second")).pipe(Effect.exit)
  259. expect(Exit.isFailure(exit)).toBe(true)
  260. if (Exit.isFailure(exit)) expect(Cause.squash(exit.cause)).toBeInstanceOf(Runner.Busy)
  261. yield* Deferred.succeed(gate, undefined)
  262. yield* Fiber.await(sh)
  263. }),
  264. )
  265. it.live(
  266. "cancel interrupts shell",
  267. Effect.gen(function* () {
  268. const s = yield* Scope.Scope
  269. const runner = Runner.make<string>(s)
  270. const gate = yield* Deferred.make<void>()
  271. const sh = yield* runner.startShell(Deferred.await(gate).pipe(Effect.as("ignored"))).pipe(Effect.forkChild)
  272. yield* waitForState(runner, "Shell")
  273. const stop = yield* runner.cancel.pipe(Effect.forkChild)
  274. const stopExit = yield* Fiber.await(stop).pipe(Effect.timeout("250 millis"))
  275. expect(Exit.isSuccess(stopExit)).toBe(true)
  276. expect(runner.busy).toBe(false)
  277. const shellExit = yield* Fiber.await(sh)
  278. expect(Exit.isFailure(shellExit)).toBe(true)
  279. yield* Deferred.succeed(gate, undefined).pipe(Effect.ignore)
  280. }),
  281. )
  282. it.live(
  283. "cancel does not mask shell defects",
  284. Effect.gen(function* () {
  285. const s = yield* Scope.Scope
  286. const runner = Runner.make<string>(s, { onInterrupt: Effect.succeed("interrupted") })
  287. const ready = yield* Latch.make()
  288. const sh = yield* runner
  289. .startShell(
  290. Effect.gen(function* () {
  291. yield* ready.open
  292. return yield* Effect.never.pipe(Effect.as("ignored"))
  293. }).pipe(Effect.ensuring(Effect.die("boom"))),
  294. ready,
  295. )
  296. .pipe(Effect.forkChild)
  297. yield* ready.await.pipe(Effect.timeout("250 millis"))
  298. yield* runner.cancel
  299. expect(Exit.isFailure(yield* Fiber.await(sh))).toBe(true)
  300. }),
  301. )
  302. // --- shell→run handoff ---
  303. it.live(
  304. "ensureRunning queues behind shell then runs after",
  305. Effect.gen(function* () {
  306. const s = yield* Scope.Scope
  307. const runner = Runner.make<string>(s)
  308. const gate = yield* Deferred.make<void>()
  309. const sh = yield* runner.startShell(Deferred.await(gate).pipe(Effect.as("shell-result"))).pipe(Effect.forkChild)
  310. yield* waitForState(runner, "Shell")
  311. expect(runner.state._tag).toBe("Shell")
  312. const run = yield* runner.ensureRunning(Effect.succeed("run-result")).pipe(Effect.forkChild)
  313. yield* waitForState(runner, "ShellThenRun")
  314. expect(runner.state._tag).toBe("ShellThenRun")
  315. yield* Deferred.succeed(gate, undefined)
  316. yield* Fiber.await(sh)
  317. const exit = yield* Fiber.await(run)
  318. expect(Exit.isSuccess(exit)).toBe(true)
  319. if (Exit.isSuccess(exit)) expect(exit.value).toBe("run-result")
  320. expect(runner.state._tag).toBe("Idle")
  321. }),
  322. )
  323. it.live(
  324. "multiple ensureRunning callers share the queued run behind shell",
  325. Effect.gen(function* () {
  326. const s = yield* Scope.Scope
  327. const runner = Runner.make<string>(s)
  328. const calls = yield* Ref.make(0)
  329. const gate = yield* Deferred.make<void>()
  330. const sh = yield* runner.startShell(Deferred.await(gate).pipe(Effect.as("shell"))).pipe(Effect.forkChild)
  331. yield* waitForState(runner, "Shell")
  332. const work = Effect.gen(function* () {
  333. yield* Ref.update(calls, (n) => n + 1)
  334. return "run"
  335. })
  336. const a = yield* runner.ensureRunning(work).pipe(Effect.forkChild)
  337. const b = yield* runner.ensureRunning(work).pipe(Effect.forkChild)
  338. yield* waitForState(runner, "ShellThenRun")
  339. yield* Deferred.succeed(gate, undefined)
  340. yield* Fiber.await(sh)
  341. const [exitA, exitB] = yield* Effect.all([Fiber.await(a), Fiber.await(b)])
  342. expect(Exit.isSuccess(exitA)).toBe(true)
  343. expect(Exit.isSuccess(exitB)).toBe(true)
  344. expect(yield* Ref.get(calls)).toBe(1)
  345. }),
  346. )
  347. it.live(
  348. "cancel during shell_then_run cancels both",
  349. Effect.gen(function* () {
  350. const s = yield* Scope.Scope
  351. const runner = Runner.make<string>(s)
  352. const sh = yield* runner.startShell(Effect.never.pipe(Effect.as("aborted"))).pipe(Effect.forkChild)
  353. yield* waitForState(runner, "Shell")
  354. const run = yield* runner.ensureRunning(Effect.succeed("y")).pipe(Effect.forkChild)
  355. yield* waitForState(runner, "ShellThenRun")
  356. expect(runner.state._tag).toBe("ShellThenRun")
  357. yield* runner.cancel
  358. expect(runner.busy).toBe(false)
  359. yield* Fiber.await(sh)
  360. const exit = yield* Fiber.await(run)
  361. expect(Exit.isFailure(exit)).toBe(true)
  362. }),
  363. )
  364. // --- lifecycle callbacks ---
  365. it.live(
  366. "onIdle fires when returning to idle from running",
  367. Effect.gen(function* () {
  368. const s = yield* Scope.Scope
  369. const count = yield* Ref.make(0)
  370. const runner = Runner.make<string>(s, {
  371. onIdle: Ref.update(count, (n) => n + 1),
  372. })
  373. yield* runner.ensureRunning(Effect.succeed("ok"))
  374. expect(yield* Ref.get(count)).toBe(1)
  375. }),
  376. )
  377. it.live(
  378. "onIdle fires on cancel",
  379. Effect.gen(function* () {
  380. const s = yield* Scope.Scope
  381. const count = yield* Ref.make(0)
  382. const runner = Runner.make<string>(s, {
  383. onIdle: Ref.update(count, (n) => n + 1),
  384. })
  385. const fiber = yield* runner.ensureRunning(Effect.never.pipe(Effect.as("x"))).pipe(Effect.forkChild)
  386. yield* waitForState(runner, "Running")
  387. yield* runner.cancel
  388. yield* Fiber.await(fiber)
  389. expect(yield* Ref.get(count)).toBeGreaterThanOrEqual(1)
  390. }),
  391. )
  392. it.live(
  393. "onBusy fires when shell starts",
  394. Effect.gen(function* () {
  395. const s = yield* Scope.Scope
  396. const count = yield* Ref.make(0)
  397. const runner = Runner.make<string>(s, {
  398. onBusy: Ref.update(count, (n) => n + 1),
  399. })
  400. yield* runner.startShell(Effect.succeed("done"))
  401. expect(yield* Ref.get(count)).toBe(1)
  402. }),
  403. )
  404. // --- busy flag ---
  405. it.live(
  406. "busy is true during run",
  407. Effect.gen(function* () {
  408. const s = yield* Scope.Scope
  409. const runner = Runner.make<string>(s)
  410. const gate = yield* Deferred.make<void>()
  411. const fiber = yield* runner.ensureRunning(Deferred.await(gate).pipe(Effect.as("ok"))).pipe(Effect.forkChild)
  412. yield* waitForState(runner, "Running")
  413. expect(runner.busy).toBe(true)
  414. yield* Deferred.succeed(gate, undefined)
  415. yield* Fiber.await(fiber)
  416. expect(runner.busy).toBe(false)
  417. }),
  418. )
  419. it.live(
  420. "busy is true during shell",
  421. Effect.gen(function* () {
  422. const s = yield* Scope.Scope
  423. const runner = Runner.make<string>(s)
  424. const gate = yield* Deferred.make<void>()
  425. const fiber = yield* runner.startShell(Deferred.await(gate).pipe(Effect.as("ok"))).pipe(Effect.forkChild)
  426. yield* waitForState(runner, "Shell")
  427. expect(runner.busy).toBe(true)
  428. yield* Deferred.succeed(gate, undefined)
  429. yield* Fiber.await(fiber)
  430. expect(runner.busy).toBe(false)
  431. }),
  432. )
  433. })