|
| 1 | +/** |
| 2 | + * The run dispatcher's row windows against real PostgreSQL: rows that share a `position` (two |
| 3 | + * inserts that assigned it concurrently) are each dispatched exactly once, wherever the window |
| 4 | + * boundary falls. |
| 5 | + */ |
| 6 | +import { db } from '@sim/db' |
| 7 | +import { userTableDefinitions, userTableRows } from '@sim/db/schema' |
| 8 | +import { readTestDatabaseUrl } from '@sim/db/testing/test-infrastructure' |
| 9 | +import { tableEventsMock } from '@sim/testing/mocks/table-events.mock' |
| 10 | +import { |
| 11 | + tableWorkflowColumnsMock, |
| 12 | + tableWorkflowColumnsMockFns, |
| 13 | +} from '@sim/testing/mocks/table-workflow-columns.mock' |
| 14 | +import { generateId } from '@sim/utils/id' |
| 15 | +import postgres from 'postgres' |
| 16 | +import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest' |
| 17 | + |
| 18 | +const { mockBatchEnqueueAndWait } = vi.hoisted(() => ({ mockBatchEnqueueAndWait: vi.fn() })) |
| 19 | + |
| 20 | +vi.mock('@/lib/core/async-jobs/config', () => ({ |
| 21 | + getJobQueue: async () => ({ batchEnqueueAndWait: mockBatchEnqueueAndWait }), |
| 22 | +})) |
| 23 | +vi.mock('@/lib/table/events', () => tableEventsMock) |
| 24 | +vi.mock('@/lib/table/workflow-columns', () => tableWorkflowColumnsMock) |
| 25 | + |
| 26 | +import { insertDispatch, runDispatcherToCompletion } from '@/lib/table/dispatcher' |
| 27 | + |
| 28 | +const url = readTestDatabaseUrl() |
| 29 | +if (process.env.DATABASE_URL !== url) { |
| 30 | + throw new Error('This suite requires only the disposable local test database') |
| 31 | +} |
| 32 | +const control = postgres(url, { max: 2, onnotice: () => {} }) |
| 33 | +const workspaceId = generateId() |
| 34 | +const userId = generateId() |
| 35 | + |
| 36 | +describe('table run dispatcher against real PostgreSQL', () => { |
| 37 | + beforeAll(async () => { |
| 38 | + await control`INSERT INTO "user" (id, name, email, email_verified, created_at, updated_at) |
| 39 | + VALUES (${userId}, 'Dispatcher fixture', ${`${userId}@example.test`}, true, now(), now())` |
| 40 | + await control.begin(async (tx) => { |
| 41 | + await tx`INSERT INTO workspace (id, name, owner_id, billed_account_user_id) |
| 42 | + VALUES (${workspaceId}, 'Dispatcher fixtures', ${userId}, ${userId})` |
| 43 | + await tx`INSERT INTO project (id, name, owner_id) |
| 44 | + VALUES (${workspaceId}, 'Dispatcher fixture project', ${userId})` |
| 45 | + await tx`INSERT INTO project_workspace (project_id, workspace_id) |
| 46 | + VALUES (${workspaceId}, ${workspaceId})` |
| 47 | + }) |
| 48 | + }) |
| 49 | + |
| 50 | + afterAll(async () => { |
| 51 | + await control.begin(async (tx) => { |
| 52 | + await tx`DELETE FROM workspace WHERE id = ${workspaceId}` |
| 53 | + await tx`DELETE FROM project WHERE id = ${workspaceId}` |
| 54 | + await tx`DELETE FROM "user" WHERE id = ${userId}` |
| 55 | + }) |
| 56 | + await control.end() |
| 57 | + }) |
| 58 | + |
| 59 | + it('dispatches every row once when a window boundary splits rows that share a position', async () => { |
| 60 | + const tableId = generateId() |
| 61 | + await db.insert(userTableDefinitions).values({ |
| 62 | + id: tableId, |
| 63 | + workspaceId, |
| 64 | + name: tableId, |
| 65 | + schema: { columns: [], workflowGroups: [{ id: 'group-1' }] }, |
| 66 | + createdBy: userId, |
| 67 | + }) |
| 68 | + const positions = [0, 1, 1, 1, 2, 3, 3, 4] |
| 69 | + await db.insert(userTableRows).values( |
| 70 | + positions.map((position, i) => ({ |
| 71 | + id: `${tableId}-${i}`, |
| 72 | + tableId, |
| 73 | + workspaceId, |
| 74 | + data: {}, |
| 75 | + position, |
| 76 | + orderKey: `a${i}`, |
| 77 | + })) |
| 78 | + ) |
| 79 | + tableWorkflowColumnsMockFns.mockBuildPendingRuns.mockImplementation( |
| 80 | + (_table: unknown, rows: Array<{ id: string }>) => |
| 81 | + rows.map((row) => ({ rowId: row.id, groupId: 'group-1', tableId, workspaceId })) |
| 82 | + ) |
| 83 | + tableWorkflowColumnsMockFns.mockBuildEnqueueItems.mockImplementation(async (runs: unknown[]) => |
| 84 | + runs.map((payload) => ({ payload })) |
| 85 | + ) |
| 86 | + const dispatched: string[] = [] |
| 87 | + mockBatchEnqueueAndWait.mockImplementation( |
| 88 | + async (_kind: string, items: Array<{ payload: { rowId: string } }>) => { |
| 89 | + for (const item of items) dispatched.push(item.payload.rowId) |
| 90 | + } |
| 91 | + ) |
| 92 | + |
| 93 | + const dispatchId = await insertDispatch({ |
| 94 | + tableId, |
| 95 | + workspaceId, |
| 96 | + requestId: 'dispatcher-ties', |
| 97 | + mode: 'all', |
| 98 | + scope: { groupIds: ['group-1'] }, |
| 99 | + isManualRun: true, |
| 100 | + capabilityGovernedUserId: null, |
| 101 | + }) |
| 102 | + await runDispatcherToCompletion(dispatchId, 2) |
| 103 | + |
| 104 | + expect([...dispatched].sort()).toEqual(positions.map((_, i) => `${tableId}-${i}`).sort()) |
| 105 | + }) |
| 106 | +}) |
0 commit comments