The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 06:00 UTC
| 1 | import { describe, expect, it } from "@alchemy.run/test"; |
| 2 | import { CallbackError, type Callback, type CallbackOptions } from "alchemy/Callback"; |
| 3 | import { RuntimeContext } from "alchemy/RuntimeContext"; |
| 4 | import * as Cause from "effect/Cause"; |
| 5 | import * as Clock from "effect/Clock"; |
| 6 | import * as Deferred from "effect/Deferred"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import * as Exit from "effect/Exit"; |
| 9 | import * as Fiber from "effect/Fiber"; |
| 10 | import * as Logger from "effect/Logger"; |
| 11 | import * as TestClock from "effect/testing/TestClock"; |
| 12 | import { makeAlarmScheduler } from "./fixtures/alarm-scheduler.ts"; |
| 13 | |
| 14 | describe( |
| 15 | "durable callback scheduling policy", |
| 16 | { |
| 17 | tags: ["unit", "local", "provider:cloudflare", "provider:cloudflare:worker"], |
| 18 | }, |
| 19 | () => { |
| 20 | it.effect("acknowledges a successful job after persisting its recovery wake", () => |
| 21 | Effect.gen(function* () { |
| 22 | const fixture = yield* makeAlarmScheduler; |
| 23 | const delivered: string[] = []; |
| 24 | const callback = yield* fixture.register("archive", (payload: string) => |
| 25 | Effect.gen(function* () { |
| 26 | expect(fixture.alarm!).toBeGreaterThan(yield* Clock.currentTimeMillis); |
| 27 | delivered.push(payload); |
| 28 | }), |
| 29 | ); |
| 30 | fixture.seal(); |
| 31 | yield* callback.schedule("a", { after: 0, payload: "saved" }); |
| 32 | yield* fixture.fire; |
| 33 | expect(delivered).toEqual(["saved"]); |
| 34 | expect(fixture.alarm).toBeNull(); |
| 35 | }).pipe(Effect.provide(RuntimeContext.phantom)), |
| 36 | ); |
| 37 | |
| 38 | it.effect("backs off failures durably across instance reconstruction", () => |
| 39 | Effect.gen(function* () { |
| 40 | const fixture = yield* makeAlarmScheduler; |
| 41 | const register = () => |
| 42 | fixture.register("broken", () => Effect.fail("unavailable"), { |
| 43 | retry: { delay: "1 second" }, |
| 44 | }); |
| 45 | const callback = yield* register(); |
| 46 | fixture.seal(); |
| 47 | yield* callback.schedule("a", { after: 0, payload: null }); |
| 48 | yield* fixture.fire; |
| 49 | expect(fixture.alarm! - (yield* Clock.currentTimeMillis)).toBe(1_000); |
| 50 | fixture.restart(); |
| 51 | yield* register(); |
| 52 | fixture.seal(); |
| 53 | yield* fixture.fire; |
| 54 | expect(fixture.alarm! - (yield* Clock.currentTimeMillis)).toBe(2_000); |
| 55 | }).pipe(Effect.provide(RuntimeContext.phantom)), |
| 56 | ); |
| 57 | |
| 58 | for (const changeId of [false, true]) { |
| 59 | it.effect( |
| 60 | `bounds successful self-rearms${changeId ? " across cancellation and changed IDs" : ""}`, |
| 61 | () => |
| 62 | Effect.gen(function* () { |
| 63 | const fixture = yield* makeAlarmScheduler; |
| 64 | let deliveries = 0; |
| 65 | const callback: Callback<null> = yield* fixture.register( |
| 66 | "loop", |
| 67 | () => |
| 68 | Effect.gen(function* () { |
| 69 | yield* callback.cancel(String(changeId ? deliveries : 0)); |
| 70 | deliveries++; |
| 71 | yield* callback.schedule(String(changeId ? deliveries : 0), { |
| 72 | after: 0, |
| 73 | payload: null, |
| 74 | }); |
| 75 | }), |
| 76 | { retry: { delay: "1 second" } }, |
| 77 | ); |
| 78 | fixture.seal(); |
| 79 | yield* callback.schedule("0", { after: 0, payload: null }); |
| 80 | for (const delay of [ |
| 81 | 1_000, 2_000, 4_000, 8_000, 16_000, 32_000, 64_000, 3_600_000, 3_600_000, |
| 82 | ]) { |
| 83 | yield* fixture.fire; |
| 84 | expect(fixture.alarm! - (yield* Clock.currentTimeMillis)).toBe(delay); |
| 85 | } |
| 86 | expect(deliveries).toBe(9); |
| 87 | }).pipe(Effect.provide(RuntimeContext.phantom)), |
| 88 | ); |
| 89 | } |
| 90 | |
| 91 | it.effect( |
| 92 | "spaces retries from failure completion when a handler outlasts its recovery delay", |
| 93 | () => |
| 94 | Effect.gen(function* () { |
| 95 | const fixture = yield* makeAlarmScheduler; |
| 96 | const callback = yield* fixture.register( |
| 97 | "slow", |
| 98 | () => |
| 99 | Effect.gen(function* () { |
| 100 | yield* TestClock.adjust("10 seconds"); |
| 101 | return yield* Effect.fail("still unavailable"); |
| 102 | }), |
| 103 | { retry: { delay: "1 second" } }, |
| 104 | ); |
| 105 | fixture.seal(); |
| 106 | yield* callback.schedule("a", { after: 0, payload: null }); |
| 107 | yield* fixture.fire; |
| 108 | expect(fixture.alarm! - (yield* Clock.currentTimeMillis)).toBe(1_000); |
| 109 | }).pipe(Effect.provide(RuntimeContext.phantom)), |
| 110 | ); |
| 111 | |
| 112 | it.effect("parks repeated failures and preserves parking after reconstruction", () => |
| 113 | Effect.gen(function* () { |
| 114 | const fixture = yield* makeAlarmScheduler; |
| 115 | const register = () => |
| 116 | fixture.register("broken", () => Effect.fail("unavailable"), { |
| 117 | retry: { delay: "1 second" }, |
| 118 | }); |
| 119 | const callback = yield* register(); |
| 120 | fixture.seal(); |
| 121 | yield* callback.schedule("a", { after: 0, payload: null }); |
| 122 | for (let attempt = 0; attempt < 8; attempt++) yield* fixture.fire; |
| 123 | expect(fixture.alarm! - (yield* Clock.currentTimeMillis)).toBe(3_600_000); |
| 124 | fixture.restart(); |
| 125 | yield* register(); |
| 126 | fixture.seal(); |
| 127 | yield* fixture.fire; |
| 128 | expect(fixture.alarm! - (yield* Clock.currentTimeMillis)).toBe(3_600_000); |
| 129 | }).pipe(Effect.provide(RuntimeContext.phantom)), |
| 130 | ); |
| 131 | |
| 132 | it.effect("enforces a one-second floor for subsecond retry delays", () => |
| 133 | Effect.gen(function* () { |
| 134 | const fixture = yield* makeAlarmScheduler; |
| 135 | const callback = yield* fixture.register("broken", () => Effect.fail("unavailable"), { |
| 136 | retry: { delay: "1 millis" }, |
| 137 | }); |
| 138 | fixture.seal(); |
| 139 | yield* callback.schedule("a", { after: 0, payload: null }); |
| 140 | yield* fixture.fire; |
| 141 | expect(fixture.alarm! - (yield* Clock.currentTimeMillis)).toBe(1_000); |
| 142 | }).pipe(Effect.provide(RuntimeContext.phantom)), |
| 143 | ); |
| 144 | |
| 145 | it.effect("rejects a detached self-rearm after the callback invocation ends", () => |
| 146 | Effect.gen(function* () { |
| 147 | const fixture = yield* makeAlarmScheduler; |
| 148 | const release = yield* Deferred.make<void>(); |
| 149 | let detached: Fiber.Fiber<void, unknown> | undefined; |
| 150 | const callback: Callback<null> = yield* fixture.register("detached", () => |
| 151 | Effect.gen(function* () { |
| 152 | detached = yield* Deferred.await(release).pipe( |
| 153 | Effect.andThen(() => callback.schedule("a", { after: 0, payload: null })), |
| 154 | Effect.forkDetach, |
| 155 | ); |
| 156 | }), |
| 157 | ); |
| 158 | fixture.seal(); |
| 159 | yield* callback.schedule("a", { after: 0, payload: null }); |
| 160 | yield* fixture.fire; |
| 161 | yield* Deferred.succeed(release, undefined); |
| 162 | expect(Exit.isFailure(yield* Fiber.await(detached!))).toBe(true); |
| 163 | expect(fixture.alarm).toBeNull(); |
| 164 | }).pipe(Effect.provide(RuntimeContext.phantom)), |
| 165 | ); |
| 166 | |
| 167 | it.effect("continues unrelated due work when a callback fails", () => |
| 168 | Effect.gen(function* () { |
| 169 | const fixture = yield* makeAlarmScheduler; |
| 170 | let delivered = false; |
| 171 | const broken = yield* fixture.register("a", () => Effect.fail("unavailable")); |
| 172 | const healthy = yield* fixture.register("b", () => |
| 173 | Effect.sync(() => { |
| 174 | delivered = true; |
| 175 | }), |
| 176 | ); |
| 177 | fixture.seal(); |
| 178 | yield* broken.schedule("a", { after: 0, payload: null }); |
| 179 | yield* healthy.schedule("b", { after: 0, payload: null }); |
| 180 | yield* fixture.fire; |
| 181 | expect(delivered).toBe(true); |
| 182 | }).pipe(Effect.provide(RuntimeContext.phantom)), |
| 183 | ); |
| 184 | |
| 185 | it.effect( |
| 186 | "preserves the requested deadline and inspects status without decoding payloads", |
| 187 | () => |
| 188 | Effect.gen(function* () { |
| 189 | const fixture = yield* makeAlarmScheduler; |
| 190 | const callback = yield* fixture.register("broken", () => Effect.fail("unavailable"), { |
| 191 | retry: { delay: "1 second" }, |
| 192 | }); |
| 193 | fixture.seal(); |
| 194 | yield* callback.schedule("a", { |
| 195 | at: 15_000, |
| 196 | payload: null, |
| 197 | progress: 0, |
| 198 | }); |
| 199 | expect(yield* callback.getStatus("a")).toEqual({ |
| 200 | scheduledAt: 15_000, |
| 201 | retryAt: undefined, |
| 202 | attempts: 0, |
| 203 | parked: false, |
| 204 | progress: 0, |
| 205 | }); |
| 206 | yield* fixture.fire; |
| 207 | const status = yield* callback.getStatus("a"); |
| 208 | expect(status).toEqual({ |
| 209 | scheduledAt: 15_000, |
| 210 | retryAt: 16_000, |
| 211 | attempts: 1, |
| 212 | parked: false, |
| 213 | progress: 0, |
| 214 | }); |
| 215 | fixture.db.run("UPDATE alchemy_alarm_callbacks SET payload = 'invalid json'"); |
| 216 | expect(yield* callback.getStatus("a")).toEqual(status); |
| 217 | yield* callback.cancel("a"); |
| 218 | expect(yield* callback.getStatus("a")).toBeUndefined(); |
| 219 | expect(fixture.alarm).toBeNull(); |
| 220 | }).pipe(Effect.provide(RuntimeContext.phantom)), |
| 221 | ); |
| 222 | |
| 223 | it.effect( |
| 224 | "keeps parked jobs parked when the budget grows and resumes only on new progress or external enrollment", |
| 225 | () => |
| 226 | Effect.gen(function* () { |
| 227 | const fixture = yield* makeAlarmScheduler; |
| 228 | const register = (maxAttempts: number) => |
| 229 | fixture.register("broken", () => Effect.fail("unavailable"), { |
| 230 | retry: { |
| 231 | delay: "2 seconds", |
| 232 | maxAttempts, |
| 233 | parkedDelay: "2 hours", |
| 234 | }, |
| 235 | }); |
| 236 | let callback = yield* register(2); |
| 237 | fixture.seal(); |
| 238 | yield* callback.schedule("a", { |
| 239 | after: 0, |
| 240 | payload: null, |
| 241 | progress: 1, |
| 242 | }); |
| 243 | yield* fixture.fire; |
| 244 | yield* fixture.fire; |
| 245 | const parked = yield* callback.getStatus("a"); |
| 246 | expect(parked?.parked).toBe(true); |
| 247 | expect(parked?.attempts).toBe(2); |
| 248 | expect(fixture.alarm! - (yield* Clock.currentTimeMillis)).toBe(7_200_000); |
| 249 | fixture.restart(); |
| 250 | callback = yield* register(20); |
| 251 | fixture.seal(); |
| 252 | yield* fixture.fire; |
| 253 | expect((yield* callback.getStatus("a"))?.attempts).toBe(2); |
| 254 | expect((yield* callback.getStatus("a"))?.parked).toBe(true); |
| 255 | const nextWake = fixture.alarm; |
| 256 | for (const progress of [0, 1]) { |
| 257 | yield* callback.schedule("a", { |
| 258 | after: 0, |
| 259 | payload: "stale", |
| 260 | progress, |
| 261 | }); |
| 262 | expect(fixture.alarm).toBe(nextWake); |
| 263 | } |
| 264 | yield* callback.schedule("a", { |
| 265 | after: 0, |
| 266 | payload: "new", |
| 267 | progress: 2, |
| 268 | }); |
| 269 | expect(yield* callback.getStatus("a")).toEqual({ |
| 270 | scheduledAt: yield* Clock.currentTimeMillis, |
| 271 | retryAt: undefined, |
| 272 | attempts: 0, |
| 273 | parked: false, |
| 274 | progress: 2, |
| 275 | }); |
| 276 | yield* fixture.fire; |
| 277 | yield* callback.schedule("a", { |
| 278 | after: 0, |
| 279 | payload: "explicit recovery", |
| 280 | }); |
| 281 | expect((yield* callback.getStatus("a"))?.attempts).toBe(0); |
| 282 | expect((yield* callback.getStatus("a"))?.progress).toBe(2); |
| 283 | }).pipe(Effect.provide(RuntimeContext.phantom)), |
| 284 | ); |
| 285 | |
| 286 | it.effect("weighs another callback's cursor without letting it hide a schedule", () => |
| 287 | Effect.gen(function* () { |
| 288 | const fixture = yield* makeAlarmScheduler; |
| 289 | const delivered: string[] = []; |
| 290 | const target = yield* fixture.register( |
| 291 | "target", |
| 292 | (id: string) => Effect.sync(() => delivered.push(id)), |
| 293 | { retry: { delay: "1 second" } }, |
| 294 | ); |
| 295 | const source = yield* fixture.register( |
| 296 | "source", |
| 297 | () => |
| 298 | Effect.gen(function* () { |
| 299 | yield* target.schedule("behind", { after: 0, payload: "behind", progress: 1 }); |
| 300 | yield* target.schedule("past", { after: 0, payload: "past", progress: 101 }); |
| 301 | }), |
| 302 | { retry: { delay: "1 second" } }, |
| 303 | ); |
| 304 | fixture.seal(); |
| 305 | yield* source.schedule("s", { after: 0, payload: null, progress: 100 }); |
| 306 | yield* fixture.fire; |
| 307 | // Behind the source's cursor: scheduled under its own cursor, with the |
| 308 | // source's budget. Past it: progress, so a fresh budget. |
| 309 | expect(yield* target.getStatus("behind")).toMatchObject({ attempts: 1, progress: 1 }); |
| 310 | expect(yield* target.getStatus("past")).toMatchObject({ attempts: 0, progress: 101 }); |
| 311 | yield* fixture.fire; |
| 312 | expect(delivered).toEqual(["past"]); |
| 313 | yield* fixture.fire; |
| 314 | expect(delivered).toEqual(["past", "behind"]); |
| 315 | }).pipe(Effect.provide(RuntimeContext.phantom)), |
| 316 | ); |
| 317 | |
| 318 | for (const changeId of [false, true]) { |
| 319 | it.effect( |
| 320 | `resets the budget for committed forward progress${changeId ? " across IDs" : ""}`, |
| 321 | () => |
| 322 | Effect.gen(function* () { |
| 323 | const fixture = yield* makeAlarmScheduler; |
| 324 | fixture.db.exec( |
| 325 | "CREATE TABLE source (version INTEGER NOT NULL); INSERT INTO source VALUES (0)", |
| 326 | ); |
| 327 | let version = 0; |
| 328 | const callback: Callback<null> = yield* fixture.register( |
| 329 | "progress", |
| 330 | () => |
| 331 | fixture.state.storage.transaction( |
| 332 | Effect.gen(function* () { |
| 333 | version++; |
| 334 | yield* fixture.state.storage.sql.exec("UPDATE source SET version = ?", version); |
| 335 | yield* callback.schedule(String(changeId ? version : 0), { |
| 336 | after: 0, |
| 337 | payload: null, |
| 338 | progress: version, |
| 339 | }); |
| 340 | }), |
| 341 | ), |
| 342 | { retry: { delay: "1 second", maxAttempts: 2 } }, |
| 343 | ); |
| 344 | fixture.seal(); |
| 345 | yield* callback.schedule("0", { |
| 346 | after: 0, |
| 347 | payload: null, |
| 348 | progress: 0, |
| 349 | }); |
| 350 | for (let i = 1; i <= 8; i++) { |
| 351 | yield* fixture.fire; |
| 352 | const status = yield* callback.getStatus(String(changeId ? i : 0)); |
| 353 | expect(status?.attempts).toBe(0); |
| 354 | expect(status?.parked).toBe(false); |
| 355 | expect(status?.progress).toBe(i); |
| 356 | expect(fixture.alarm! - (yield* Clock.currentTimeMillis)).toBe(1); |
| 357 | } |
| 358 | expect(fixture.db.query("SELECT version FROM source").get()).toEqual({ version: 8 }); |
| 359 | }).pipe(Effect.provide(RuntimeContext.phantom)), |
| 360 | ); |
| 361 | } |
| 362 | |
| 363 | it.effect( |
| 364 | "rolls back application progress and self-rearms together without undoing the persisted recovery attempt", |
| 365 | () => |
| 366 | Effect.gen(function* () { |
| 367 | const fixture = yield* makeAlarmScheduler; |
| 368 | fixture.db.exec( |
| 369 | "CREATE TABLE source (version INTEGER NOT NULL); INSERT INTO source VALUES (0)", |
| 370 | ); |
| 371 | const callback: Callback<null> = yield* fixture.register( |
| 372 | "rollback", |
| 373 | () => |
| 374 | fixture.state.storage.transaction( |
| 375 | Effect.gen(function* () { |
| 376 | yield* fixture.state.storage.sql.exec("UPDATE source SET version = 1"); |
| 377 | yield* callback.schedule("a", { |
| 378 | after: 0, |
| 379 | payload: null, |
| 380 | progress: 1, |
| 381 | }); |
| 382 | return yield* Effect.fail("rollback progress"); |
| 383 | }), |
| 384 | ), |
| 385 | { retry: { delay: "1 second", maxAttempts: 1 } }, |
| 386 | ); |
| 387 | fixture.seal(); |
| 388 | yield* callback.schedule("a", { |
| 389 | at: 15_000, |
| 390 | payload: null, |
| 391 | progress: 0, |
| 392 | }); |
| 393 | yield* fixture.fire; |
| 394 | expect(fixture.db.query("SELECT version FROM source").get()).toEqual({ |
| 395 | version: 0, |
| 396 | }); |
| 397 | expect(yield* callback.getStatus("a")).toEqual({ |
| 398 | scheduledAt: 15_000, |
| 399 | retryAt: 3_615_000, |
| 400 | attempts: 1, |
| 401 | parked: true, |
| 402 | progress: 0, |
| 403 | }); |
| 404 | expect(fixture.alarm).toBe(3_615_000); |
| 405 | }).pipe(Effect.provide(RuntimeContext.phantom)), |
| 406 | ); |
| 407 | |
| 408 | it.effect( |
| 409 | "charges multiple self-rearms followed by failure only once and preserves the last replacement", |
| 410 | () => |
| 411 | Effect.gen(function* () { |
| 412 | const fixture = yield* makeAlarmScheduler; |
| 413 | const callback: Callback<string> = yield* fixture.register( |
| 414 | "replace", |
| 415 | () => |
| 416 | Effect.gen(function* () { |
| 417 | yield* callback.schedule("a", { |
| 418 | after: "1 minute", |
| 419 | payload: "first", |
| 420 | }); |
| 421 | yield* callback.schedule("a", { |
| 422 | after: "2 minutes", |
| 423 | payload: "last", |
| 424 | }); |
| 425 | return yield* Effect.fail("old attempt failed"); |
| 426 | }), |
| 427 | { retry: { delay: "1 second" } }, |
| 428 | ); |
| 429 | fixture.seal(); |
| 430 | yield* callback.schedule("a", { after: 0, payload: "initial" }); |
| 431 | yield* fixture.fire; |
| 432 | const now = yield* Clock.currentTimeMillis; |
| 433 | expect(yield* callback.getStatus("a")).toEqual({ |
| 434 | scheduledAt: now + 120_000, |
| 435 | retryAt: now + 1_000, |
| 436 | attempts: 1, |
| 437 | parked: false, |
| 438 | progress: undefined, |
| 439 | }); |
| 440 | expect(fixture.alarm).toBe(now + 120_000); |
| 441 | expect(fixture.db.query("SELECT payload FROM alchemy_alarm_callbacks").get()).toEqual({ |
| 442 | payload: '"last"', |
| 443 | }); |
| 444 | }).pipe(Effect.provide(RuntimeContext.phantom)), |
| 445 | ); |
| 446 | |
| 447 | it.effect("does not overwrite an external replacement when an older delivery fails", () => |
| 448 | Effect.gen(function* () { |
| 449 | const fixture = yield* makeAlarmScheduler; |
| 450 | const entered = yield* Deferred.make<void>(); |
| 451 | const release = yield* Deferred.make<void>(); |
| 452 | const callback = yield* fixture.register( |
| 453 | "replace", |
| 454 | () => |
| 455 | Effect.gen(function* () { |
| 456 | yield* Deferred.succeed(entered, undefined); |
| 457 | yield* Deferred.await(release); |
| 458 | return yield* Effect.fail("stale failure"); |
| 459 | }), |
| 460 | { retry: { delay: "1 second" } }, |
| 461 | ); |
| 462 | fixture.seal(); |
| 463 | yield* callback.schedule("a", { |
| 464 | after: 0, |
| 465 | payload: null, |
| 466 | progress: 1, |
| 467 | }); |
| 468 | const delivery = yield* fixture.fire.pipe(Effect.forkChild); |
| 469 | yield* Deferred.await(entered); |
| 470 | yield* callback.schedule("a", { |
| 471 | after: "5 minutes", |
| 472 | payload: "replacement", |
| 473 | progress: 2, |
| 474 | }); |
| 475 | const replacement = yield* callback.getStatus("a"); |
| 476 | const wake = fixture.alarm; |
| 477 | yield* Deferred.succeed(release, undefined); |
| 478 | yield* Fiber.join(delivery); |
| 479 | expect(yield* callback.getStatus("a")).toEqual(replacement); |
| 480 | expect(fixture.alarm).toBe(wake); |
| 481 | }).pipe(Effect.provide(RuntimeContext.phantom)), |
| 482 | ); |
| 483 | |
| 484 | it.effect("parks missing handlers and delivers retained work when the handler returns", () => |
| 485 | Effect.gen(function* () { |
| 486 | const fixture = yield* makeAlarmScheduler; |
| 487 | const callback = yield* fixture.register("removed", () => Effect.void); |
| 488 | fixture.seal(); |
| 489 | yield* callback.schedule("a", { after: 0, payload: null }); |
| 490 | fixture.restart(); |
| 491 | fixture.seal(); |
| 492 | for (let i = 0; i < 8; i++) yield* fixture.fire; |
| 493 | expect((yield* callback.getStatus("a"))?.parked).toBe(true); |
| 494 | expect(fixture.alarm! - (yield* Clock.currentTimeMillis)).toBe(3_600_000); |
| 495 | fixture.restart(); |
| 496 | let delivered = false; |
| 497 | yield* fixture.register("removed", () => |
| 498 | Effect.sync(() => { |
| 499 | delivered = true; |
| 500 | }), |
| 501 | ); |
| 502 | fixture.seal(); |
| 503 | yield* fixture.fire; |
| 504 | expect(delivered).toBe(true); |
| 505 | expect(yield* callback.getStatus("a")).toBeUndefined(); |
| 506 | expect(fixture.alarm).toBeNull(); |
| 507 | }).pipe(Effect.provide(RuntimeContext.phantom)), |
| 508 | ); |
| 509 | |
| 510 | it.effect( |
| 511 | "migrates version-one callback rows atomically while preserving their native wake", |
| 512 | () => |
| 513 | Effect.gen(function* () { |
| 514 | const fixture = yield* makeAlarmScheduler; |
| 515 | fixture.db.exec(` |
| 516 | CREATE TABLE alchemy_alarm_schema (id INTEGER PRIMARY KEY, version INTEGER NOT NULL); |
| 517 | INSERT INTO alchemy_alarm_schema VALUES (1, 1); |
| 518 | CREATE TABLE alchemy_scheduled_events (id TEXT PRIMARY KEY, run_at INTEGER NOT NULL, repeat_ms INTEGER, payload TEXT NOT NULL); |
| 519 | CREATE TABLE alchemy_alarm_callbacks ( |
| 520 | callback TEXT NOT NULL, id TEXT NOT NULL, version TEXT NOT NULL, |
| 521 | run_at INTEGER NOT NULL, payload TEXT NOT NULL, PRIMARY KEY (callback, id) |
| 522 | ); |
| 523 | CREATE INDEX idx_alchemy_alarm_callbacks_run_at ON alchemy_alarm_callbacks (run_at, callback, id); |
| 524 | INSERT INTO alchemy_alarm_callbacks VALUES ('legacy', 'a', 'existing-version', 15000, 'null'); |
| 525 | `); |
| 526 | yield* Effect.promise(() => fixture.raw.storage.setAlarm(15_000)); |
| 527 | const before = fixture.db.query("SELECT * FROM sqlite_master ORDER BY name").all(); |
| 528 | const callback = yield* fixture.register("legacy", () => Effect.void); |
| 529 | fixture.seal(); |
| 530 | const rollback = yield* fixture.state.storage |
| 531 | .transaction( |
| 532 | Effect.gen(function* () { |
| 533 | yield* callback.getStatus("a"); |
| 534 | return yield* Effect.fail("rollback migration"); |
| 535 | }), |
| 536 | ) |
| 537 | .pipe(Effect.exit); |
| 538 | expect(Exit.isFailure(rollback)).toBe(true); |
| 539 | expect(fixture.db.query("SELECT * FROM sqlite_master ORDER BY name").all()).toEqual( |
| 540 | before, |
| 541 | ); |
| 542 | expect(fixture.alarm).toBe(15_000); |
| 543 | expect(yield* callback.getStatus("a")).toEqual({ |
| 544 | scheduledAt: 15_000, |
| 545 | retryAt: undefined, |
| 546 | attempts: 0, |
| 547 | parked: false, |
| 548 | progress: undefined, |
| 549 | }); |
| 550 | expect(fixture.db.query("SELECT version FROM alchemy_alarm_schema").get()).toEqual({ |
| 551 | version: 2, |
| 552 | }); |
| 553 | expect( |
| 554 | fixture.db.query("SELECT version, payload FROM alchemy_alarm_callbacks").get(), |
| 555 | ).toEqual({ version: "existing-version", payload: "null" }); |
| 556 | expect(fixture.alarm).toBe(15_000); |
| 557 | yield* fixture.fire; |
| 558 | expect(fixture.alarm).toBeNull(); |
| 559 | }).pipe(Effect.provide(RuntimeContext.phantom)), |
| 560 | ); |
| 561 | |
| 562 | for (const retry of [ |
| 563 | { delay: 0 }, |
| 564 | { delay: Infinity }, |
| 565 | { maxAttempts: 0 }, |
| 566 | { maxAttempts: 1.5 }, |
| 567 | { maxAttempts: Infinity }, |
| 568 | { parkedDelay: "59 minutes" }, |
| 569 | { parkedDelay: Infinity }, |
| 570 | { delay: "2 hours", parkedDelay: "1 hour" }, |
| 571 | ] satisfies NonNullable<CallbackOptions["retry"]>[]) { |
| 572 | it.effect(`rejects invalid retry policy ${JSON.stringify(retry)}`, () => |
| 573 | Effect.gen(function* () { |
| 574 | const fixture = yield* makeAlarmScheduler; |
| 575 | const exit = yield* fixture |
| 576 | .register("invalid", () => Effect.void, { retry }) |
| 577 | .pipe(Effect.exit); |
| 578 | expect(Exit.isFailure(exit)).toBe(true); |
| 579 | if (Exit.isFailure(exit)) expect(Cause.squash(exit.cause)).toBeInstanceOf(CallbackError); |
| 580 | expect(fixture.alarm).toBeNull(); |
| 581 | }).pipe(Effect.provide(RuntimeContext.phantom)), |
| 582 | ); |
| 583 | } |
| 584 | |
| 585 | it.effect("rejects invalid source cursors without replacing pending work", () => |
| 586 | Effect.gen(function* () { |
| 587 | const fixture = yield* makeAlarmScheduler; |
| 588 | const callback = yield* fixture.register("invalid", () => Effect.void); |
| 589 | fixture.seal(); |
| 590 | yield* callback.schedule("a", { after: "1 minute", payload: null }); |
| 591 | const before = yield* callback.getStatus("a"); |
| 592 | for (const progress of [-1, 1.5, Infinity, Number.MAX_SAFE_INTEGER + 1]) { |
| 593 | const exit = yield* callback |
| 594 | .schedule("a", { after: 0, payload: null, progress }) |
| 595 | .pipe(Effect.exit); |
| 596 | expect(Exit.isFailure(exit)).toBe(true); |
| 597 | expect(yield* callback.getStatus("a")).toEqual(before); |
| 598 | } |
| 599 | }).pipe(Effect.provide(RuntimeContext.phantom)), |
| 600 | ); |
| 601 | it.effect("reports committed parking once even when a later rearm rolls back", () => { |
| 602 | const reports: unknown[][] = []; |
| 603 | const logger = Logger.make(({ message }) => { |
| 604 | if (Array.isArray(message) && message[0] === "Durable Object alarm callback parked") { |
| 605 | reports.push(message); |
| 606 | } |
| 607 | }); |
| 608 | return Effect.gen(function* () { |
| 609 | const fixture = yield* makeAlarmScheduler; |
| 610 | const callback: Callback<string> = yield* fixture.register( |
| 611 | "PRIVATE_CALLBACK", |
| 612 | () => |
| 613 | fixture.state.storage.transaction( |
| 614 | Effect.gen(function* () { |
| 615 | yield* callback.schedule("PRIVATE_ID", { |
| 616 | after: 0, |
| 617 | payload: "PRIVATE_PAYLOAD", |
| 618 | }); |
| 619 | return yield* Effect.fail("rollback rearm"); |
| 620 | }), |
| 621 | ), |
| 622 | { retry: { delay: "1 second", maxAttempts: 1 } }, |
| 623 | ); |
| 624 | fixture.seal(); |
| 625 | yield* callback.schedule("PRIVATE_ID", { |
| 626 | after: 0, |
| 627 | payload: "PRIVATE_PAYLOAD", |
| 628 | }); |
| 629 | yield* fixture.fire; |
| 630 | expect(reports).toHaveLength(1); |
| 631 | expect(reports[0]).toEqual([ |
| 632 | "Durable Object alarm callback parked", |
| 633 | { attempts: 1, retryAt: fixture.alarm }, |
| 634 | ]); |
| 635 | yield* fixture.fire; |
| 636 | expect(reports).toHaveLength(1); |
| 637 | }).pipe( |
| 638 | Effect.provideService(Logger.CurrentLoggers, new Set([logger])), |
| 639 | Effect.provide(RuntimeContext.phantom), |
| 640 | ); |
| 641 | }); |
| 642 | }, |
| 643 | ); |