runner.test.ts 16 KB

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