|
1 | 1 | import * as schema from '@sim/db/schema' |
2 | 2 | import { readTestDatabaseUrl } from '@sim/db/testing/test-infrastructure' |
3 | 3 | import { generateId } from '@sim/utils/id' |
| 4 | +import { eq, sql } from 'drizzle-orm' |
4 | 5 | import { drizzle } from 'drizzle-orm/postgres-js' |
5 | 6 | import postgres from 'postgres' |
6 | 7 | import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest' |
@@ -48,6 +49,7 @@ vi.mock('@/lib/billing/core/usage-segment-cache', () => ({ |
48 | 49 | }, |
49 | 50 | })) |
50 | 51 |
|
| 52 | +import { markFinishedRunDirty } from '@/lib/billing/activity-rollup/marks' |
51 | 53 | import { |
52 | 54 | type ActivityRollupPassOptions, |
53 | 55 | markStaleHours, |
@@ -634,6 +636,29 @@ describe('organization activity rollup', () => { |
634 | 636 | await expectEquivalent() |
635 | 637 | }, 120_000) |
636 | 638 |
|
| 639 | + it('reads a completion committed after the scan passed its stamped end live at once', async () => { |
| 640 | + const run = await oneLog( |
| 641 | + `status = 'running' AND started_at < (now() AT TIME ZONE 'UTC') - interval '3 days'` |
| 642 | + ) |
| 643 | + // The end is stamped when the run finished, so its write lands behind the late-end scan. |
| 644 | + await database.transaction(async (tx) => { |
| 645 | + const [finished] = await tx |
| 646 | + .update(schema.workflowExecutionLogs) |
| 647 | + .set({ |
| 648 | + status: 'completed', |
| 649 | + totalDurationMs: 1800, |
| 650 | + endedAt: sql`${schema.workflowExecutionLogs.startedAt} + interval '30 minutes'`, |
| 651 | + }) |
| 652 | + .where(eq(schema.workflowExecutionLogs.id, run.id)) |
| 653 | + .returning() |
| 654 | + if (!finished) throw new Error('No run to finish') |
| 655 | + await markFinishedRunDirty(tx, finished) |
| 656 | + }) |
| 657 | + await expectEquivalent() |
| 658 | + await runPass() |
| 659 | + await expectEquivalent() |
| 660 | + }, 120_000) |
| 661 | + |
637 | 662 | it('runs one pass at a time and rewrites nothing when run again', async () => { |
638 | 663 | const results = await Promise.all([runPass(), runPass()]) |
639 | 664 | expect(results.map((result) => result.status).sort()).toEqual(['leased', 'ran']) |
@@ -711,13 +736,16 @@ describe('organization activity rollup', () => { |
711 | 736 | FOR EACH ROW WHEN (NEW.bucket_start = '${failing.toISOString().replace('T', ' ').slice(0, 19)}') |
712 | 737 | EXECUTE FUNCTION fail_rollup_write(); |
713 | 738 | `) |
714 | | - await expect(runPass()).rejects.toThrow() |
715 | | - const crashed = await readRollupState() |
716 | | - expect(crashed?.coverageStart?.getTime()).toBeGreaterThan(failing.getTime()) |
717 | | - expect(crashed?.leaseOwner).toBeNull() |
718 | | - await connection.unsafe( |
719 | | - `DROP TRIGGER fail_rollup_write ON workflow_activity_hourly; DROP FUNCTION fail_rollup_write()` |
720 | | - ) |
| 739 | + try { |
| 740 | + await expect(runPass()).rejects.toThrow() |
| 741 | + const crashed = await readRollupState() |
| 742 | + expect(crashed?.coverageStart?.getTime()).toBeGreaterThan(failing.getTime()) |
| 743 | + expect(crashed?.leaseOwner).toBeNull() |
| 744 | + } finally { |
| 745 | + await connection.unsafe( |
| 746 | + `DROP TRIGGER fail_rollup_write ON workflow_activity_hourly; DROP FUNCTION fail_rollup_write()` |
| 747 | + ) |
| 748 | + } |
721 | 749 | expect((await runPass()).backfillComplete).toBe(true) |
722 | 750 | await expectEquivalent() |
723 | 751 | }, 300_000) |
|
0 commit comments