|
| 1 | +import { containerTest } from "@internal/testcontainers"; |
| 2 | +import { trace } from "@internal/tracing"; |
| 3 | +import { expect } from "vitest"; |
| 4 | +import { RunEngine } from "../index.js"; |
| 5 | +import { setupAuthenticatedEnvironment, setupBackgroundWorker } from "./setup.js"; |
| 6 | +import { setTimeout } from "timers/promises"; |
| 7 | + |
| 8 | +vi.setConfig({ testTimeout: 60_000 }); |
| 9 | + |
| 10 | +describe("RunEngine Waitpoints – race condition", () => { |
| 11 | + containerTest( |
| 12 | + "join-row removed before run continues (failing race)", |
| 13 | + async ({ prisma, redisOptions }) => { |
| 14 | + const env = await setupAuthenticatedEnvironment(prisma, "PRODUCTION"); |
| 15 | + const engine = new RunEngine({ |
| 16 | + prisma, |
| 17 | + worker: { redis: redisOptions, workers: 1, tasksPerWorker: 10, pollIntervalMs: 100 }, |
| 18 | + queue: { redis: redisOptions }, |
| 19 | + runLock: { redis: redisOptions }, |
| 20 | + machines: { |
| 21 | + defaultMachine: "small-1x", |
| 22 | + machines: { |
| 23 | + "small-1x": { name: "small-1x" as const, cpu: 0.5, memory: 0.5, centsPerMs: 0.0001 }, |
| 24 | + }, |
| 25 | + baseCostInCents: 0.0001, |
| 26 | + }, |
| 27 | + tracer: trace.getTracer("test", "0.0.0"), |
| 28 | + }); |
| 29 | + |
| 30 | + try { |
| 31 | + const taskIdentifier = "test-task"; |
| 32 | + await setupBackgroundWorker(engine, env, taskIdentifier); |
| 33 | + |
| 34 | + const run = await engine.trigger( |
| 35 | + { |
| 36 | + number: 1, |
| 37 | + friendlyId: "run_race", |
| 38 | + environment: env, |
| 39 | + taskIdentifier, |
| 40 | + payload: "{}", |
| 41 | + payloadType: "application/json", |
| 42 | + context: {}, |
| 43 | + traceContext: {}, |
| 44 | + traceId: "race-trace", |
| 45 | + spanId: "race-span", |
| 46 | + masterQueue: "main", |
| 47 | + queue: "task/test-task", |
| 48 | + isTest: false, |
| 49 | + tags: [], |
| 50 | + }, |
| 51 | + prisma |
| 52 | + ); |
| 53 | + |
| 54 | + const dequeued = await engine.dequeueFromMasterQueue({ |
| 55 | + consumerId: "test", |
| 56 | + masterQueue: run.masterQueue, |
| 57 | + maxRunCount: 10, |
| 58 | + }); |
| 59 | + await engine.startRunAttempt({ |
| 60 | + runId: dequeued[0].run.id, |
| 61 | + snapshotId: dequeued[0].snapshot.id, |
| 62 | + }); |
| 63 | + |
| 64 | + // create manual waitpoint |
| 65 | + const { waitpoint } = await engine.createManualWaitpoint({ |
| 66 | + environmentId: env.id, |
| 67 | + projectId: env.projectId, |
| 68 | + }); |
| 69 | + |
| 70 | + // block the run |
| 71 | + await engine.blockRunWithWaitpoint({ |
| 72 | + runId: run.id, |
| 73 | + waitpoints: waitpoint.id, |
| 74 | + projectId: env.projectId, |
| 75 | + organizationId: env.organizationId, |
| 76 | + }); |
| 77 | + |
| 78 | + // Now we need to block the run again right after the continueRunIfUnblocked function |
| 79 | + // is called as a result of the above completeWaitpoint call |
| 80 | + const { waitpoint: waitpoint2 } = await engine.createManualWaitpoint({ |
| 81 | + environmentId: env.id, |
| 82 | + projectId: env.projectId, |
| 83 | + }); |
| 84 | + |
| 85 | + engine.registerRacepointForRun({ runId: run.id, waitInterval: 500 }); |
| 86 | + |
| 87 | + // complete the waitpoint (this will schedule a continueRunIfUnblocked job normally) |
| 88 | + await engine.completeWaitpoint({ id: waitpoint.id }); |
| 89 | + |
| 90 | + await engine.waitpointSystem.blockRunWithWaitpoint({ |
| 91 | + runId: run.id, |
| 92 | + waitpoints: waitpoint2.id, |
| 93 | + projectId: env.projectId, |
| 94 | + organizationId: env.organizationId, |
| 95 | + }); |
| 96 | + |
| 97 | + await setTimeout(1000); |
| 98 | + |
| 99 | + // The join row SHOULD still exist until the run progresses naturally. |
| 100 | + const joinRow = await prisma.taskRunWaitpoint.findFirst({ |
| 101 | + where: { taskRunId: run.id, waitpointId: waitpoint2.id }, |
| 102 | + }); |
| 103 | + |
| 104 | + // Intentionally expect it to still be there – current implementation erroneously deletes it so test fails. |
| 105 | + expect(joinRow).not.toBeNull(); |
| 106 | + } finally { |
| 107 | + await engine.quit(); |
| 108 | + } |
| 109 | + } |
| 110 | + ); |
| 111 | +}); |
0 commit comments