@dudousxd/nestjs-durable-testing 0.22.1 → 0.22.3

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
@@ -853,7 +853,7 @@ function runStateStoreContract(name, makeStore) {
853
853
  supportsTagFilter = ctx.supportsTagFilter ?? true;
854
854
  skipReason = void 0;
855
855
  } catch (err) {
856
- if (isUnavailable(err)) {
856
+ if (isUnavailable(err) && !process.env.DURABLE_TEST_REQUIRE_BACKENDS) {
857
857
  skipReason = err.message;
858
858
  if (!loggedSkip) {
859
859
  console.warn(`[state-store-contract] SKIPPING "${name}": ${skipReason}`);
@@ -1484,6 +1484,70 @@ function runStateStoreContract(name, makeStore) {
1484
1484
  "chatty-old"
1485
1485
  ]);
1486
1486
  });
1487
+ t("a parent joining children that already finished settles them from their rows", async () => {
1488
+ const engine = new WorkflowEngine3({
1489
+ store,
1490
+ reconcileMs: 0
1491
+ });
1492
+ engine.register("j-child", "1", async (_ctx, input) => ({
1493
+ n: input.n
1494
+ }));
1495
+ engine.register("j-parent", "1", async (ctx) => {
1496
+ const lone = await ctx.child("j-child", {
1497
+ n: 9
1498
+ }, "j-own-id");
1499
+ const fan = await ctx.allSettled("j-child", [
1500
+ {
1501
+ n: 0
1502
+ },
1503
+ {
1504
+ n: 1
1505
+ }
1506
+ ]);
1507
+ return {
1508
+ lone,
1509
+ fan
1510
+ };
1511
+ });
1512
+ await engine.start("j-child", {
1513
+ n: 9
1514
+ }, "j-own-id");
1515
+ await engine.start("j-child", {
1516
+ n: 1
1517
+ }, "jp.all.1.1");
1518
+ expect3((await engine.waitForRun("j-own-id", {
1519
+ timeoutMs: 2e4
1520
+ })).status).toBe("completed");
1521
+ expect3((await engine.waitForRun("jp.all.1.1", {
1522
+ timeoutMs: 2e4
1523
+ })).status).toBe("completed");
1524
+ await engine.start("j-parent", {}, "jp");
1525
+ const deadline = Date.now() + 2e4;
1526
+ while ((await store.getRun("jp"))?.status !== "completed") {
1527
+ if (Date.now() > deadline) throw new Error("parent never completed");
1528
+ await new Promise((resolve) => setTimeout(resolve, 20));
1529
+ }
1530
+ expect3((await store.getRun("jp"))?.output).toEqual({
1531
+ lone: {
1532
+ n: 9
1533
+ },
1534
+ fan: [
1535
+ {
1536
+ status: "fulfilled",
1537
+ value: {
1538
+ n: 0
1539
+ }
1540
+ },
1541
+ {
1542
+ status: "fulfilled",
1543
+ value: {
1544
+ n: 1
1545
+ }
1546
+ }
1547
+ ]
1548
+ });
1549
+ await engine.drain(5e3);
1550
+ });
1487
1551
  t("engine.purgeRuns deletes a scope with its child subtrees and cancels live runs", async () => {
1488
1552
  const engine = new WorkflowEngine3({
1489
1553
  store
@@ -3963,6 +4027,111 @@ function runStateStoreContract(name, makeStore) {
3963
4027
  seq: 0
3964
4028
  });
3965
4029
  });
4030
+ t("lists buffered signals by token prefix in insertion order, a page at a time", async () => {
4031
+ expect3(store.listBufferedSignals).toBeTypeOf("function");
4032
+ const list = /* @__PURE__ */ __name((prefix, after, limit) => store.listBufferedSignals?.(prefix, after, limit) ?? Promise.resolve([]), "list");
4033
+ await store.bufferSignal("child:a", {
4034
+ ok: true,
4035
+ value: {
4036
+ n: 1
4037
+ }
4038
+ });
4039
+ await store.bufferSignal("order:1", {
4040
+ paid: true
4041
+ });
4042
+ await store.bufferSignal("child:b", {
4043
+ ok: false,
4044
+ error: "x"
4045
+ });
4046
+ await store.bufferSignal("child:a", {
4047
+ ok: true,
4048
+ value: {
4049
+ n: 2
4050
+ }
4051
+ });
4052
+ await store.bufferSignal("wake_x:1", null);
4053
+ await store.bufferSignal("wakeyx:1", null);
4054
+ const all = await list("child:", void 0, 10);
4055
+ expect3(all.map((r) => [
4056
+ r.token,
4057
+ r.payload
4058
+ ])).toEqual([
4059
+ [
4060
+ "child:a",
4061
+ {
4062
+ ok: true,
4063
+ value: {
4064
+ n: 1
4065
+ }
4066
+ }
4067
+ ],
4068
+ [
4069
+ "child:b",
4070
+ {
4071
+ ok: false,
4072
+ error: "x"
4073
+ }
4074
+ ],
4075
+ [
4076
+ "child:a",
4077
+ {
4078
+ ok: true,
4079
+ value: {
4080
+ n: 2
4081
+ }
4082
+ }
4083
+ ]
4084
+ ]);
4085
+ expect3(all.map((r) => typeof r.id)).toEqual([
4086
+ "string",
4087
+ "string",
4088
+ "string"
4089
+ ]);
4090
+ const first = await list("child:", void 0, 2);
4091
+ expect3(first.map((r) => r.id)).toEqual(all.slice(0, 2).map((r) => r.id));
4092
+ const rest = await list("child:", first.at(-1)?.id, 2);
4093
+ expect3(rest.map((r) => r.id)).toEqual([
4094
+ all[2]?.id
4095
+ ]);
4096
+ expect3(await list("child:", all[2]?.id, 2)).toEqual([]);
4097
+ expect3((await list("wake_x:", void 0, 10)).map((r) => r.token)).toEqual([
4098
+ "wake_x:1"
4099
+ ]);
4100
+ });
4101
+ t("deletes buffered signals by id and leaves the rest in FIFO order", async () => {
4102
+ expect3(store.deleteBufferedSignals).toBeTypeOf("function");
4103
+ await store.bufferSignal("child:a", {
4104
+ n: 1
4105
+ });
4106
+ await store.bufferSignal("child:a", {
4107
+ n: 2
4108
+ });
4109
+ await store.bufferSignal("child:a", {
4110
+ n: 3
4111
+ });
4112
+ await store.bufferSignal("child:b", {
4113
+ n: 4
4114
+ });
4115
+ const rows = await store.listBufferedSignals?.("child:", void 0, 10) ?? [];
4116
+ await store.deleteBufferedSignals?.([
4117
+ rows[1]?.id,
4118
+ rows[3]?.id,
4119
+ "999999999"
4120
+ ]);
4121
+ await store.deleteBufferedSignals?.([]);
4122
+ expect3(await store.takeBufferedSignal("child:a")).toEqual({
4123
+ payload: {
4124
+ n: 1
4125
+ }
4126
+ });
4127
+ expect3(await store.takeBufferedSignal("child:a")).toEqual({
4128
+ payload: {
4129
+ n: 3
4130
+ }
4131
+ });
4132
+ expect3(await store.takeBufferedSignal("child:a")).toBeNull();
4133
+ expect3(await store.takeBufferedSignal("child:b")).toBeNull();
4134
+ });
3966
4135
  t("buffers signals and takes them FIFO per token", async () => {
3967
4136
  await store.bufferSignal("sig", {
3968
4137
  n: 1
@@ -4393,7 +4562,10 @@ function instances(store, transports, clock) {
4393
4562
  });
4394
4563
  }
4395
4564
  __name(instances, "instances");
4396
- function registerFan(all, fanSize, ran, settle2) {
4565
+ function registerFan(all, fanSize, ran, settle2, spreadWithinMs) {
4566
+ const executors = /* @__PURE__ */ __name(() => new Set([
4567
+ ...ran.values()
4568
+ ].map(([first]) => first)), "executors");
4397
4569
  for (const { name, engine } of all) {
4398
4570
  engine.register("child", "1", async (ctx, input) => {
4399
4571
  const { i } = input;
@@ -4402,6 +4574,9 @@ function registerFan(all, fanSize, ran, settle2) {
4402
4574
  ...ran.get(i) ?? [],
4403
4575
  name
4404
4576
  ]);
4577
+ if (spreadWithinMs !== void 0) {
4578
+ await until(() => executors().size >= all.length, spreadWithinMs).catch(() => void 0);
4579
+ }
4405
4580
  await pause(15);
4406
4581
  return i * 2;
4407
4582
  });
@@ -4583,7 +4758,7 @@ function runRunDispatchContract(name, makeHarness, options = {}) {
4583
4758
  const store = new InMemoryStateStore4();
4584
4759
  const all = instances(store, pair);
4585
4760
  const ran = /* @__PURE__ */ new Map();
4586
- registerFan(all, fanSize, ran, settle2);
4761
+ registerFan(all, fanSize, ran, settle2, timeoutMs / 2);
4587
4762
  try {
4588
4763
  for (const { engine } of all) engine.consumeRuns();
4589
4764
  await all[0]?.engine.start("parent", {}, "p");
@@ -4625,8 +4800,13 @@ function runRunDispatchContract(name, makeHarness, options = {}) {
4625
4800
  expect4(turns).toBe(0);
4626
4801
  a.failing.on = false;
4627
4802
  await engine.runPending();
4628
- await until(async () => (await store.getRun("lost"))?.status === "completed", timeoutMs);
4629
- expect4((await store.getRun("kept"))?.status).toBe("completed");
4803
+ await until(async () => {
4804
+ const [lost, kept] = await Promise.all([
4805
+ store.getRun("lost"),
4806
+ store.getRun("kept")
4807
+ ]);
4808
+ return lost?.status === "completed" && kept?.status === "completed";
4809
+ }, timeoutMs);
4630
4810
  a.failing.on = true;
4631
4811
  await engine.start("wf", {}, "down");
4632
4812
  await pause(50);
@@ -5081,6 +5261,59 @@ function runRunDispatchContract(name, makeHarness, options = {}) {
5081
5261
  await Promise.allSettled(pair.map((t) => t.close?.()));
5082
5262
  }
5083
5263
  }, timeoutMs + 1e4);
5264
+ for (const { fan, settle: settle2 } of FANS) {
5265
+ it3(`a ${fan} joined after some of its children finished settles them at once (reconcile off)`, async (ctx) => {
5266
+ const pair = await pairOrSkip(() => ctx.skip());
5267
+ if (!pair) return;
5268
+ const store = new InMemoryStateStore4();
5269
+ const all = instances(store, pair);
5270
+ for (const { engine } of all) {
5271
+ engine.register("line", "1", async (_ctx, input) => input.i * 10);
5272
+ engine.register("parent", "1", async (c) => {
5273
+ const inputs = [
5274
+ 0,
5275
+ 1,
5276
+ 2,
5277
+ 3
5278
+ ].map((i) => ({
5279
+ i
5280
+ }));
5281
+ return settle2 ? c.allSettled("line", inputs) : c.all("line", inputs);
5282
+ });
5283
+ }
5284
+ try {
5285
+ for (const { engine } of all) engine.consumeRuns();
5286
+ const [first] = all;
5287
+ for (const i of [
5288
+ 1,
5289
+ 3
5290
+ ]) await first.engine.start("line", {
5291
+ i
5292
+ }, `p.all.0.${i}`);
5293
+ await until(async () => {
5294
+ const runs = await Promise.all([
5295
+ 1,
5296
+ 3
5297
+ ].map((i) => store.getRun(`p.all.0.${i}`)));
5298
+ return runs.every((r) => r?.status === "completed");
5299
+ }, timeoutMs);
5300
+ await first.engine.start("parent", {}, "p");
5301
+ await until(async () => (await store.getRun("p"))?.status === "completed", timeoutMs);
5302
+ const values = [
5303
+ 0,
5304
+ 10,
5305
+ 20,
5306
+ 30
5307
+ ];
5308
+ expect4((await store.getRun("p"))?.output).toEqual(settle2 ? values.map((value) => ({
5309
+ status: "fulfilled",
5310
+ value
5311
+ })) : values);
5312
+ } finally {
5313
+ await shutdown(all, pair);
5314
+ }
5315
+ }, timeoutMs + 1e4);
5316
+ }
5084
5317
  it3("drain waits for a run taken off the queue, and the draining instance takes no more", async (ctx) => {
5085
5318
  const pair = await pairOrSkip(() => ctx.skip());
5086
5319
  if (!pair) return;