@dudousxd/nestjs-durable-testing 0.24.0 → 0.25.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.d.ts +5 -0
- package/dist/index.js +499 -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,367 @@ 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
|
+
await store.saveCheckpoint(pending("r1", 4));
|
|
2933
|
+
const events = [
|
|
2934
|
+
{
|
|
2935
|
+
at: 2,
|
|
2936
|
+
level: "info",
|
|
2937
|
+
message: "started"
|
|
2938
|
+
},
|
|
2939
|
+
{
|
|
2940
|
+
at: 3,
|
|
2941
|
+
level: "info",
|
|
2942
|
+
message: "done"
|
|
2943
|
+
}
|
|
2944
|
+
];
|
|
2945
|
+
const [arrays] = await store.settleResultsIfPending([
|
|
2946
|
+
{
|
|
2947
|
+
runId: "r1",
|
|
2948
|
+
seq: 4,
|
|
2949
|
+
status: "completed",
|
|
2950
|
+
output: [
|
|
2951
|
+
{
|
|
2952
|
+
row: 1
|
|
2953
|
+
},
|
|
2954
|
+
{
|
|
2955
|
+
row: 2
|
|
2956
|
+
}
|
|
2957
|
+
],
|
|
2958
|
+
events,
|
|
2959
|
+
finishedAt: later
|
|
2960
|
+
}
|
|
2961
|
+
]);
|
|
2962
|
+
expect3(arrays?.status).toBe("completed");
|
|
2963
|
+
expect3(arrays?.output).toEqual([
|
|
2964
|
+
{
|
|
2965
|
+
row: 1
|
|
2966
|
+
},
|
|
2967
|
+
{
|
|
2968
|
+
row: 2
|
|
2969
|
+
}
|
|
2970
|
+
]);
|
|
2971
|
+
expect3(arrays?.events).toEqual(events);
|
|
2972
|
+
expect3((await store.getCheckpoint("r1", 4))?.events).toEqual(events);
|
|
2973
|
+
const stored = await store.getCheckpoint("r1", 0);
|
|
2974
|
+
expect3(stored?.output).toEqual({
|
|
2975
|
+
n: 0
|
|
2976
|
+
});
|
|
2977
|
+
expect3(stored?.input).toEqual({
|
|
2978
|
+
prompt: "p0"
|
|
2979
|
+
});
|
|
2980
|
+
expect3((await store.getCheckpoint("r1", 1))?.output).toEqual({
|
|
2981
|
+
first: 1
|
|
2982
|
+
});
|
|
2983
|
+
expect3((await store.getCheckpoint("r1", 3))?.status).toBe("pending");
|
|
2984
|
+
expect3((await store.getCheckpoint("gone", 0))?.status).toBe("pending");
|
|
2985
|
+
expect3(await store.getCheckpoint("r1", 9)).toBeNull();
|
|
2986
|
+
});
|
|
2987
|
+
t("insertCheckpointIfAbsent writes a position only once", async () => {
|
|
2988
|
+
if (!store.insertCheckpointIfAbsent) return;
|
|
2989
|
+
await store.createRun(run());
|
|
2990
|
+
const first = checkpoint({
|
|
2991
|
+
seq: 4,
|
|
2992
|
+
status: "pending",
|
|
2993
|
+
input: {
|
|
2994
|
+
a: 1
|
|
2995
|
+
},
|
|
2996
|
+
output: void 0
|
|
2997
|
+
});
|
|
2998
|
+
expect3(await store.insertCheckpointIfAbsent(first)).toBe(true);
|
|
2999
|
+
expect3(await store.insertCheckpointIfAbsent({
|
|
3000
|
+
...first,
|
|
3001
|
+
status: "completed",
|
|
3002
|
+
output: {
|
|
3003
|
+
b: 2
|
|
3004
|
+
}
|
|
3005
|
+
})).toBe(false);
|
|
3006
|
+
const stored = await store.getCheckpoint("r1", 4);
|
|
3007
|
+
expect3(stored?.status).toBe("pending");
|
|
3008
|
+
expect3(stored?.input).toEqual({
|
|
3009
|
+
a: 1
|
|
3010
|
+
});
|
|
3011
|
+
expect3(stored?.output).toBeUndefined();
|
|
3012
|
+
});
|
|
3013
|
+
t("claimPendingRuns leases the oldest free pending runs, and concurrent claims never overlap", async () => {
|
|
3014
|
+
if (!store.claimPendingRuns) return;
|
|
3015
|
+
const created2 = /* @__PURE__ */ __name((i) => new Date(at.getTime() + i * 1e3), "created");
|
|
3016
|
+
for (let i = 0; i < 6; i += 1) {
|
|
3017
|
+
await store.createRun(run({
|
|
3018
|
+
id: `p${i}`,
|
|
3019
|
+
status: "pending",
|
|
3020
|
+
createdAt: created2(i)
|
|
3021
|
+
}));
|
|
3022
|
+
}
|
|
3023
|
+
await store.createRun(run({
|
|
3024
|
+
id: "busy",
|
|
3025
|
+
status: "pending",
|
|
3026
|
+
createdAt: created2(-2)
|
|
3027
|
+
}));
|
|
3028
|
+
await store.updateRun("busy", {
|
|
3029
|
+
lockedBy: "other",
|
|
3030
|
+
lockedUntil: 5e3
|
|
3031
|
+
});
|
|
3032
|
+
await store.createRun(run({
|
|
3033
|
+
id: "stale",
|
|
3034
|
+
status: "pending",
|
|
3035
|
+
createdAt: created2(-1)
|
|
3036
|
+
}));
|
|
3037
|
+
await store.updateRun("stale", {
|
|
3038
|
+
lockedBy: "gone",
|
|
3039
|
+
lockedUntil: 500
|
|
3040
|
+
});
|
|
3041
|
+
await store.createRun(run({
|
|
3042
|
+
id: "done",
|
|
3043
|
+
status: "completed",
|
|
3044
|
+
createdAt: created2(-3)
|
|
3045
|
+
}));
|
|
3046
|
+
const first = await store.claimPendingRuns(3, "a", 9e3, 1e3);
|
|
3047
|
+
expect3(first.map((r) => r.id)).toEqual([
|
|
3048
|
+
"stale",
|
|
3049
|
+
"p0",
|
|
3050
|
+
"p1"
|
|
3051
|
+
]);
|
|
3052
|
+
for (const r of first) {
|
|
3053
|
+
expect3(r.status).toBe("pending");
|
|
3054
|
+
expect3(r.lockedBy).toBe("a");
|
|
3055
|
+
expect3(r.lockedUntil).toBe(9e3);
|
|
3056
|
+
}
|
|
3057
|
+
expect3((await store.getRun("p0"))?.lockedBy).toBe("a");
|
|
3058
|
+
expect3((await store.getRun("busy"))?.lockedBy).toBe("other");
|
|
3059
|
+
expect3(await store.claimPendingRuns(0, "a", 9e3, 1e3)).toEqual([]);
|
|
3060
|
+
const [x, y] = await Promise.all([
|
|
3061
|
+
store.claimPendingRuns(3, "x", 9e3, 1e3),
|
|
3062
|
+
peerStore.claimPendingRuns?.(3, "y", 9e3, 1e3) ?? Promise.resolve([])
|
|
3063
|
+
]);
|
|
3064
|
+
const ids2 = [
|
|
3065
|
+
...x,
|
|
3066
|
+
...y
|
|
3067
|
+
].map((r) => r.id);
|
|
3068
|
+
expect3(new Set(ids2).size).toBe(ids2.length);
|
|
3069
|
+
expect3(ids2.length).toBeGreaterThan(0);
|
|
3070
|
+
const rest = await store.claimPendingRuns(10, "z", 9e3, 1e3);
|
|
3071
|
+
expect3([
|
|
3072
|
+
...ids2,
|
|
3073
|
+
...rest.map((r) => r.id)
|
|
3074
|
+
].sort()).toEqual([
|
|
3075
|
+
"p2",
|
|
3076
|
+
"p3",
|
|
3077
|
+
"p4",
|
|
3078
|
+
"p5"
|
|
3079
|
+
]);
|
|
3080
|
+
});
|
|
3081
|
+
t("keeps updatedAt to the millisecond, through createRun, updateRun and updateRunIf", async () => {
|
|
3082
|
+
const created2 = new Date(at.getTime() + 123);
|
|
3083
|
+
await store.createRun(run({
|
|
3084
|
+
updatedAt: created2
|
|
3085
|
+
}));
|
|
3086
|
+
expect3((await store.getRun("r1"))?.updatedAt.getTime()).toBe(created2.getTime());
|
|
3087
|
+
const patched = new Date(at.getTime() + 60456);
|
|
3088
|
+
await store.updateRun("r1", {
|
|
3089
|
+
status: "suspended",
|
|
3090
|
+
updatedAt: patched
|
|
3091
|
+
});
|
|
3092
|
+
expect3((await store.getRun("r1"))?.updatedAt.getTime()).toBe(patched.getTime());
|
|
3093
|
+
if (store.updateRunIf) {
|
|
3094
|
+
const stamped = new Date(at.getTime() + 120789);
|
|
3095
|
+
expect3(await store.updateRunIf("r1", [
|
|
3096
|
+
"suspended"
|
|
3097
|
+
], {
|
|
3098
|
+
updatedAt: stamped
|
|
3099
|
+
})).toBe(true);
|
|
3100
|
+
expect3((await store.getRun("r1"))?.updatedAt.getTime()).toBe(stamped.getTime());
|
|
3101
|
+
}
|
|
3102
|
+
});
|
|
3103
|
+
t("tryLockRunAndGet takes a free or expired lease and reads the run", async () => {
|
|
3104
|
+
if (!store.tryLockRunAndGet) return;
|
|
3105
|
+
await store.createRun(run({
|
|
3106
|
+
status: "suspended"
|
|
3107
|
+
}));
|
|
3108
|
+
const locked = await store.tryLockRunAndGet("r1", "a", 2e3, 1e3);
|
|
3109
|
+
expect3(locked?.id).toBe("r1");
|
|
3110
|
+
expect3(locked?.status).toBe("suspended");
|
|
3111
|
+
expect3(locked?.lockedBy).toBe("a");
|
|
3112
|
+
expect3(locked?.lockedUntil).toBe(2e3);
|
|
3113
|
+
expect3(await store.tryLockRunAndGet("r1", "b", 3e3, 1500)).toBeNull();
|
|
3114
|
+
expect3((await store.getRun("r1"))?.lockedBy).toBe("a");
|
|
3115
|
+
expect3((await store.tryLockRunAndGet("r1", "b", 4e3, 2e3))?.lockedBy).toBe("b");
|
|
3116
|
+
expect3(await store.tryLockRunAndGet("missing", "a", 2e3, 1e3)).toBeNull();
|
|
3117
|
+
});
|
|
2750
3118
|
t("settleCheckpointsIfPending sends none of the input of the steps it settles", async () => {
|
|
2751
3119
|
if (!statementLog || !store.settleCheckpointsIfPending) return;
|
|
2752
3120
|
await store.createRun(run());
|
|
@@ -5328,6 +5696,137 @@ function runStateStoreContract(name, makeStore) {
|
|
|
5328
5696
|
}
|
|
5329
5697
|
});
|
|
5330
5698
|
});
|
|
5699
|
+
t("takes the oldest buffered signal among several tokens (waitForAny)", async () => {
|
|
5700
|
+
const take = store.takeOldestBufferedSignal?.bind(store);
|
|
5701
|
+
expect3(take).toBeTypeOf("function");
|
|
5702
|
+
if (!take) return;
|
|
5703
|
+
expect3(await take([])).toBeNull();
|
|
5704
|
+
expect3(await take([
|
|
5705
|
+
"a",
|
|
5706
|
+
"b"
|
|
5707
|
+
])).toBeNull();
|
|
5708
|
+
await store.bufferSignal("b", {
|
|
5709
|
+
n: 1
|
|
5710
|
+
});
|
|
5711
|
+
await store.bufferSignal("a", {
|
|
5712
|
+
n: 2
|
|
5713
|
+
});
|
|
5714
|
+
await store.bufferSignal("c", {
|
|
5715
|
+
n: 3
|
|
5716
|
+
});
|
|
5717
|
+
await store.bufferSignal("b", {
|
|
5718
|
+
n: 4
|
|
5719
|
+
});
|
|
5720
|
+
await store.bufferSignal("a_", {
|
|
5721
|
+
n: 5
|
|
5722
|
+
});
|
|
5723
|
+
expect3(await take([
|
|
5724
|
+
"a",
|
|
5725
|
+
"b"
|
|
5726
|
+
])).toEqual({
|
|
5727
|
+
token: "b",
|
|
5728
|
+
payload: {
|
|
5729
|
+
n: 1
|
|
5730
|
+
}
|
|
5731
|
+
});
|
|
5732
|
+
expect3(await take([
|
|
5733
|
+
"a",
|
|
5734
|
+
"b",
|
|
5735
|
+
"a"
|
|
5736
|
+
])).toEqual({
|
|
5737
|
+
token: "a",
|
|
5738
|
+
payload: {
|
|
5739
|
+
n: 2
|
|
5740
|
+
}
|
|
5741
|
+
});
|
|
5742
|
+
expect3(await take([
|
|
5743
|
+
"a",
|
|
5744
|
+
"b"
|
|
5745
|
+
])).toEqual({
|
|
5746
|
+
token: "b",
|
|
5747
|
+
payload: {
|
|
5748
|
+
n: 4
|
|
5749
|
+
}
|
|
5750
|
+
});
|
|
5751
|
+
expect3(await take([
|
|
5752
|
+
"a",
|
|
5753
|
+
"b"
|
|
5754
|
+
])).toBeNull();
|
|
5755
|
+
expect3(await store.takeBufferedSignal("c")).toEqual({
|
|
5756
|
+
payload: {
|
|
5757
|
+
n: 3
|
|
5758
|
+
}
|
|
5759
|
+
});
|
|
5760
|
+
expect3(await store.takeBufferedSignal("a_")).toEqual({
|
|
5761
|
+
payload: {
|
|
5762
|
+
n: 5
|
|
5763
|
+
}
|
|
5764
|
+
});
|
|
5765
|
+
});
|
|
5766
|
+
t("never hands one buffered signal to two concurrent takers among tokens", async () => {
|
|
5767
|
+
const take = store.takeOldestBufferedSignal?.bind(store);
|
|
5768
|
+
const peerTake = peerStore.takeOldestBufferedSignal?.bind(peerStore);
|
|
5769
|
+
if (!take || !peerTake) return;
|
|
5770
|
+
for (let i = 0; i < 6; i += 1) await store.bufferSignal(i % 2 ? "x" : "y", i);
|
|
5771
|
+
const taken = await Promise.all(Array.from({
|
|
5772
|
+
length: 10
|
|
5773
|
+
}, (_, i) => (i % 2 ? take : peerTake)([
|
|
5774
|
+
"x",
|
|
5775
|
+
"y"
|
|
5776
|
+
])));
|
|
5777
|
+
const payloads = taken.flatMap((t2) => t2 ? [
|
|
5778
|
+
t2.payload
|
|
5779
|
+
] : []);
|
|
5780
|
+
expect3(payloads.sort()).toEqual([
|
|
5781
|
+
0,
|
|
5782
|
+
1,
|
|
5783
|
+
2,
|
|
5784
|
+
3,
|
|
5785
|
+
4,
|
|
5786
|
+
5
|
|
5787
|
+
]);
|
|
5788
|
+
});
|
|
5789
|
+
t("records a signal id once per token until it expires (signal dedupe)", async () => {
|
|
5790
|
+
const claim = store.claimSignalId?.bind(store);
|
|
5791
|
+
expect3(claim).toBeTypeOf("function");
|
|
5792
|
+
expect3(store.releaseSignalId).toBeTypeOf("function");
|
|
5793
|
+
expect3(store.deleteExpiredSignalIds).toBeTypeOf("function");
|
|
5794
|
+
if (!claim) return;
|
|
5795
|
+
expect3(await claim("paid:o1", "evt-1", NOW + DAY, NOW)).toBe(true);
|
|
5796
|
+
expect3(await claim("paid:o1", "evt-1", NOW + 2 * DAY, NOW + 1e3)).toBe(false);
|
|
5797
|
+
expect3(await claim("paid:o2", "evt-1", NOW + DAY, NOW)).toBe(true);
|
|
5798
|
+
expect3(await claim("paid:o1", "evt-1", NOW + 3 * DAY, NOW + DAY)).toBe(true);
|
|
5799
|
+
expect3(await claim("paid:o1", "evt-1", NOW + 4 * DAY, NOW + 2 * DAY)).toBe(false);
|
|
5800
|
+
await store.releaseSignalId?.("paid:o1", "evt-1");
|
|
5801
|
+
expect3(await claim("paid:o1", "evt-1", NOW + DAY, NOW)).toBe(true);
|
|
5802
|
+
await store.releaseSignalId?.("never", "claimed");
|
|
5803
|
+
});
|
|
5804
|
+
t("records a signal id for exactly one of several concurrent claims", async () => {
|
|
5805
|
+
const claim = store.claimSignalId?.bind(store);
|
|
5806
|
+
const peerClaim = peerStore.claimSignalId?.bind(peerStore);
|
|
5807
|
+
if (!claim || !peerClaim) return;
|
|
5808
|
+
const fresh = await Promise.all(Array.from({
|
|
5809
|
+
length: 8
|
|
5810
|
+
}, (_, i) => (i % 2 ? claim : peerClaim)("t", "race", NOW + DAY, NOW)));
|
|
5811
|
+
expect3(fresh.filter(Boolean)).toHaveLength(1);
|
|
5812
|
+
const later = NOW + 2 * DAY;
|
|
5813
|
+
const again = await Promise.all(Array.from({
|
|
5814
|
+
length: 8
|
|
5815
|
+
}, (_, i) => (i % 2 ? claim : peerClaim)("t", "race", later + DAY, later)));
|
|
5816
|
+
expect3(again.filter(Boolean)).toHaveLength(1);
|
|
5817
|
+
});
|
|
5818
|
+
t("deletes expired signal ids in bounded batches", async () => {
|
|
5819
|
+
const claim = store.claimSignalId?.bind(store);
|
|
5820
|
+
const sweep = store.deleteExpiredSignalIds?.bind(store);
|
|
5821
|
+
if (!claim || !sweep) return;
|
|
5822
|
+
for (let i = 0; i < 5; i += 1) await claim("t", `old-${i}`, NOW + i, NOW - DAY);
|
|
5823
|
+
await claim("t", "fresh", NOW + DAY, NOW);
|
|
5824
|
+
expect3(await sweep(NOW + 4, 3)).toBe(3);
|
|
5825
|
+
expect3(await sweep(NOW + 4, 3)).toBe(2);
|
|
5826
|
+
expect3(await sweep(NOW + 4, 3)).toBe(0);
|
|
5827
|
+
expect3(await claim("t", "fresh", NOW + DAY, NOW)).toBe(false);
|
|
5828
|
+
expect3(await claim("t", "old-0", NOW + DAY, NOW + 5)).toBe(true);
|
|
5829
|
+
});
|
|
5331
5830
|
t("buffers events per name, oldest-first, and lists them without consuming", async () => {
|
|
5332
5831
|
await store.bufferEvent({
|
|
5333
5832
|
name: "order.paid",
|