@dudousxd/nestjs-durable-testing 0.22.0 → 0.22.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -4427,6 +4427,99 @@ function doubled(fanSize, settle2) {
4427
4427
  })) : values;
4428
4428
  }
4429
4429
  __name(doubled, "doubled");
4430
+ function holdingTurn(inner, hold) {
4431
+ let release;
4432
+ const gate = new Promise((resolve) => {
4433
+ release = resolve;
4434
+ });
4435
+ const state = {
4436
+ held: false,
4437
+ release: /* @__PURE__ */ __name(() => release(), "release")
4438
+ };
4439
+ const store = new Proxy(inner, {
4440
+ get(target, prop, receiver) {
4441
+ const value = Reflect.get(target, prop, receiver);
4442
+ if (typeof value !== "function") return value;
4443
+ const fn = value;
4444
+ const holds = /* @__PURE__ */ __name((args) => {
4445
+ if (state.held) return false;
4446
+ if (hold === "suspend" && (prop === "updateRun" || prop === "updateRunIf")) {
4447
+ const patch = prop === "updateRun" ? args[1] : args[2];
4448
+ return args[0] === "p" && patch.status === "suspended";
4449
+ }
4450
+ if (hold === "placeholder" && prop === "saveCheckpoint") {
4451
+ const cp2 = args[0];
4452
+ return cp2.runId === "p" && cp2.status === "running" && cp2.name.startsWith("signal:child:");
4453
+ }
4454
+ return false;
4455
+ }, "holds");
4456
+ return async (...args) => {
4457
+ if (holds(args)) {
4458
+ state.held = true;
4459
+ await gate;
4460
+ }
4461
+ return fn.apply(target, args);
4462
+ };
4463
+ }
4464
+ });
4465
+ return {
4466
+ store,
4467
+ state
4468
+ };
4469
+ }
4470
+ __name(holdingTurn, "holdingTurn");
4471
+ var MID_TURN = [
4472
+ {
4473
+ what: "ctx.child's completion",
4474
+ body: /* @__PURE__ */ __name((ctx) => ctx.child("child", {}), "body"),
4475
+ output: "done",
4476
+ signal: false
4477
+ },
4478
+ {
4479
+ what: "ctx.child's completion, raced against its timeline placeholder",
4480
+ body: /* @__PURE__ */ __name((ctx) => ctx.child("child", {}), "body"),
4481
+ output: "done",
4482
+ signal: false,
4483
+ hold: "placeholder"
4484
+ },
4485
+ {
4486
+ what: "a ctx.all fan's completions",
4487
+ body: /* @__PURE__ */ __name((ctx) => ctx.all("child", [
4488
+ {},
4489
+ {},
4490
+ {}
4491
+ ]), "body"),
4492
+ output: [
4493
+ "done",
4494
+ "done",
4495
+ "done"
4496
+ ],
4497
+ signal: false
4498
+ },
4499
+ {
4500
+ what: "a ctx.allSettled fan's completions",
4501
+ body: /* @__PURE__ */ __name((ctx) => ctx.allSettled("child", [
4502
+ {},
4503
+ {},
4504
+ {}
4505
+ ]), "body"),
4506
+ output: [
4507
+ 1,
4508
+ 2,
4509
+ 3
4510
+ ].map(() => ({
4511
+ status: "fulfilled",
4512
+ value: "done"
4513
+ })),
4514
+ signal: false
4515
+ },
4516
+ {
4517
+ what: "a signal",
4518
+ body: /* @__PURE__ */ __name((ctx) => ctx.waitForSignal("go"), "body"),
4519
+ output: "sent",
4520
+ signal: true
4521
+ }
4522
+ ];
4430
4523
  async function shutdown(all, transports) {
4431
4524
  await Promise.allSettled(all.map(({ engine }) => engine.drain(2e3)));
4432
4525
  await Promise.allSettled(transports.map((t) => t.close?.()));
@@ -5035,6 +5128,65 @@ function runRunDispatchContract(name, makeHarness, options = {}) {
5035
5128
  await shutdown(all, pair);
5036
5129
  }
5037
5130
  }, timeoutMs + 1e4);
5131
+ for (const { what, body, output, signal, hold = "suspend" } of MID_TURN) {
5132
+ it3(`${what}, landing while the parent's turn runs on the other instance, resumes it once that turn ends`, async (ctx) => {
5133
+ const pair = await pairOrSkip(() => ctx.skip());
5134
+ if (!pair) return;
5135
+ const inner = new InMemoryStateStore4();
5136
+ const { store, state } = holdingTurn(inner, hold);
5137
+ const now = Date.now();
5138
+ const [first, second] = pair;
5139
+ const busy = new Proxy(first, {
5140
+ get(target, prop, receiver) {
5141
+ if (prop === "onDispatchedRun") {
5142
+ return (handler, opts) => target.onDispatchedRun?.(handler, {
5143
+ ...opts,
5144
+ concurrency: 1
5145
+ });
5146
+ }
5147
+ const value = Reflect.get(target, prop, receiver);
5148
+ return typeof value === "function" ? value.bind(target) : value;
5149
+ }
5150
+ });
5151
+ const engines = [
5152
+ busy,
5153
+ second
5154
+ ].map((transport) => new WorkflowEngine5({
5155
+ store,
5156
+ transport,
5157
+ clock: /* @__PURE__ */ __name(() => now, "clock")
5158
+ }));
5159
+ for (const engine of engines) {
5160
+ engine.register("child", "1", async () => "done");
5161
+ engine.register("parent", "1", async (wctx) => body(wctx));
5162
+ }
5163
+ const [holder, other] = engines;
5164
+ try {
5165
+ holder.consumeRuns();
5166
+ await holder.start("parent", {}, "p");
5167
+ await until(() => state.held, timeoutMs);
5168
+ other.consumeRuns();
5169
+ if (signal) await other.signal("go", "sent");
5170
+ else if (hold === "suspend") {
5171
+ const n = Array.isArray(output) ? output.length : 1;
5172
+ await until(async () => (await inner.listRuns({
5173
+ workflow: "child",
5174
+ status: "completed"
5175
+ })).length >= n, timeoutMs);
5176
+ }
5177
+ await pause(2600);
5178
+ state.release();
5179
+ await until(async () => (await inner.getRun("p"))?.status === "completed", timeoutMs);
5180
+ expect4((await inner.getRun("p"))?.output).toEqual(output);
5181
+ } finally {
5182
+ state.release();
5183
+ await shutdown(engines.map((engine, i) => ({
5184
+ name: `instance-${i}`,
5185
+ engine
5186
+ })), pair);
5187
+ }
5188
+ }, timeoutMs + 2e4);
5189
+ }
5038
5190
  });
5039
5191
  }
5040
5192
  __name(runRunDispatchContract, "runRunDispatchContract");