@@ -15,6 +15,7 @@ import {
1515 type InMemoryStripe ,
1616 stripeClientMock ,
1717} from '@sim/testing/mocks/stripe.mock'
18+ import { sleep } from '@sim/utils/helpers'
1819import { generateId } from '@sim/utils/id'
1920import { type BetterAuthOptions , betterAuth } from 'better-auth'
2021import { createAuthMiddleware } from 'better-auth/api'
@@ -372,7 +373,7 @@ type TestTransaction = Parameters<Parameters<typeof testDatabase.transaction>[0]
372373 * Starts a transaction that takes its locks in `holdLocks`, then parks until released and runs
373374 * `finish`. `untilBlocking` resolves once another backend is waiting on one of its locks.
374375 */
375- function startParkedTransaction (
376+ async function startParkedTransaction (
376377 holdLocks : ( tx : TestTransaction ) => Promise < void > ,
377378 finish : ( tx : TestTransaction ) => Promise < void > = async ( ) => { }
378379) {
@@ -393,15 +394,17 @@ function startParkedTransaction(
393394 } )
394395 async function untilBlocking ( ) {
395396 const pid = await holderPid
396- for ( let attempt = 0 ; attempt < 200 ; attempt ++ ) {
397+ const deadline = Date . now ( ) + 5000
398+ while ( Date . now ( ) < deadline ) {
397399 const [ row ] = await connection < { blocked : number } [ ] > `
398400 select count(*)::int as blocked from pg_stat_activity
399401 where ${ pid } ::int = any(pg_blocking_pids(pid))`
400402 if ( row . blocked > 0 ) return
401- await new Promise < void > ( ( resolve ) => setImmediate ( resolve ) )
403+ await sleep ( 10 )
402404 }
403405 throw new Error ( 'No transaction ever waited on the parked one' )
404406 }
407+ await Promise . race ( [ holderPid , done ] )
405408 return { done, release, untilBlocking }
406409}
407410
@@ -1019,7 +1022,7 @@ describe('Team activation', () => {
10191022 } )
10201023 stripe . addSubscription ( { id : stripeSubscriptionId , customer : `cus_${ subscriptionId } ` } )
10211024
1022- const cancelling = startParkedTransaction ( async ( tx ) => {
1025+ const cancelling = await startParkedTransaction ( async ( tx ) => {
10231026 await tx
10241027 . update ( subscription )
10251028 . set ( { cancelAtPeriodEnd : true } )
@@ -1039,8 +1042,12 @@ describe('Team activation', () => {
10391042 workspaceIdsToAttach : [ ] ,
10401043 } )
10411044 )
1042- await cancelling . untilBlocking ( )
1043- cancelling . release ( )
1045+ try {
1046+ await cancelling . untilBlocking ( )
1047+ } finally {
1048+ cancelling . release ( )
1049+ await Promise . allSettled ( [ cancelling . done , activating ] )
1050+ }
10441051 await cancelling . done
10451052 await expect ( activating ) . resolves . toMatchObject ( { success : true } )
10461053 expect ( ( await storedSubscription ( subscriptionId ) ) . cancelAtPeriodEnd ) . toBe ( false )
@@ -1066,7 +1073,7 @@ describe('operator retry', () => {
10661073 )
10671074 await deadLetter ( pauseSync )
10681075
1069- const writing = startParkedTransaction (
1076+ const writing = await startParkedTransaction (
10701077 async ( tx ) => {
10711078 await tx
10721079 . update ( subscription )
@@ -1083,8 +1090,12 @@ describe('operator retry', () => {
10831090 }
10841091 )
10851092 const requeuing = requeueFromAdminApi ( pauseSync )
1086- await writing . untilBlocking ( )
1087- writing . release ( )
1093+ try {
1094+ await writing . untilBlocking ( )
1095+ } finally {
1096+ writing . release ( )
1097+ await Promise . allSettled ( [ writing . done , requeuing ] )
1098+ }
10881099
10891100 await expect ( Promise . all ( [ writing . done , requeuing ] ) ) . resolves . toBeDefined ( )
10901101 await deliverUnrelatedUpdate ( pro . stripeSubscriptionId )
@@ -1107,7 +1118,7 @@ describe('operator retry', () => {
11071118 )
11081119 await deadLetter ( cancelSync )
11091120
1110- const writing = startParkedTransaction (
1121+ const writing = await startParkedTransaction (
11111122 async ( tx ) => {
11121123 await tx
11131124 . update ( subscription )
@@ -1129,8 +1140,12 @@ describe('operator retry', () => {
11291140 timing : 'period_end' ,
11301141 actor,
11311142 } )
1132- await writing . untilBlocking ( )
1133- writing . release ( )
1143+ try {
1144+ await writing . untilBlocking ( )
1145+ } finally {
1146+ writing . release ( )
1147+ await Promise . allSettled ( [ writing . done , retrying ] )
1148+ }
11341149
11351150 await expect ( Promise . all ( [ writing . done , retrying ] ) ) . resolves . toBeDefined ( )
11361151 expect ( ( await storedSubscription ( org . subscriptionId ) ) . cancelAtPeriodEnd ) . toBe ( true )
0 commit comments