@dudousxd/nestjs-durable-testing 0.22.3 → 0.23.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 +47 -1
- package/dist/index.js +532 -0
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
package/dist/index.d.ts
CHANGED
|
@@ -256,6 +256,12 @@ interface StateStoreContext {
|
|
|
256
256
|
* a real engine) still asserts it, so this doesn't hide drift in the supported configurations.
|
|
257
257
|
*/
|
|
258
258
|
supportsTagFilter?: boolean;
|
|
259
|
+
/**
|
|
260
|
+
* How many statements the store has sent to its database so far (from the driver's query log).
|
|
261
|
+
* When given, the contract checks that the batched checkpoint methods send a fixed number of them
|
|
262
|
+
* for a wide batch, not one per row.
|
|
263
|
+
*/
|
|
264
|
+
statements?: () => number;
|
|
259
265
|
}
|
|
260
266
|
/** Builds a fresh, empty store for one test. Called once per `it` (in `beforeEach`). */
|
|
261
267
|
type StateStoreFactory = () => Promise<StateStoreContext>;
|
|
@@ -350,4 +356,44 @@ interface RunDispatchContractOptions {
|
|
|
350
356
|
*/
|
|
351
357
|
declare function runRunDispatchContract(name: string, makeHarness: RunDispatchHarnessFactory, options?: RunDispatchContractOptions): void;
|
|
352
358
|
|
|
353
|
-
|
|
359
|
+
/** The step every case fans out over. */
|
|
360
|
+
declare const STEP_FAN_STEP = "fan-results-double";
|
|
361
|
+
/** Two engine-side transports on one broker (two instances of a deployment) and the worker side. */
|
|
362
|
+
interface StepFanResultsHarness {
|
|
363
|
+
/**
|
|
364
|
+
* Fresh queues (prefix, tables) for one case: two engine-side transports, and a worker serving
|
|
365
|
+
* {@link STEP_FAN_STEP} with `handler` (registered on whichever transport the backend runs
|
|
366
|
+
* workers on). `close` releases everything the case opened.
|
|
367
|
+
*/
|
|
368
|
+
open(handler: (input: unknown) => Promise<unknown>): Promise<{
|
|
369
|
+
engines: [Transport, Transport];
|
|
370
|
+
close: () => Promise<void>;
|
|
371
|
+
}>;
|
|
372
|
+
}
|
|
373
|
+
/** Builds the harness, or returns why the backend is unavailable (no Docker, say): the case skips. */
|
|
374
|
+
type StepFanResultsHarnessFactory = () => Promise<StepFanResultsHarness | string>;
|
|
375
|
+
interface StepFanResultsContractOptions {
|
|
376
|
+
/** Per-case budget. Default 60s. */
|
|
377
|
+
timeoutMs?: number;
|
|
378
|
+
/** Steps in the fan. Default 2000. */
|
|
379
|
+
fanSize?: number;
|
|
380
|
+
/**
|
|
381
|
+
* The fewest results the largest batched write of a backlog settles. Default 8: a consumer that
|
|
382
|
+
* hands the engine one result at a time settles a fan in one write and one barrier read each.
|
|
383
|
+
*/
|
|
384
|
+
minBatch?: number;
|
|
385
|
+
/**
|
|
386
|
+
* Whether the transport sends many tasks at once (`dispatchMany`). Default true: the contract
|
|
387
|
+
* then checks that a fan's steps are dispatched through it, never one `dispatch` per step.
|
|
388
|
+
*/
|
|
389
|
+
bulkDispatch?: boolean;
|
|
390
|
+
}
|
|
391
|
+
/**
|
|
392
|
+
* A wide step fan (`ctx.all` / `ctx.allSettled` over a `@Step`) whose results come back over a
|
|
393
|
+
* real broker to two engine instances: every step settles once, the parent wakes O(1) times per
|
|
394
|
+
* wave, and the results consumer hands the engine enough results at once that they are written in
|
|
395
|
+
* batches rather than one by one.
|
|
396
|
+
*/
|
|
397
|
+
declare function runStepFanResultsContract(name: string, makeHarness: StepFanResultsHarnessFactory, options?: StepFanResultsContractOptions): void;
|
|
398
|
+
|
|
399
|
+
export { type AdmissionBackendFactory, DURABLE_CANONICAL_COLUMNS, type DurableColumnMap, type DurableColumnMismatch, type HandleableTransport, MutableClock, type RunDispatchContractOptions, type RunDispatchHarness, type RunDispatchHarnessFactory, type RunHistory, STEP_FAN_STEP, type StateStoreContext, type StateStoreFactory, StateStoreUnavailableError, type StepFanResultsContractOptions, type StepFanResultsHarness, type StepFanResultsHarnessFactory, type TestEngine, aggregationFixture, assertDurableColumns, assertOutput, assertReplayable, assertRunStatus, assertStepAttempts, assertStepsRan, assertTransportConformance, captureHistory, createTestEngine, failOnce, failTimes, parseRunHistory, preserveColumnExpectation, recordedSteps, registerCheckpointAggregationCases, runAdmissionBackendContract, runRunDispatchContract, runStateStoreContract, runStepFanResultsContract, serializeRunHistory };
|
package/dist/index.js
CHANGED
|
@@ -841,6 +841,7 @@ function runStateStoreContract(name, makeStore) {
|
|
|
841
841
|
let cleanup;
|
|
842
842
|
let supportsAsyncTransaction = true;
|
|
843
843
|
let supportsTagFilter = true;
|
|
844
|
+
let statements;
|
|
844
845
|
let skipReason;
|
|
845
846
|
let loggedSkip = false;
|
|
846
847
|
beforeEach(async () => {
|
|
@@ -851,6 +852,7 @@ function runStateStoreContract(name, makeStore) {
|
|
|
851
852
|
cleanup = ctx.cleanup;
|
|
852
853
|
supportsAsyncTransaction = ctx.supportsAsyncTransaction ?? true;
|
|
853
854
|
supportsTagFilter = ctx.supportsTagFilter ?? true;
|
|
855
|
+
statements = ctx.statements;
|
|
854
856
|
skipReason = void 0;
|
|
855
857
|
} catch (err) {
|
|
856
858
|
if (isUnavailable(err) && !process.env.DURABLE_TEST_REQUIRE_BACKENDS) {
|
|
@@ -2120,6 +2122,191 @@ function runStateStoreContract(name, makeStore) {
|
|
|
2120
2122
|
n: 3
|
|
2121
2123
|
});
|
|
2122
2124
|
});
|
|
2125
|
+
t("getCheckpoints reads many of a run's checkpoints at once, in seq order", async () => {
|
|
2126
|
+
expect3(store.getCheckpoints).toBeDefined();
|
|
2127
|
+
if (!store.getCheckpoints) return;
|
|
2128
|
+
await store.createRun(run());
|
|
2129
|
+
await store.createRun(run({
|
|
2130
|
+
id: "r2"
|
|
2131
|
+
}));
|
|
2132
|
+
expect3(await store.getCheckpoints("r1", [])).toEqual([]);
|
|
2133
|
+
for (const seq of [
|
|
2134
|
+
0,
|
|
2135
|
+
1,
|
|
2136
|
+
2,
|
|
2137
|
+
5
|
|
2138
|
+
]) {
|
|
2139
|
+
await store.saveCheckpoint(checkpoint({
|
|
2140
|
+
seq,
|
|
2141
|
+
stepId: `r1:${seq}`,
|
|
2142
|
+
output: {
|
|
2143
|
+
seq
|
|
2144
|
+
},
|
|
2145
|
+
wakeAt: seq === 2 ? 7e3 : void 0
|
|
2146
|
+
}));
|
|
2147
|
+
}
|
|
2148
|
+
await store.saveCheckpoint(checkpoint({
|
|
2149
|
+
runId: "r2",
|
|
2150
|
+
seq: 3,
|
|
2151
|
+
stepId: "r2:3"
|
|
2152
|
+
}));
|
|
2153
|
+
const read = await store.getCheckpoints("r1", [
|
|
2154
|
+
5,
|
|
2155
|
+
0,
|
|
2156
|
+
3,
|
|
2157
|
+
2,
|
|
2158
|
+
0
|
|
2159
|
+
]);
|
|
2160
|
+
expect3(read.map((c) => [
|
|
2161
|
+
c.runId,
|
|
2162
|
+
c.seq,
|
|
2163
|
+
c.output
|
|
2164
|
+
])).toEqual([
|
|
2165
|
+
[
|
|
2166
|
+
"r1",
|
|
2167
|
+
0,
|
|
2168
|
+
{
|
|
2169
|
+
seq: 0
|
|
2170
|
+
}
|
|
2171
|
+
],
|
|
2172
|
+
[
|
|
2173
|
+
"r1",
|
|
2174
|
+
2,
|
|
2175
|
+
{
|
|
2176
|
+
seq: 2
|
|
2177
|
+
}
|
|
2178
|
+
],
|
|
2179
|
+
[
|
|
2180
|
+
"r1",
|
|
2181
|
+
5,
|
|
2182
|
+
{
|
|
2183
|
+
seq: 5
|
|
2184
|
+
}
|
|
2185
|
+
]
|
|
2186
|
+
]);
|
|
2187
|
+
expect3(read[1]).toEqual(await store.getCheckpoint("r1", 2));
|
|
2188
|
+
const n = 1200;
|
|
2189
|
+
for (let seq = 10; seq < 10 + n; seq += 1) {
|
|
2190
|
+
await store.saveCheckpoint(checkpoint({
|
|
2191
|
+
seq,
|
|
2192
|
+
stepId: `r1:${seq}`
|
|
2193
|
+
}));
|
|
2194
|
+
}
|
|
2195
|
+
const wide = await store.getCheckpoints("r1", Array.from({
|
|
2196
|
+
length: n
|
|
2197
|
+
}, (_, i) => 10 + i));
|
|
2198
|
+
expect3(wide.map((c) => c.seq)).toEqual(Array.from({
|
|
2199
|
+
length: n
|
|
2200
|
+
}, (_, i) => 10 + i));
|
|
2201
|
+
});
|
|
2202
|
+
t("saveCheckpoints writes many checkpoints at once, inserting or overwriting each", async () => {
|
|
2203
|
+
expect3(store.saveCheckpoints).toBeDefined();
|
|
2204
|
+
if (!store.saveCheckpoints) return;
|
|
2205
|
+
await store.saveCheckpoints([]);
|
|
2206
|
+
await store.createRun(run());
|
|
2207
|
+
await store.saveCheckpoint(checkpoint({
|
|
2208
|
+
seq: 1,
|
|
2209
|
+
stepId: "r1:1",
|
|
2210
|
+
output: {
|
|
2211
|
+
old: true
|
|
2212
|
+
}
|
|
2213
|
+
}));
|
|
2214
|
+
await store.saveCheckpoints([
|
|
2215
|
+
checkpoint({
|
|
2216
|
+
seq: 0,
|
|
2217
|
+
stepId: "r1:0",
|
|
2218
|
+
status: "pending",
|
|
2219
|
+
output: void 0,
|
|
2220
|
+
wakeAt: 4e3
|
|
2221
|
+
}),
|
|
2222
|
+
checkpoint({
|
|
2223
|
+
seq: 1,
|
|
2224
|
+
stepId: "r1:1",
|
|
2225
|
+
output: {
|
|
2226
|
+
new: true
|
|
2227
|
+
},
|
|
2228
|
+
parallelGroup: "g"
|
|
2229
|
+
}),
|
|
2230
|
+
checkpoint({
|
|
2231
|
+
seq: 2,
|
|
2232
|
+
stepId: "r1:2",
|
|
2233
|
+
input: {
|
|
2234
|
+
i: 2
|
|
2235
|
+
},
|
|
2236
|
+
workerGroup: "w"
|
|
2237
|
+
})
|
|
2238
|
+
]);
|
|
2239
|
+
const rows = await store.listCheckpoints("r1");
|
|
2240
|
+
expect3(rows.map((c) => [
|
|
2241
|
+
c.seq,
|
|
2242
|
+
c.status,
|
|
2243
|
+
c.output
|
|
2244
|
+
])).toEqual([
|
|
2245
|
+
[
|
|
2246
|
+
0,
|
|
2247
|
+
"pending",
|
|
2248
|
+
void 0
|
|
2249
|
+
],
|
|
2250
|
+
[
|
|
2251
|
+
1,
|
|
2252
|
+
"completed",
|
|
2253
|
+
{
|
|
2254
|
+
new: true
|
|
2255
|
+
}
|
|
2256
|
+
],
|
|
2257
|
+
[
|
|
2258
|
+
2,
|
|
2259
|
+
"completed",
|
|
2260
|
+
{
|
|
2261
|
+
ok: true
|
|
2262
|
+
}
|
|
2263
|
+
]
|
|
2264
|
+
]);
|
|
2265
|
+
expect3(rows[0]?.wakeAt).toBe(4e3);
|
|
2266
|
+
expect3(rows[1]?.parallelGroup).toBe("g");
|
|
2267
|
+
expect3(rows[2]?.input).toEqual({
|
|
2268
|
+
i: 2
|
|
2269
|
+
});
|
|
2270
|
+
expect3(rows[2]?.workerGroup).toBe("w");
|
|
2271
|
+
});
|
|
2272
|
+
t("the batched checkpoint methods send a fixed number of statements for a wide batch", async () => {
|
|
2273
|
+
if (!statements || !store.saveCheckpoints || !store.saveCheckpointsIfPending) return;
|
|
2274
|
+
if (!store.getCheckpoints) return;
|
|
2275
|
+
await store.createRun(run());
|
|
2276
|
+
const n = 600;
|
|
2277
|
+
const limit = n / 20;
|
|
2278
|
+
const rows = Array.from({
|
|
2279
|
+
length: n
|
|
2280
|
+
}, (_, seq) => checkpoint({
|
|
2281
|
+
seq,
|
|
2282
|
+
stepId: `r1:${seq}`,
|
|
2283
|
+
status: "pending",
|
|
2284
|
+
output: void 0
|
|
2285
|
+
}));
|
|
2286
|
+
const sent = /* @__PURE__ */ __name(async (work) => {
|
|
2287
|
+
const before = statements();
|
|
2288
|
+
await work();
|
|
2289
|
+
return statements() - before;
|
|
2290
|
+
}, "sent");
|
|
2291
|
+
expect3(await sent(() => store.saveCheckpoints?.(rows))).toBeLessThanOrEqual(limit);
|
|
2292
|
+
const seqs = rows.map((r) => r.seq);
|
|
2293
|
+
expect3(await sent(() => store.getCheckpoints?.("r1", seqs))).toBeLessThanOrEqual(limit);
|
|
2294
|
+
const settled = rows.map((row) => ({
|
|
2295
|
+
...row,
|
|
2296
|
+
status: "completed",
|
|
2297
|
+
output: {
|
|
2298
|
+
seq: row.seq
|
|
2299
|
+
}
|
|
2300
|
+
}));
|
|
2301
|
+
let wrote = [];
|
|
2302
|
+
expect3(await sent(async () => {
|
|
2303
|
+
wrote = await store.saveCheckpointsIfPending?.(settled) ?? [];
|
|
2304
|
+
})).toBeLessThanOrEqual(limit);
|
|
2305
|
+
expect3(wrote.every((w) => w)).toBe(true);
|
|
2306
|
+
expect3(await store.countCheckpoints?.("r1", 0, n - 1, [
|
|
2307
|
+
"completed"
|
|
2308
|
+
])).toBe(n);
|
|
2309
|
+
});
|
|
2123
2310
|
t("saveCheckpointsIfPending writes a wide batch", async () => {
|
|
2124
2311
|
if (!store.saveCheckpointsIfPending) return;
|
|
2125
2312
|
await store.createRun(run());
|
|
@@ -4132,6 +4319,56 @@ function runStateStoreContract(name, makeStore) {
|
|
|
4132
4319
|
expect3(await store.takeBufferedSignal("child:a")).toBeNull();
|
|
4133
4320
|
expect3(await store.takeBufferedSignal("child:b")).toBeNull();
|
|
4134
4321
|
});
|
|
4322
|
+
t("a sweep of buffered child completions sends a few statements per page, not one per row", async () => {
|
|
4323
|
+
if (!statements || !store.listBufferedSignals || !store.deleteBufferedSignals) return;
|
|
4324
|
+
if (!store.saveCheckpoints) return;
|
|
4325
|
+
const parent = "wide";
|
|
4326
|
+
await store.createRun(run({
|
|
4327
|
+
id: parent,
|
|
4328
|
+
status: "suspended"
|
|
4329
|
+
}));
|
|
4330
|
+
const fan = 4e3;
|
|
4331
|
+
for (let from = 0; from < fan; from += 500) {
|
|
4332
|
+
await store.saveCheckpoints(Array.from({
|
|
4333
|
+
length: 500
|
|
4334
|
+
}, (_, k) => {
|
|
4335
|
+
const i = from + k;
|
|
4336
|
+
return checkpoint({
|
|
4337
|
+
runId: parent,
|
|
4338
|
+
seq: i,
|
|
4339
|
+
stepId: `${parent}:${i}`,
|
|
4340
|
+
name: `signal:child:${parent}.all.0.${i}`,
|
|
4341
|
+
kind: "signal",
|
|
4342
|
+
output: {
|
|
4343
|
+
ok: true,
|
|
4344
|
+
value: {
|
|
4345
|
+
i
|
|
4346
|
+
}
|
|
4347
|
+
},
|
|
4348
|
+
parallelGroup: `all:0:n${fan}`
|
|
4349
|
+
});
|
|
4350
|
+
}));
|
|
4351
|
+
}
|
|
4352
|
+
const rows = 500;
|
|
4353
|
+
for (let k = 0; k < rows; k++) {
|
|
4354
|
+
const i = 2e3 + k;
|
|
4355
|
+
await store.bufferSignal(`child:${parent}.all.0.${i}`, {
|
|
4356
|
+
ok: true,
|
|
4357
|
+
value: {
|
|
4358
|
+
i
|
|
4359
|
+
}
|
|
4360
|
+
});
|
|
4361
|
+
}
|
|
4362
|
+
const engine = new WorkflowEngine3({
|
|
4363
|
+
store
|
|
4364
|
+
});
|
|
4365
|
+
const before = statements();
|
|
4366
|
+
const deleted = await engine.sweepBufferedSignals();
|
|
4367
|
+
const sent = statements() - before;
|
|
4368
|
+
expect3(deleted).toBe(rows);
|
|
4369
|
+
expect3(await store.listBufferedSignals("child:", void 0, 10)).toEqual([]);
|
|
4370
|
+
expect3(sent).toBeLessThanOrEqual(10);
|
|
4371
|
+
});
|
|
4135
4372
|
t("buffers signals and takes them FIFO per token", async () => {
|
|
4136
4373
|
await store.bufferSignal("sig", {
|
|
4137
4374
|
n: 1
|
|
@@ -4695,6 +4932,26 @@ var MID_TURN = [
|
|
|
4695
4932
|
signal: true
|
|
4696
4933
|
}
|
|
4697
4934
|
];
|
|
4935
|
+
var HOLDER_DIES = [
|
|
4936
|
+
{
|
|
4937
|
+
when: "after deciding to run the turn a kept wake asks for, before running it",
|
|
4938
|
+
// The lease of that turn, or its dispatch on the run queue.
|
|
4939
|
+
storeTrigger: [
|
|
4940
|
+
"tryLockRun"
|
|
4941
|
+
],
|
|
4942
|
+
transportTrigger: [
|
|
4943
|
+
"dispatchRuns"
|
|
4944
|
+
]
|
|
4945
|
+
},
|
|
4946
|
+
{
|
|
4947
|
+
when: "right after writing its suspension, before looking at the kept wake",
|
|
4948
|
+
// Releasing the lease, which comes before the look.
|
|
4949
|
+
storeTrigger: [
|
|
4950
|
+
"releaseRunLock"
|
|
4951
|
+
],
|
|
4952
|
+
transportTrigger: []
|
|
4953
|
+
}
|
|
4954
|
+
];
|
|
4698
4955
|
async function shutdown(all, transports) {
|
|
4699
4956
|
await Promise.allSettled(all.map(({ engine }) => engine.drain(2e3)));
|
|
4700
4957
|
await Promise.allSettled(transports.map((t) => t.close?.()));
|
|
@@ -5420,13 +5677,287 @@ function runRunDispatchContract(name, makeHarness, options = {}) {
|
|
|
5420
5677
|
}
|
|
5421
5678
|
}, timeoutMs + 2e4);
|
|
5422
5679
|
}
|
|
5680
|
+
for (const { when, storeTrigger, transportTrigger } of HOLDER_DIES) {
|
|
5681
|
+
it3(`a wake kept for a turn survives the instance holding the run going down ${when}`, async (ctx) => {
|
|
5682
|
+
const pair = await pairOrSkip(() => ctx.skip());
|
|
5683
|
+
if (!pair) return;
|
|
5684
|
+
const inner = new InMemoryStateStore4();
|
|
5685
|
+
let now = Date.now();
|
|
5686
|
+
const clock = /* @__PURE__ */ __name(() => now, "clock");
|
|
5687
|
+
let armed = false;
|
|
5688
|
+
let dead = false;
|
|
5689
|
+
const hang = /* @__PURE__ */ __name(() => new Promise(() => {
|
|
5690
|
+
}), "hang");
|
|
5691
|
+
const cutOff = /* @__PURE__ */ __name((target, trigger) => new Proxy(target, {
|
|
5692
|
+
get(t, prop, receiver) {
|
|
5693
|
+
const value = Reflect.get(t, prop, receiver);
|
|
5694
|
+
if (typeof value !== "function") return value;
|
|
5695
|
+
return (...args) => {
|
|
5696
|
+
if (armed && trigger.includes(String(prop))) dead = true;
|
|
5697
|
+
return dead ? hang() : value.apply(t, args);
|
|
5698
|
+
};
|
|
5699
|
+
}
|
|
5700
|
+
}), "cutOff");
|
|
5701
|
+
const holderStore = cutOff(inner, storeTrigger);
|
|
5702
|
+
const holderTransport = cutOff(pair[0], transportTrigger);
|
|
5703
|
+
let requesterDead = false;
|
|
5704
|
+
const requesterStore = new Proxy(inner, {
|
|
5705
|
+
get(t, prop, receiver) {
|
|
5706
|
+
const value = Reflect.get(t, prop, receiver);
|
|
5707
|
+
if (typeof value !== "function") return value;
|
|
5708
|
+
return (...args) => requesterDead ? hang() : value.apply(t, args);
|
|
5709
|
+
}
|
|
5710
|
+
});
|
|
5711
|
+
let turns = 0;
|
|
5712
|
+
let openTurn;
|
|
5713
|
+
const held = new Promise((resolve) => {
|
|
5714
|
+
openTurn = resolve;
|
|
5715
|
+
});
|
|
5716
|
+
const make = /* @__PURE__ */ __name((store, transport) => {
|
|
5717
|
+
const engine = new WorkflowEngine5({
|
|
5718
|
+
store,
|
|
5719
|
+
transport,
|
|
5720
|
+
clock,
|
|
5721
|
+
reconcileMs: 3e5
|
|
5722
|
+
});
|
|
5723
|
+
engine.register("kept", "1", async (wctx) => {
|
|
5724
|
+
turns += 1;
|
|
5725
|
+
if (turns === 2) await held;
|
|
5726
|
+
await wctx.waitForSignal("go");
|
|
5727
|
+
});
|
|
5728
|
+
return engine;
|
|
5729
|
+
}, "make");
|
|
5730
|
+
const holder = make(holderStore, holderTransport);
|
|
5731
|
+
const requester = make(requesterStore, pair[1]);
|
|
5732
|
+
let survivor;
|
|
5733
|
+
try {
|
|
5734
|
+
holder.consumeRuns();
|
|
5735
|
+
await holder.start("kept", {}, "p");
|
|
5736
|
+
await until(async () => (await inner.getRun("p"))?.status === "suspended", timeoutMs);
|
|
5737
|
+
void holder.resume("p").catch(() => void 0);
|
|
5738
|
+
await until(() => turns === 2, timeoutMs);
|
|
5739
|
+
await requester.resume("p");
|
|
5740
|
+
await until(async () => {
|
|
5741
|
+
const run2 = await inner.getRun("p");
|
|
5742
|
+
return run2?.wakeAt !== void 0 && run2.wakeAt <= now + 31e3;
|
|
5743
|
+
}, timeoutMs);
|
|
5744
|
+
await pause(100);
|
|
5745
|
+
requesterDead = true;
|
|
5746
|
+
armed = true;
|
|
5747
|
+
openTurn();
|
|
5748
|
+
await until(() => dead, timeoutMs);
|
|
5749
|
+
expect4(turns).toBe(2);
|
|
5750
|
+
await Promise.race([
|
|
5751
|
+
pair[0].close?.(),
|
|
5752
|
+
pause(2e3)
|
|
5753
|
+
]);
|
|
5754
|
+
await pause(1500);
|
|
5755
|
+
survivor = make(inner, pair[1]);
|
|
5756
|
+
survivor.consumeRuns();
|
|
5757
|
+
now += 31e3;
|
|
5758
|
+
await survivor.resumeDueTimers(now);
|
|
5759
|
+
await until(() => turns === 3, timeoutMs);
|
|
5760
|
+
expect4((await inner.getRun("p"))?.status).toBe("suspended");
|
|
5761
|
+
} finally {
|
|
5762
|
+
openTurn();
|
|
5763
|
+
requesterDead = true;
|
|
5764
|
+
await Promise.allSettled([
|
|
5765
|
+
survivor?.drain(2e3)
|
|
5766
|
+
]);
|
|
5767
|
+
await Promise.race([
|
|
5768
|
+
Promise.allSettled(pair.map((t) => t.close?.())),
|
|
5769
|
+
pause(5e3)
|
|
5770
|
+
]);
|
|
5771
|
+
}
|
|
5772
|
+
}, timeoutMs + 2e4);
|
|
5773
|
+
}
|
|
5423
5774
|
});
|
|
5424
5775
|
}
|
|
5425
5776
|
__name(runRunDispatchContract, "runRunDispatchContract");
|
|
5777
|
+
|
|
5778
|
+
// src/step-fan-results-contract.ts
|
|
5779
|
+
import { DURABLE_STEP_NAME, InMemoryStateStore as InMemoryStateStore5, WorkflowEngine as WorkflowEngine6 } from "@dudousxd/nestjs-durable-core";
|
|
5780
|
+
import { describe as describe4, expect as expect5, it as it4 } from "vitest";
|
|
5781
|
+
var STEP_FAN_STEP = "fan-results-double";
|
|
5782
|
+
var doubleLine = /* @__PURE__ */ __name(() => {
|
|
5783
|
+
}, "doubleLine");
|
|
5784
|
+
doubleLine[DURABLE_STEP_NAME] = STEP_FAN_STEP;
|
|
5785
|
+
function counted(inner, counts) {
|
|
5786
|
+
return new Proxy(inner, {
|
|
5787
|
+
get(target, prop, receiver) {
|
|
5788
|
+
const value = Reflect.get(target, prop, receiver);
|
|
5789
|
+
if (typeof value !== "function") return value;
|
|
5790
|
+
return (...args) => {
|
|
5791
|
+
const name = String(prop);
|
|
5792
|
+
counts.calls.set(name, (counts.calls.get(name) ?? 0) + 1);
|
|
5793
|
+
if (name === "saveCheckpointsIfPending") counts.batches.push(args[0].length);
|
|
5794
|
+
if (name === "saveCheckpointIfPending") counts.batches.push(1);
|
|
5795
|
+
return new Promise((resolve) => setImmediate(resolve)).then(() => value.apply(target, args));
|
|
5796
|
+
};
|
|
5797
|
+
}
|
|
5798
|
+
});
|
|
5799
|
+
}
|
|
5800
|
+
__name(counted, "counted");
|
|
5801
|
+
function behind(transport, gate, dispatched) {
|
|
5802
|
+
return new Proxy(transport, {
|
|
5803
|
+
get(target, prop, receiver) {
|
|
5804
|
+
if (prop === "dispatch") {
|
|
5805
|
+
return (...args) => {
|
|
5806
|
+
dispatched.one += 1;
|
|
5807
|
+
return target.dispatch(...args);
|
|
5808
|
+
};
|
|
5809
|
+
}
|
|
5810
|
+
if (prop === "onResult") {
|
|
5811
|
+
return (handler) => target.onResult(async (result) => {
|
|
5812
|
+
await gate;
|
|
5813
|
+
return handler(result);
|
|
5814
|
+
});
|
|
5815
|
+
}
|
|
5816
|
+
const value = Reflect.get(target, prop, receiver);
|
|
5817
|
+
return typeof value === "function" ? value.bind(target) : value;
|
|
5818
|
+
}
|
|
5819
|
+
});
|
|
5820
|
+
}
|
|
5821
|
+
__name(behind, "behind");
|
|
5822
|
+
async function until2(check, timeoutMs) {
|
|
5823
|
+
const deadline = Date.now() + timeoutMs;
|
|
5824
|
+
while (Date.now() < deadline) {
|
|
5825
|
+
if (await check()) return;
|
|
5826
|
+
await new Promise((resolve) => setTimeout(resolve, 20));
|
|
5827
|
+
}
|
|
5828
|
+
throw new Error("condition not met");
|
|
5829
|
+
}
|
|
5830
|
+
__name(until2, "until");
|
|
5831
|
+
var FORMS = [
|
|
5832
|
+
{
|
|
5833
|
+
form: "ctx.all",
|
|
5834
|
+
settle: false,
|
|
5835
|
+
waves: 1
|
|
5836
|
+
},
|
|
5837
|
+
{
|
|
5838
|
+
form: "ctx.allSettled",
|
|
5839
|
+
settle: true,
|
|
5840
|
+
waves: 1
|
|
5841
|
+
},
|
|
5842
|
+
{
|
|
5843
|
+
form: "ctx.all with concurrency",
|
|
5844
|
+
settle: false,
|
|
5845
|
+
waves: 4
|
|
5846
|
+
},
|
|
5847
|
+
{
|
|
5848
|
+
form: "ctx.allSettled with concurrency",
|
|
5849
|
+
settle: true,
|
|
5850
|
+
waves: 4
|
|
5851
|
+
}
|
|
5852
|
+
];
|
|
5853
|
+
function runStepFanResultsContract(name, makeHarness, options = {}) {
|
|
5854
|
+
const timeoutMs = options.timeoutMs ?? 6e4;
|
|
5855
|
+
const fanSize = options.fanSize ?? 2e3;
|
|
5856
|
+
const minBatch = options.minBatch ?? 8;
|
|
5857
|
+
const bulkDispatch = options.bulkDispatch ?? true;
|
|
5858
|
+
describe4(`Step fan results contract: ${name}`, () => {
|
|
5859
|
+
for (const { form, settle: settle2, waves } of FORMS) {
|
|
5860
|
+
const size = waves === 1 ? fanSize : Math.floor(fanSize / 2);
|
|
5861
|
+
const concurrency = waves === 1 ? void 0 : Math.ceil(size / waves);
|
|
5862
|
+
it4(`settles a ${form} fan of ${size} steps over two instances in batches`, async (ctx) => {
|
|
5863
|
+
const harness = await makeHarness();
|
|
5864
|
+
if (typeof harness === "string") {
|
|
5865
|
+
console.warn(`[step-fan-results contract] ${name} skipped: ${harness}`);
|
|
5866
|
+
ctx.skip();
|
|
5867
|
+
return;
|
|
5868
|
+
}
|
|
5869
|
+
const failing = settle2 ? /* @__PURE__ */ new Set([
|
|
5870
|
+
1,
|
|
5871
|
+
size - 2
|
|
5872
|
+
]) : /* @__PURE__ */ new Set();
|
|
5873
|
+
let ran = 0;
|
|
5874
|
+
let openBacklog;
|
|
5875
|
+
const backlog = new Promise((resolve) => {
|
|
5876
|
+
openBacklog = resolve;
|
|
5877
|
+
});
|
|
5878
|
+
const { engines: opened, close } = await harness.open(async (input) => {
|
|
5879
|
+
ran += 1;
|
|
5880
|
+
if (ran >= (concurrency ?? size)) openBacklog();
|
|
5881
|
+
const { i } = input;
|
|
5882
|
+
if (failing.has(i)) throw Object.assign(new Error(`bad ${i}`), {
|
|
5883
|
+
retryable: false
|
|
5884
|
+
});
|
|
5885
|
+
return i * 2;
|
|
5886
|
+
});
|
|
5887
|
+
const dispatched = {
|
|
5888
|
+
one: 0
|
|
5889
|
+
};
|
|
5890
|
+
const transports = opened.map((t) => behind(t, backlog, dispatched));
|
|
5891
|
+
if (bulkDispatch) {
|
|
5892
|
+
for (const t of opened) expect5(t.dispatchMany).toBeTypeOf("function");
|
|
5893
|
+
}
|
|
5894
|
+
const inner = new InMemoryStateStore5();
|
|
5895
|
+
const counts = {
|
|
5896
|
+
calls: /* @__PURE__ */ new Map(),
|
|
5897
|
+
batches: []
|
|
5898
|
+
};
|
|
5899
|
+
let turns = 0;
|
|
5900
|
+
const settled = /* @__PURE__ */ new Map();
|
|
5901
|
+
const engines = transports.map((transport) => {
|
|
5902
|
+
const engine = new WorkflowEngine6({
|
|
5903
|
+
store: counted(inner, counts),
|
|
5904
|
+
transport,
|
|
5905
|
+
reconcileMs: 0
|
|
5906
|
+
});
|
|
5907
|
+
engine.register("fan", "1", async (c) => {
|
|
5908
|
+
turns += 1;
|
|
5909
|
+
const inputs = Array.from({
|
|
5910
|
+
length: size
|
|
5911
|
+
}, (_, i) => ({
|
|
5912
|
+
i
|
|
5913
|
+
}));
|
|
5914
|
+
const opts = concurrency === void 0 ? {} : {
|
|
5915
|
+
concurrency
|
|
5916
|
+
};
|
|
5917
|
+
if (!settle2) return c.all(doubleLine, inputs, opts);
|
|
5918
|
+
const all = await c.allSettled(doubleLine, inputs, opts);
|
|
5919
|
+
return all.map((s) => s.status === "fulfilled" ? s.value : "failed");
|
|
5920
|
+
});
|
|
5921
|
+
engine.subscribe((e) => {
|
|
5922
|
+
if ((e.type === "step.completed" || e.type === "step.failed") && e.seq !== void 0) {
|
|
5923
|
+
settled.set(e.seq, (settled.get(e.seq) ?? 0) + 1);
|
|
5924
|
+
}
|
|
5925
|
+
});
|
|
5926
|
+
return engine;
|
|
5927
|
+
});
|
|
5928
|
+
try {
|
|
5929
|
+
await engines[0].start("fan", {}, "p");
|
|
5930
|
+
await until2(async () => {
|
|
5931
|
+
const status = (await inner.getRun("p"))?.status;
|
|
5932
|
+
return status === "completed" || status === "failed";
|
|
5933
|
+
}, timeoutMs - 5e3);
|
|
5934
|
+
const run2 = await inner.getRun("p");
|
|
5935
|
+
expect5(run2?.status).toBe("completed");
|
|
5936
|
+
expect5(run2?.output).toEqual(Array.from({
|
|
5937
|
+
length: size
|
|
5938
|
+
}, (_, i) => failing.has(i) ? "failed" : i * 2));
|
|
5939
|
+
expect5(settled.size).toBe(size);
|
|
5940
|
+
expect5([
|
|
5941
|
+
...settled.values()
|
|
5942
|
+
].every((n) => n === 1)).toBe(true);
|
|
5943
|
+
const barrierReads = counts.calls.get("countCheckpoints") ?? 0;
|
|
5944
|
+
expect5(Math.max(...counts.batches)).toBeGreaterThanOrEqual(minBatch);
|
|
5945
|
+
if (bulkDispatch) expect5(dispatched.one).toBe(0);
|
|
5946
|
+
if (waves === 1) expect5(barrierReads).toBeLessThanOrEqual(size / minBatch);
|
|
5947
|
+
expect5(turns).toBeLessThanOrEqual(1 + 2 * waves + failing.size);
|
|
5948
|
+
} finally {
|
|
5949
|
+
await close();
|
|
5950
|
+
}
|
|
5951
|
+
}, timeoutMs);
|
|
5952
|
+
}
|
|
5953
|
+
});
|
|
5954
|
+
}
|
|
5955
|
+
__name(runStepFanResultsContract, "runStepFanResultsContract");
|
|
5426
5956
|
export {
|
|
5427
5957
|
DURABLE_CANONICAL_COLUMNS,
|
|
5428
5958
|
MutableClock,
|
|
5429
5959
|
RUN_HISTORY_FIXTURE_VERSION,
|
|
5960
|
+
STEP_FAN_STEP,
|
|
5430
5961
|
StateStoreUnavailableError,
|
|
5431
5962
|
aggregationFixture,
|
|
5432
5963
|
assertDurableColumns,
|
|
@@ -5447,6 +5978,7 @@ export {
|
|
|
5447
5978
|
runAdmissionBackendContract,
|
|
5448
5979
|
runRunDispatchContract,
|
|
5449
5980
|
runStateStoreContract,
|
|
5981
|
+
runStepFanResultsContract,
|
|
5450
5982
|
serializeRunHistory
|
|
5451
5983
|
};
|
|
5452
5984
|
//# sourceMappingURL=index.js.map
|