@@ -10,6 +10,9 @@ export const POSTGRES_USER = process.env.EXTERNAL_DB_TEST_USER || 'postgres';
1010export const POSTGRES_PASSWORD = process . env . EXTERNAL_DB_TEST_PASSWORD || 'PASSWORD-PLACEHOLDER--uqfEC1hmmv' ;
1111export const TEST_TIMEOUT = 120000 ;
1212export const HIGH_VOLUME_TIMEOUT = 600000 ; // 10 minutes for 1500+ users
13+ const SHOULD_FORCE_EXTERNAL_DB_SYNC = process . env . STACK_FORCE_EXTERNAL_DB_SYNC === 'true' ;
14+ const FORCE_SYNC_INTERVAL_MS = 2000 ;
15+ let lastForcedSyncAt = - Infinity ;
1316
1417// Connection settings to prevent connection leaks
1518const CLIENT_CONFIG : Partial < ClientConfig > = {
@@ -126,10 +129,11 @@ export async function waitForCondition(
126129 options : { timeoutMs ?: number , intervalMs ?: number , description ?: string } = { }
127130) : Promise < void > {
128131 const { timeoutMs = 10000 , intervalMs = 100 , description = 'condition' } = options ;
129- const startTime = Date . now ( ) ;
132+ const startTime = performance . now ( ) ;
130133
131- while ( Date . now ( ) - startTime < timeoutMs ) {
134+ while ( performance . now ( ) - startTime < timeoutMs ) {
132135 try {
136+ await maybeForceExternalDbSync ( ) ;
133137 if ( await checkFn ( ) ) {
134138 return ;
135139 }
@@ -148,6 +152,30 @@ export async function waitForCondition(
148152 throw new Error ( `Timeout waiting for ${ description } after ${ timeoutMs } ms` ) ;
149153}
150154
155+ async function maybeForceExternalDbSync ( ) {
156+ if ( ! SHOULD_FORCE_EXTERNAL_DB_SYNC ) return ;
157+
158+ const now = performance . now ( ) ;
159+ if ( now - lastForcedSyncAt < FORCE_SYNC_INTERVAL_MS ) return ;
160+ lastForcedSyncAt = now ;
161+
162+ const cronSecret = process . env . CRON_SECRET ;
163+ if ( ! cronSecret ) {
164+ throw new Error ( 'CRON_SECRET is required when STACK_FORCE_EXTERNAL_DB_SYNC=true' ) ;
165+ }
166+
167+ await niceFetch ( new URL ( '/api/latest/internal/external-db-sync/sequencer' , STACK_BACKEND_BASE_URL ) , {
168+ headers : {
169+ Authorization : `Bearer ${ cronSecret } ` ,
170+ } ,
171+ } ) ;
172+ await niceFetch ( new URL ( '/api/latest/internal/external-db-sync/poller' , STACK_BACKEND_BASE_URL ) , {
173+ headers : {
174+ Authorization : `Bearer ${ cronSecret } ` ,
175+ } ,
176+ } ) ;
177+ }
178+
151179/**
152180 * Wait for data to appear in external DB (relies on automatic cron job)
153181 */
0 commit comments