Skip to content

Commit ed4836e

Browse files
committed
test(run-engine): pin the single nested write and the resumed QUEUED
The store-routing counter separated nested creates from standalone ones, so it can assert the trigger path makes no standalone snapshot write at all rather than just more writes than before. A new case drives a run through suspend and resume to prove a re-enqueue still writes its own QUEUED snapshot.
1 parent 7e78ed8 commit ed4836e

2 files changed

Lines changed: 125 additions & 6 deletions

File tree

internal-packages/run-engine/src/engine/systems/enqueueSystem.test.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,28 +50,31 @@ function createEngineOptions(redisOptions: any, prisma: any, store?: PostgresRun
5050
* the routing is observed over real containers without ever mocking prisma or the store.
5151
*/
5252
class CountingPostgresRunStore extends PostgresRunStore {
53-
public snapshotCreates = 0;
53+
/** Snapshots written nested in a run create: the trigger path's QUEUED write. */
54+
public nestedSnapshotCreates = 0;
55+
/** Snapshots written standalone through `createExecutionSnapshot`: every re-enqueue. */
56+
public standaloneSnapshotCreates = 0;
5457

5558
override async createExecutionSnapshot(
5659
input: any,
5760
tx?: any
5861
): ReturnType<PostgresRunStore["createExecutionSnapshot"]> {
59-
this.snapshotCreates++;
62+
this.standaloneSnapshotCreates++;
6063
return super.createExecutionSnapshot(input, tx);
6164
}
6265

6366
override async createRun(
6467
params: Parameters<PostgresRunStore["createRun"]>[0],
6568
tx?: any
6669
): ReturnType<PostgresRunStore["createRun"]> {
67-
this.snapshotCreates++;
70+
this.nestedSnapshotCreates++;
6871
return super.createRun(params, tx);
6972
}
7073
}
7174

7275
describe("RunEngine enqueueRun store routing", () => {
7376
containerTest(
74-
"the QUEUED snapshot routes through the store",
77+
"the QUEUED snapshot routes through the store as a single nested write",
7578
async ({ prisma, redisOptions }) => {
7679
const countingStore = new CountingPostgresRunStore({ prisma, readOnlyPrisma: prisma });
7780
const engine = new RunEngine(createEngineOptions(redisOptions, prisma, countingStore));
@@ -81,7 +84,8 @@ describe("RunEngine enqueueRun store routing", () => {
8184
const taskIdentifier = "test-task";
8285
await setupBackgroundWorker(engine, environment, taskIdentifier);
8386

84-
const before = countingStore.snapshotCreates;
87+
const nestedBefore = countingStore.nestedSnapshotCreates;
88+
const standaloneBefore = countingStore.standaloneSnapshotCreates;
8589

8690
const run = await engine.trigger(
8791
{
@@ -103,7 +107,8 @@ describe("RunEngine enqueueRun store routing", () => {
103107
prisma
104108
);
105109

106-
expect(countingStore.snapshotCreates).toBeGreaterThan(before);
110+
expect(countingStore.nestedSnapshotCreates).toBe(nestedBefore + 1);
111+
expect(countingStore.standaloneSnapshotCreates).toBe(standaloneBefore);
107112

108113
const latest = await getLatestExecutionSnapshot(prisma, run.id);
109114
assertNonNullable(latest);

internal-packages/run-engine/src/engine/tests/triggerSnapshotCollapse.test.ts

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,4 +253,118 @@ describe("RunEngine trigger() execution snapshots", () => {
253253
}
254254
}
255255
);
256+
257+
containerTest(
258+
"a resumed run still writes its own QUEUED snapshot",
259+
async ({ prisma, redisOptions }) => {
260+
const authenticatedEnvironment = await setupAuthenticatedEnvironment(prisma, "PRODUCTION");
261+
262+
const engine = new RunEngine({
263+
prisma,
264+
worker: {
265+
redis: redisOptions,
266+
workers: 1,
267+
tasksPerWorker: 10,
268+
pollIntervalMs: 100,
269+
},
270+
queue: {
271+
redis: redisOptions,
272+
masterQueueConsumersDisabled: true,
273+
processWorkerQueueDebounceMs: 50,
274+
},
275+
runLock: {
276+
redis: redisOptions,
277+
},
278+
machines: {
279+
defaultMachine: "small-1x",
280+
machines: {
281+
"small-1x": {
282+
name: "small-1x" as const,
283+
cpu: 0.5,
284+
memory: 0.5,
285+
centsPerMs: 0.0001,
286+
},
287+
},
288+
baseCostInCents: 0.0001,
289+
},
290+
tracer: trace.getTracer("test", "0.0.0"),
291+
});
292+
293+
try {
294+
const taskIdentifier = "test-task";
295+
296+
await setupBackgroundWorker(engine, authenticatedEnvironment, taskIdentifier);
297+
298+
const run = await engine.trigger(
299+
{
300+
number: 1,
301+
friendlyId: "run_1237",
302+
environment: authenticatedEnvironment,
303+
taskIdentifier,
304+
payload: "{}",
305+
payloadType: "application/json",
306+
context: {},
307+
traceContext: {},
308+
traceId: "t_collapse_4",
309+
spanId: "s_collapse_4",
310+
workerQueue: "main",
311+
queue: "task/test-task",
312+
isTest: false,
313+
tags: [],
314+
},
315+
prisma
316+
);
317+
318+
await setTimeout(500);
319+
const dequeued = await engine.dequeueFromWorkerQueue({
320+
consumerId: "test_collapse_4",
321+
workerQueue: "main",
322+
});
323+
assertNonNullable(dequeued[0]);
324+
325+
await engine.startRunAttempt({
326+
runId: dequeued[0].run.id,
327+
snapshotId: dequeued[0].snapshot.id,
328+
});
329+
330+
const waitpointResult = await engine.createManualWaitpoint({
331+
environmentId: authenticatedEnvironment.id,
332+
projectId: authenticatedEnvironment.projectId,
333+
});
334+
335+
const blockedResult = await engine.blockRunWithWaitpoint({
336+
runId: run.id,
337+
waitpoints: waitpointResult.waitpoint.id,
338+
projectId: authenticatedEnvironment.projectId,
339+
organizationId: authenticatedEnvironment.organizationId,
340+
});
341+
342+
const checkpointResult = await engine.createCheckpoint({
343+
runId: run.id,
344+
snapshotId: blockedResult.id,
345+
checkpoint: {
346+
type: "DOCKER",
347+
reason: "TEST_CHECKPOINT",
348+
location: "test-location",
349+
imageRef: "test-image-ref",
350+
},
351+
});
352+
expect(checkpointResult.ok).toBe(true);
353+
354+
await engine.completeWaitpoint({ id: waitpointResult.waitpoint.id });
355+
await setTimeout(500);
356+
357+
expect(await snapshotStatuses(prisma, run.id)).toEqual([
358+
"QUEUED",
359+
"PENDING_EXECUTING",
360+
"EXECUTING",
361+
"EXECUTING_WITH_WAITPOINTS",
362+
"SUSPENDED",
363+
"QUEUED",
364+
]);
365+
} finally {
366+
await engine.quit();
367+
}
368+
}
369+
);
256370
});

0 commit comments

Comments
 (0)