From 4b7dd53bf8a4bc84f602544027f8606ad3711986 Mon Sep 17 00:00:00 2001 From: Bilal Godil Date: Fri, 30 Jan 2026 14:22:02 -0800 Subject: [PATCH] attempt test fixes --- .github/workflows/e2e-api-tests.yaml | 1 + .../e2e-custom-base-port-api-tests.yaml | 1 + .../e2e-source-of-truth-api-tests.yaml | 1 + ...rt-dev-and-test-with-custom-base-port.yaml | 1 + .github/workflows/restart-dev-and-test.yaml | 2 ++ .../setup-tests-with-custom-base-port.yaml | 1 + .github/workflows/setup-tests.yaml | 2 ++ apps/e2e/.env.development | 2 ++ .../api/v1/external-db-sync-utils.ts | 32 +++++++++++++++++-- 9 files changed, 41 insertions(+), 2 deletions(-) diff --git a/.github/workflows/e2e-api-tests.yaml b/.github/workflows/e2e-api-tests.yaml index 33e8da6d7..fab629822 100644 --- a/.github/workflows/e2e-api-tests.yaml +++ b/.github/workflows/e2e-api-tests.yaml @@ -19,6 +19,7 @@ jobs: NODE_ENV: test STACK_ENABLE_HARDCODED_PASSKEY_CHALLENGE_FOR_TESTING: yes STACK_DATABASE_CONNECTION_STRING: "postgres://postgres:PASSWORD-PLACEHOLDER--uqfEC1hmmv@localhost:8128/stackframe" + STACK_FORCE_EXTERNAL_DB_SYNC: "true" strategy: matrix: diff --git a/.github/workflows/e2e-custom-base-port-api-tests.yaml b/.github/workflows/e2e-custom-base-port-api-tests.yaml index 38a555907..e214d5907 100644 --- a/.github/workflows/e2e-custom-base-port-api-tests.yaml +++ b/.github/workflows/e2e-custom-base-port-api-tests.yaml @@ -19,6 +19,7 @@ jobs: STACK_ENABLE_HARDCODED_PASSKEY_CHALLENGE_FOR_TESTING: yes STACK_DATABASE_CONNECTION_STRING: "postgres://postgres:PASSWORD-PLACEHOLDER--uqfEC1hmmv@localhost:6728/stackframe" NEXT_PUBLIC_STACK_PORT_PREFIX: "67" + STACK_FORCE_EXTERNAL_DB_SYNC: "true" strategy: matrix: diff --git a/.github/workflows/e2e-source-of-truth-api-tests.yaml b/.github/workflows/e2e-source-of-truth-api-tests.yaml index 9865086fe..4980c543f 100644 --- a/.github/workflows/e2e-source-of-truth-api-tests.yaml +++ b/.github/workflows/e2e-source-of-truth-api-tests.yaml @@ -21,6 +21,7 @@ jobs: STACK_OVERRIDE_SOURCE_OF_TRUTH: '{"type": "postgres", "connectionString": "postgres://postgres:PASSWORD-PLACEHOLDER--uqfEC1hmmv@localhost:8128/source-of-truth-db?schema=sot-schema"}' STACK_TEST_SOURCE_OF_TRUTH: true STACK_DATABASE_CONNECTION_STRING: "postgres://postgres:PASSWORD-PLACEHOLDER--uqfEC1hmmv@localhost:8128/stackframe" + STACK_FORCE_EXTERNAL_DB_SYNC: "true" strategy: matrix: diff --git a/.github/workflows/restart-dev-and-test-with-custom-base-port.yaml b/.github/workflows/restart-dev-and-test-with-custom-base-port.yaml index 6f22cce5f..2e4a64f17 100644 --- a/.github/workflows/restart-dev-and-test-with-custom-base-port.yaml +++ b/.github/workflows/restart-dev-and-test-with-custom-base-port.yaml @@ -19,6 +19,7 @@ jobs: runs-on: ubicloud-standard-16 env: NEXT_PUBLIC_STACK_PORT_PREFIX: "69" + STACK_FORCE_EXTERNAL_DB_SYNC: "true" steps: - uses: actions/checkout@v6 diff --git a/.github/workflows/restart-dev-and-test.yaml b/.github/workflows/restart-dev-and-test.yaml index e300fa8b1..69e1edd97 100644 --- a/.github/workflows/restart-dev-and-test.yaml +++ b/.github/workflows/restart-dev-and-test.yaml @@ -17,6 +17,8 @@ env: jobs: restart-dev-and-test: runs-on: ubicloud-standard-16 + env: + STACK_FORCE_EXTERNAL_DB_SYNC: "true" steps: - uses: actions/checkout@v6 diff --git a/.github/workflows/setup-tests-with-custom-base-port.yaml b/.github/workflows/setup-tests-with-custom-base-port.yaml index c08290574..ea2a78243 100644 --- a/.github/workflows/setup-tests-with-custom-base-port.yaml +++ b/.github/workflows/setup-tests-with-custom-base-port.yaml @@ -19,6 +19,7 @@ jobs: runs-on: ubicloud-standard-16 env: NEXT_PUBLIC_STACK_PORT_PREFIX: "69" + STACK_FORCE_EXTERNAL_DB_SYNC: "true" steps: - uses: actions/checkout@v6 diff --git a/.github/workflows/setup-tests.yaml b/.github/workflows/setup-tests.yaml index 9dd86c057..0611c9051 100644 --- a/.github/workflows/setup-tests.yaml +++ b/.github/workflows/setup-tests.yaml @@ -17,6 +17,8 @@ env: jobs: setup-tests: runs-on: ubicloud-standard-16 + env: + STACK_FORCE_EXTERNAL_DB_SYNC: "true" steps: - uses: actions/checkout@v6 diff --git a/apps/e2e/.env.development b/apps/e2e/.env.development index 331666f8c..42b681a54 100644 --- a/apps/e2e/.env.development +++ b/apps/e2e/.env.development @@ -10,3 +10,5 @@ STACK_INBUCKET_API_URL=http://localhost:${NEXT_PUBLIC_STACK_PORT_PREFIX:-81}05 STACK_SVIX_SERVER_URL=http://localhost:${NEXT_PUBLIC_STACK_PORT_PREFIX:-81}13 STACK_EMAIL_MONITOR_SECRET_TOKEN=this-secret-token-is-for-local-development-only + +CRON_SECRET=mock_cron_secret diff --git a/apps/e2e/tests/backend/endpoints/api/v1/external-db-sync-utils.ts b/apps/e2e/tests/backend/endpoints/api/v1/external-db-sync-utils.ts index 2dc123126..c6bd6f20d 100644 --- a/apps/e2e/tests/backend/endpoints/api/v1/external-db-sync-utils.ts +++ b/apps/e2e/tests/backend/endpoints/api/v1/external-db-sync-utils.ts @@ -10,6 +10,9 @@ export const POSTGRES_USER = process.env.EXTERNAL_DB_TEST_USER || 'postgres'; export const POSTGRES_PASSWORD = process.env.EXTERNAL_DB_TEST_PASSWORD || 'PASSWORD-PLACEHOLDER--uqfEC1hmmv'; export const TEST_TIMEOUT = 120000; export const HIGH_VOLUME_TIMEOUT = 600000; // 10 minutes for 1500+ users +const SHOULD_FORCE_EXTERNAL_DB_SYNC = process.env.STACK_FORCE_EXTERNAL_DB_SYNC === 'true'; +const FORCE_SYNC_INTERVAL_MS = 2000; +let lastForcedSyncAt = -Infinity; // Connection settings to prevent connection leaks const CLIENT_CONFIG: Partial = { @@ -126,10 +129,11 @@ export async function waitForCondition( options: { timeoutMs?: number, intervalMs?: number, description?: string } = {} ): Promise { const { timeoutMs = 10000, intervalMs = 100, description = 'condition' } = options; - const startTime = Date.now(); + const startTime = performance.now(); - while (Date.now() - startTime < timeoutMs) { + while (performance.now() - startTime < timeoutMs) { try { + await maybeForceExternalDbSync(); if (await checkFn()) { return; } @@ -148,6 +152,30 @@ export async function waitForCondition( throw new Error(`Timeout waiting for ${description} after ${timeoutMs}ms`); } +async function maybeForceExternalDbSync() { + if (!SHOULD_FORCE_EXTERNAL_DB_SYNC) return; + + const now = performance.now(); + if (now - lastForcedSyncAt < FORCE_SYNC_INTERVAL_MS) return; + lastForcedSyncAt = now; + + const cronSecret = process.env.CRON_SECRET; + if (!cronSecret) { + throw new Error('CRON_SECRET is required when STACK_FORCE_EXTERNAL_DB_SYNC=true'); + } + + await niceFetch(new URL('/api/latest/internal/external-db-sync/sequencer', STACK_BACKEND_BASE_URL), { + headers: { + Authorization: `Bearer ${cronSecret}`, + }, + }); + await niceFetch(new URL('/api/latest/internal/external-db-sync/poller', STACK_BACKEND_BASE_URL), { + headers: { + Authorization: `Bearer ${cronSecret}`, + }, + }); +} + /** * Wait for data to appear in external DB (relies on automatic cron job) */