@dudousxd/nestjs-durable-testing 0.24.0 → 0.25.0
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.d.ts +5 -0
- package/dist/index.js +458 -0
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
package/dist/index.d.ts
CHANGED
|
@@ -121,6 +121,11 @@ declare const DURABLE_CANONICAL_COLUMNS: {
|
|
|
121
121
|
readonly payload: "payload";
|
|
122
122
|
readonly publishedAt: "published_at";
|
|
123
123
|
};
|
|
124
|
+
readonly durable_signal_ids: {
|
|
125
|
+
readonly token: "token";
|
|
126
|
+
readonly signalId: "signal_id";
|
|
127
|
+
readonly expiresAt: "expires_at";
|
|
128
|
+
};
|
|
124
129
|
readonly durable_schedules: {
|
|
125
130
|
readonly id: "id";
|
|
126
131
|
readonly namespace: "namespace";
|
package/dist/index.js
CHANGED
|
@@ -288,6 +288,13 @@ var DURABLE_CANONICAL_COLUMNS = {
|
|
|
288
288
|
payload: "payload",
|
|
289
289
|
publishedAt: "published_at"
|
|
290
290
|
},
|
|
291
|
+
// Signal dedupe ids (`engine.signal(token, payload, { id })`): one row per (token, id) until it
|
|
292
|
+
// expires. `expires_at` is when the id stops deduplicating.
|
|
293
|
+
durable_signal_ids: {
|
|
294
|
+
token: "token",
|
|
295
|
+
signalId: "signal_id",
|
|
296
|
+
expiresAt: "expires_at"
|
|
297
|
+
},
|
|
291
298
|
// Persisted schedules (`engine.schedules`). `spec`/`state` are engine-owned JSON documents.
|
|
292
299
|
durable_schedules: {
|
|
293
300
|
id: "id",
|
|
@@ -2747,6 +2754,326 @@ function runStateStoreContract(name, makeStore) {
|
|
|
2747
2754
|
prompt: "p0"
|
|
2748
2755
|
});
|
|
2749
2756
|
});
|
|
2757
|
+
t("settleResultsIfPending settles pending rows and reads them back without their input", async () => {
|
|
2758
|
+
if (!store.settleResultsIfPending) return;
|
|
2759
|
+
expect3(await store.settleResultsIfPending([])).toEqual([]);
|
|
2760
|
+
await store.createRun(run());
|
|
2761
|
+
await store.createRun(run({
|
|
2762
|
+
id: "r2"
|
|
2763
|
+
}));
|
|
2764
|
+
await store.createRun(run({
|
|
2765
|
+
id: "gone",
|
|
2766
|
+
status: "cancelled"
|
|
2767
|
+
}));
|
|
2768
|
+
const pending = /* @__PURE__ */ __name((runId, seq) => checkpoint({
|
|
2769
|
+
runId,
|
|
2770
|
+
seq,
|
|
2771
|
+
name: "ask",
|
|
2772
|
+
kind: "remote",
|
|
2773
|
+
stepId: `${runId}:${seq}`,
|
|
2774
|
+
status: "pending",
|
|
2775
|
+
input: {
|
|
2776
|
+
prompt: `p${seq}`
|
|
2777
|
+
},
|
|
2778
|
+
output: {
|
|
2779
|
+
partial: seq
|
|
2780
|
+
},
|
|
2781
|
+
events: [
|
|
2782
|
+
{
|
|
2783
|
+
at: 1,
|
|
2784
|
+
level: "info",
|
|
2785
|
+
message: "queued"
|
|
2786
|
+
}
|
|
2787
|
+
],
|
|
2788
|
+
attempts: 2,
|
|
2789
|
+
workerGroup: "llm",
|
|
2790
|
+
parallelGroup: "all:0:2",
|
|
2791
|
+
wakeAt: 5e3
|
|
2792
|
+
}), "pending");
|
|
2793
|
+
for (const seq of [
|
|
2794
|
+
0,
|
|
2795
|
+
1,
|
|
2796
|
+
2,
|
|
2797
|
+
3
|
|
2798
|
+
]) await store.saveCheckpoint(pending("r1", seq));
|
|
2799
|
+
await store.saveCheckpoint(pending("r2", 0));
|
|
2800
|
+
await store.saveCheckpoint(pending("gone", 0));
|
|
2801
|
+
await store.saveCheckpoint({
|
|
2802
|
+
...pending("r1", 1),
|
|
2803
|
+
status: "completed",
|
|
2804
|
+
output: {
|
|
2805
|
+
first: 1
|
|
2806
|
+
}
|
|
2807
|
+
});
|
|
2808
|
+
const later = new Date(at.getTime() + 6e4);
|
|
2809
|
+
const settled = await store.settleResultsIfPending([
|
|
2810
|
+
{
|
|
2811
|
+
runId: "r1",
|
|
2812
|
+
seq: 0,
|
|
2813
|
+
status: "completed",
|
|
2814
|
+
output: {
|
|
2815
|
+
n: 0
|
|
2816
|
+
},
|
|
2817
|
+
startedAt: new Date(at.getTime() + 1e3),
|
|
2818
|
+
finishedAt: later
|
|
2819
|
+
},
|
|
2820
|
+
// Already settled: nothing written.
|
|
2821
|
+
{
|
|
2822
|
+
runId: "r1",
|
|
2823
|
+
seq: 1,
|
|
2824
|
+
status: "completed",
|
|
2825
|
+
output: {
|
|
2826
|
+
n: 1
|
|
2827
|
+
},
|
|
2828
|
+
finishedAt: later
|
|
2829
|
+
},
|
|
2830
|
+
// A second result for the row the first one settles: loses.
|
|
2831
|
+
{
|
|
2832
|
+
runId: "r1",
|
|
2833
|
+
seq: 0,
|
|
2834
|
+
status: "completed",
|
|
2835
|
+
output: {
|
|
2836
|
+
n: "dup"
|
|
2837
|
+
},
|
|
2838
|
+
finishedAt: later
|
|
2839
|
+
},
|
|
2840
|
+
// A failure keeps the stored output, events and start; records the error.
|
|
2841
|
+
{
|
|
2842
|
+
runId: "r1",
|
|
2843
|
+
seq: 2,
|
|
2844
|
+
status: "failed",
|
|
2845
|
+
error: {
|
|
2846
|
+
message: "boom"
|
|
2847
|
+
},
|
|
2848
|
+
finishedAt: later
|
|
2849
|
+
},
|
|
2850
|
+
// No such row.
|
|
2851
|
+
{
|
|
2852
|
+
runId: "r1",
|
|
2853
|
+
seq: 9,
|
|
2854
|
+
status: "completed",
|
|
2855
|
+
output: {
|
|
2856
|
+
n: 9
|
|
2857
|
+
},
|
|
2858
|
+
finishedAt: later
|
|
2859
|
+
},
|
|
2860
|
+
// Another run's row at the same seq.
|
|
2861
|
+
{
|
|
2862
|
+
runId: "r2",
|
|
2863
|
+
seq: 0,
|
|
2864
|
+
status: "completed",
|
|
2865
|
+
output: {
|
|
2866
|
+
n: "r2"
|
|
2867
|
+
},
|
|
2868
|
+
finishedAt: later
|
|
2869
|
+
},
|
|
2870
|
+
// A finished run's step settles nothing.
|
|
2871
|
+
{
|
|
2872
|
+
runId: "gone",
|
|
2873
|
+
seq: 0,
|
|
2874
|
+
status: "completed",
|
|
2875
|
+
output: {
|
|
2876
|
+
n: "late"
|
|
2877
|
+
},
|
|
2878
|
+
finishedAt: later
|
|
2879
|
+
}
|
|
2880
|
+
]);
|
|
2881
|
+
expect3(settled.map((cp2) => cp2 ? [
|
|
2882
|
+
cp2.runId,
|
|
2883
|
+
cp2.seq,
|
|
2884
|
+
cp2.status
|
|
2885
|
+
] : null)).toEqual([
|
|
2886
|
+
[
|
|
2887
|
+
"r1",
|
|
2888
|
+
0,
|
|
2889
|
+
"completed"
|
|
2890
|
+
],
|
|
2891
|
+
null,
|
|
2892
|
+
null,
|
|
2893
|
+
[
|
|
2894
|
+
"r1",
|
|
2895
|
+
2,
|
|
2896
|
+
"failed"
|
|
2897
|
+
],
|
|
2898
|
+
null,
|
|
2899
|
+
[
|
|
2900
|
+
"r2",
|
|
2901
|
+
0,
|
|
2902
|
+
"completed"
|
|
2903
|
+
],
|
|
2904
|
+
null
|
|
2905
|
+
]);
|
|
2906
|
+
const [first, , , failed] = settled;
|
|
2907
|
+
expect3(first?.output).toEqual({
|
|
2908
|
+
n: 0
|
|
2909
|
+
});
|
|
2910
|
+
expect3(first?.input).toBeUndefined();
|
|
2911
|
+
expect3(first?.name).toBe("ask");
|
|
2912
|
+
expect3(first?.stepId).toBe("r1:0");
|
|
2913
|
+
expect3(first?.attempts).toBe(2);
|
|
2914
|
+
expect3(first?.parallelGroup).toBe("all:0:2");
|
|
2915
|
+
expect3(first?.events).toEqual([
|
|
2916
|
+
{
|
|
2917
|
+
at: 1,
|
|
2918
|
+
level: "info",
|
|
2919
|
+
message: "queued"
|
|
2920
|
+
}
|
|
2921
|
+
]);
|
|
2922
|
+
expect3(first?.startedAt.getTime()).toBe(at.getTime() + 1e3);
|
|
2923
|
+
expect3(first?.finishedAt.getTime()).toBe(later.getTime());
|
|
2924
|
+
expect3(first?.enqueuedAt.getTime()).toBe(at.getTime());
|
|
2925
|
+
expect3(failed?.output).toEqual({
|
|
2926
|
+
partial: 2
|
|
2927
|
+
});
|
|
2928
|
+
expect3(failed?.error).toEqual({
|
|
2929
|
+
message: "boom"
|
|
2930
|
+
});
|
|
2931
|
+
expect3(failed?.startedAt.getTime()).toBe(at.getTime());
|
|
2932
|
+
const stored = await store.getCheckpoint("r1", 0);
|
|
2933
|
+
expect3(stored?.output).toEqual({
|
|
2934
|
+
n: 0
|
|
2935
|
+
});
|
|
2936
|
+
expect3(stored?.input).toEqual({
|
|
2937
|
+
prompt: "p0"
|
|
2938
|
+
});
|
|
2939
|
+
expect3((await store.getCheckpoint("r1", 1))?.output).toEqual({
|
|
2940
|
+
first: 1
|
|
2941
|
+
});
|
|
2942
|
+
expect3((await store.getCheckpoint("r1", 3))?.status).toBe("pending");
|
|
2943
|
+
expect3((await store.getCheckpoint("gone", 0))?.status).toBe("pending");
|
|
2944
|
+
expect3(await store.getCheckpoint("r1", 9)).toBeNull();
|
|
2945
|
+
});
|
|
2946
|
+
t("insertCheckpointIfAbsent writes a position only once", async () => {
|
|
2947
|
+
if (!store.insertCheckpointIfAbsent) return;
|
|
2948
|
+
await store.createRun(run());
|
|
2949
|
+
const first = checkpoint({
|
|
2950
|
+
seq: 4,
|
|
2951
|
+
status: "pending",
|
|
2952
|
+
input: {
|
|
2953
|
+
a: 1
|
|
2954
|
+
},
|
|
2955
|
+
output: void 0
|
|
2956
|
+
});
|
|
2957
|
+
expect3(await store.insertCheckpointIfAbsent(first)).toBe(true);
|
|
2958
|
+
expect3(await store.insertCheckpointIfAbsent({
|
|
2959
|
+
...first,
|
|
2960
|
+
status: "completed",
|
|
2961
|
+
output: {
|
|
2962
|
+
b: 2
|
|
2963
|
+
}
|
|
2964
|
+
})).toBe(false);
|
|
2965
|
+
const stored = await store.getCheckpoint("r1", 4);
|
|
2966
|
+
expect3(stored?.status).toBe("pending");
|
|
2967
|
+
expect3(stored?.input).toEqual({
|
|
2968
|
+
a: 1
|
|
2969
|
+
});
|
|
2970
|
+
expect3(stored?.output).toBeUndefined();
|
|
2971
|
+
});
|
|
2972
|
+
t("claimPendingRuns leases the oldest free pending runs, and concurrent claims never overlap", async () => {
|
|
2973
|
+
if (!store.claimPendingRuns) return;
|
|
2974
|
+
const created2 = /* @__PURE__ */ __name((i) => new Date(at.getTime() + i * 1e3), "created");
|
|
2975
|
+
for (let i = 0; i < 6; i += 1) {
|
|
2976
|
+
await store.createRun(run({
|
|
2977
|
+
id: `p${i}`,
|
|
2978
|
+
status: "pending",
|
|
2979
|
+
createdAt: created2(i)
|
|
2980
|
+
}));
|
|
2981
|
+
}
|
|
2982
|
+
await store.createRun(run({
|
|
2983
|
+
id: "busy",
|
|
2984
|
+
status: "pending",
|
|
2985
|
+
createdAt: created2(-2)
|
|
2986
|
+
}));
|
|
2987
|
+
await store.updateRun("busy", {
|
|
2988
|
+
lockedBy: "other",
|
|
2989
|
+
lockedUntil: 5e3
|
|
2990
|
+
});
|
|
2991
|
+
await store.createRun(run({
|
|
2992
|
+
id: "stale",
|
|
2993
|
+
status: "pending",
|
|
2994
|
+
createdAt: created2(-1)
|
|
2995
|
+
}));
|
|
2996
|
+
await store.updateRun("stale", {
|
|
2997
|
+
lockedBy: "gone",
|
|
2998
|
+
lockedUntil: 500
|
|
2999
|
+
});
|
|
3000
|
+
await store.createRun(run({
|
|
3001
|
+
id: "done",
|
|
3002
|
+
status: "completed",
|
|
3003
|
+
createdAt: created2(-3)
|
|
3004
|
+
}));
|
|
3005
|
+
const first = await store.claimPendingRuns(3, "a", 9e3, 1e3);
|
|
3006
|
+
expect3(first.map((r) => r.id)).toEqual([
|
|
3007
|
+
"stale",
|
|
3008
|
+
"p0",
|
|
3009
|
+
"p1"
|
|
3010
|
+
]);
|
|
3011
|
+
for (const r of first) {
|
|
3012
|
+
expect3(r.status).toBe("pending");
|
|
3013
|
+
expect3(r.lockedBy).toBe("a");
|
|
3014
|
+
expect3(r.lockedUntil).toBe(9e3);
|
|
3015
|
+
}
|
|
3016
|
+
expect3((await store.getRun("p0"))?.lockedBy).toBe("a");
|
|
3017
|
+
expect3((await store.getRun("busy"))?.lockedBy).toBe("other");
|
|
3018
|
+
expect3(await store.claimPendingRuns(0, "a", 9e3, 1e3)).toEqual([]);
|
|
3019
|
+
const [x, y] = await Promise.all([
|
|
3020
|
+
store.claimPendingRuns(3, "x", 9e3, 1e3),
|
|
3021
|
+
peerStore.claimPendingRuns?.(3, "y", 9e3, 1e3) ?? Promise.resolve([])
|
|
3022
|
+
]);
|
|
3023
|
+
const ids2 = [
|
|
3024
|
+
...x,
|
|
3025
|
+
...y
|
|
3026
|
+
].map((r) => r.id);
|
|
3027
|
+
expect3(new Set(ids2).size).toBe(ids2.length);
|
|
3028
|
+
expect3(ids2.length).toBeGreaterThan(0);
|
|
3029
|
+
const rest = await store.claimPendingRuns(10, "z", 9e3, 1e3);
|
|
3030
|
+
expect3([
|
|
3031
|
+
...ids2,
|
|
3032
|
+
...rest.map((r) => r.id)
|
|
3033
|
+
].sort()).toEqual([
|
|
3034
|
+
"p2",
|
|
3035
|
+
"p3",
|
|
3036
|
+
"p4",
|
|
3037
|
+
"p5"
|
|
3038
|
+
]);
|
|
3039
|
+
});
|
|
3040
|
+
t("keeps updatedAt to the millisecond, through createRun, updateRun and updateRunIf", async () => {
|
|
3041
|
+
const created2 = new Date(at.getTime() + 123);
|
|
3042
|
+
await store.createRun(run({
|
|
3043
|
+
updatedAt: created2
|
|
3044
|
+
}));
|
|
3045
|
+
expect3((await store.getRun("r1"))?.updatedAt.getTime()).toBe(created2.getTime());
|
|
3046
|
+
const patched = new Date(at.getTime() + 60456);
|
|
3047
|
+
await store.updateRun("r1", {
|
|
3048
|
+
status: "suspended",
|
|
3049
|
+
updatedAt: patched
|
|
3050
|
+
});
|
|
3051
|
+
expect3((await store.getRun("r1"))?.updatedAt.getTime()).toBe(patched.getTime());
|
|
3052
|
+
if (store.updateRunIf) {
|
|
3053
|
+
const stamped = new Date(at.getTime() + 120789);
|
|
3054
|
+
expect3(await store.updateRunIf("r1", [
|
|
3055
|
+
"suspended"
|
|
3056
|
+
], {
|
|
3057
|
+
updatedAt: stamped
|
|
3058
|
+
})).toBe(true);
|
|
3059
|
+
expect3((await store.getRun("r1"))?.updatedAt.getTime()).toBe(stamped.getTime());
|
|
3060
|
+
}
|
|
3061
|
+
});
|
|
3062
|
+
t("tryLockRunAndGet takes a free or expired lease and reads the run", async () => {
|
|
3063
|
+
if (!store.tryLockRunAndGet) return;
|
|
3064
|
+
await store.createRun(run({
|
|
3065
|
+
status: "suspended"
|
|
3066
|
+
}));
|
|
3067
|
+
const locked = await store.tryLockRunAndGet("r1", "a", 2e3, 1e3);
|
|
3068
|
+
expect3(locked?.id).toBe("r1");
|
|
3069
|
+
expect3(locked?.status).toBe("suspended");
|
|
3070
|
+
expect3(locked?.lockedBy).toBe("a");
|
|
3071
|
+
expect3(locked?.lockedUntil).toBe(2e3);
|
|
3072
|
+
expect3(await store.tryLockRunAndGet("r1", "b", 3e3, 1500)).toBeNull();
|
|
3073
|
+
expect3((await store.getRun("r1"))?.lockedBy).toBe("a");
|
|
3074
|
+
expect3((await store.tryLockRunAndGet("r1", "b", 4e3, 2e3))?.lockedBy).toBe("b");
|
|
3075
|
+
expect3(await store.tryLockRunAndGet("missing", "a", 2e3, 1e3)).toBeNull();
|
|
3076
|
+
});
|
|
2750
3077
|
t("settleCheckpointsIfPending sends none of the input of the steps it settles", async () => {
|
|
2751
3078
|
if (!statementLog || !store.settleCheckpointsIfPending) return;
|
|
2752
3079
|
await store.createRun(run());
|
|
@@ -5328,6 +5655,137 @@ function runStateStoreContract(name, makeStore) {
|
|
|
5328
5655
|
}
|
|
5329
5656
|
});
|
|
5330
5657
|
});
|
|
5658
|
+
t("takes the oldest buffered signal among several tokens (waitForAny)", async () => {
|
|
5659
|
+
const take = store.takeOldestBufferedSignal?.bind(store);
|
|
5660
|
+
expect3(take).toBeTypeOf("function");
|
|
5661
|
+
if (!take) return;
|
|
5662
|
+
expect3(await take([])).toBeNull();
|
|
5663
|
+
expect3(await take([
|
|
5664
|
+
"a",
|
|
5665
|
+
"b"
|
|
5666
|
+
])).toBeNull();
|
|
5667
|
+
await store.bufferSignal("b", {
|
|
5668
|
+
n: 1
|
|
5669
|
+
});
|
|
5670
|
+
await store.bufferSignal("a", {
|
|
5671
|
+
n: 2
|
|
5672
|
+
});
|
|
5673
|
+
await store.bufferSignal("c", {
|
|
5674
|
+
n: 3
|
|
5675
|
+
});
|
|
5676
|
+
await store.bufferSignal("b", {
|
|
5677
|
+
n: 4
|
|
5678
|
+
});
|
|
5679
|
+
await store.bufferSignal("a_", {
|
|
5680
|
+
n: 5
|
|
5681
|
+
});
|
|
5682
|
+
expect3(await take([
|
|
5683
|
+
"a",
|
|
5684
|
+
"b"
|
|
5685
|
+
])).toEqual({
|
|
5686
|
+
token: "b",
|
|
5687
|
+
payload: {
|
|
5688
|
+
n: 1
|
|
5689
|
+
}
|
|
5690
|
+
});
|
|
5691
|
+
expect3(await take([
|
|
5692
|
+
"a",
|
|
5693
|
+
"b",
|
|
5694
|
+
"a"
|
|
5695
|
+
])).toEqual({
|
|
5696
|
+
token: "a",
|
|
5697
|
+
payload: {
|
|
5698
|
+
n: 2
|
|
5699
|
+
}
|
|
5700
|
+
});
|
|
5701
|
+
expect3(await take([
|
|
5702
|
+
"a",
|
|
5703
|
+
"b"
|
|
5704
|
+
])).toEqual({
|
|
5705
|
+
token: "b",
|
|
5706
|
+
payload: {
|
|
5707
|
+
n: 4
|
|
5708
|
+
}
|
|
5709
|
+
});
|
|
5710
|
+
expect3(await take([
|
|
5711
|
+
"a",
|
|
5712
|
+
"b"
|
|
5713
|
+
])).toBeNull();
|
|
5714
|
+
expect3(await store.takeBufferedSignal("c")).toEqual({
|
|
5715
|
+
payload: {
|
|
5716
|
+
n: 3
|
|
5717
|
+
}
|
|
5718
|
+
});
|
|
5719
|
+
expect3(await store.takeBufferedSignal("a_")).toEqual({
|
|
5720
|
+
payload: {
|
|
5721
|
+
n: 5
|
|
5722
|
+
}
|
|
5723
|
+
});
|
|
5724
|
+
});
|
|
5725
|
+
t("never hands one buffered signal to two concurrent takers among tokens", async () => {
|
|
5726
|
+
const take = store.takeOldestBufferedSignal?.bind(store);
|
|
5727
|
+
const peerTake = peerStore.takeOldestBufferedSignal?.bind(peerStore);
|
|
5728
|
+
if (!take || !peerTake) return;
|
|
5729
|
+
for (let i = 0; i < 6; i += 1) await store.bufferSignal(i % 2 ? "x" : "y", i);
|
|
5730
|
+
const taken = await Promise.all(Array.from({
|
|
5731
|
+
length: 10
|
|
5732
|
+
}, (_, i) => (i % 2 ? take : peerTake)([
|
|
5733
|
+
"x",
|
|
5734
|
+
"y"
|
|
5735
|
+
])));
|
|
5736
|
+
const payloads = taken.flatMap((t2) => t2 ? [
|
|
5737
|
+
t2.payload
|
|
5738
|
+
] : []);
|
|
5739
|
+
expect3(payloads.sort()).toEqual([
|
|
5740
|
+
0,
|
|
5741
|
+
1,
|
|
5742
|
+
2,
|
|
5743
|
+
3,
|
|
5744
|
+
4,
|
|
5745
|
+
5
|
|
5746
|
+
]);
|
|
5747
|
+
});
|
|
5748
|
+
t("records a signal id once per token until it expires (signal dedupe)", async () => {
|
|
5749
|
+
const claim = store.claimSignalId?.bind(store);
|
|
5750
|
+
expect3(claim).toBeTypeOf("function");
|
|
5751
|
+
expect3(store.releaseSignalId).toBeTypeOf("function");
|
|
5752
|
+
expect3(store.deleteExpiredSignalIds).toBeTypeOf("function");
|
|
5753
|
+
if (!claim) return;
|
|
5754
|
+
expect3(await claim("paid:o1", "evt-1", NOW + DAY, NOW)).toBe(true);
|
|
5755
|
+
expect3(await claim("paid:o1", "evt-1", NOW + 2 * DAY, NOW + 1e3)).toBe(false);
|
|
5756
|
+
expect3(await claim("paid:o2", "evt-1", NOW + DAY, NOW)).toBe(true);
|
|
5757
|
+
expect3(await claim("paid:o1", "evt-1", NOW + 3 * DAY, NOW + DAY)).toBe(true);
|
|
5758
|
+
expect3(await claim("paid:o1", "evt-1", NOW + 4 * DAY, NOW + 2 * DAY)).toBe(false);
|
|
5759
|
+
await store.releaseSignalId?.("paid:o1", "evt-1");
|
|
5760
|
+
expect3(await claim("paid:o1", "evt-1", NOW + DAY, NOW)).toBe(true);
|
|
5761
|
+
await store.releaseSignalId?.("never", "claimed");
|
|
5762
|
+
});
|
|
5763
|
+
t("records a signal id for exactly one of several concurrent claims", async () => {
|
|
5764
|
+
const claim = store.claimSignalId?.bind(store);
|
|
5765
|
+
const peerClaim = peerStore.claimSignalId?.bind(peerStore);
|
|
5766
|
+
if (!claim || !peerClaim) return;
|
|
5767
|
+
const fresh = await Promise.all(Array.from({
|
|
5768
|
+
length: 8
|
|
5769
|
+
}, (_, i) => (i % 2 ? claim : peerClaim)("t", "race", NOW + DAY, NOW)));
|
|
5770
|
+
expect3(fresh.filter(Boolean)).toHaveLength(1);
|
|
5771
|
+
const later = NOW + 2 * DAY;
|
|
5772
|
+
const again = await Promise.all(Array.from({
|
|
5773
|
+
length: 8
|
|
5774
|
+
}, (_, i) => (i % 2 ? claim : peerClaim)("t", "race", later + DAY, later)));
|
|
5775
|
+
expect3(again.filter(Boolean)).toHaveLength(1);
|
|
5776
|
+
});
|
|
5777
|
+
t("deletes expired signal ids in bounded batches", async () => {
|
|
5778
|
+
const claim = store.claimSignalId?.bind(store);
|
|
5779
|
+
const sweep = store.deleteExpiredSignalIds?.bind(store);
|
|
5780
|
+
if (!claim || !sweep) return;
|
|
5781
|
+
for (let i = 0; i < 5; i += 1) await claim("t", `old-${i}`, NOW + i, NOW - DAY);
|
|
5782
|
+
await claim("t", "fresh", NOW + DAY, NOW);
|
|
5783
|
+
expect3(await sweep(NOW + 4, 3)).toBe(3);
|
|
5784
|
+
expect3(await sweep(NOW + 4, 3)).toBe(2);
|
|
5785
|
+
expect3(await sweep(NOW + 4, 3)).toBe(0);
|
|
5786
|
+
expect3(await claim("t", "fresh", NOW + DAY, NOW)).toBe(false);
|
|
5787
|
+
expect3(await claim("t", "old-0", NOW + DAY, NOW + 5)).toBe(true);
|
|
5788
|
+
});
|
|
5331
5789
|
t("buffers events per name, oldest-first, and lists them without consuming", async () => {
|
|
5332
5790
|
await store.bufferEvent({
|
|
5333
5791
|
name: "order.paid",
|