Skip to content

Commit 8f36f2d

Browse files
committed
test(projects): verify concurrent lifecycle lock contention
1 parent d3c56a0 commit 8f36f2d

1 file changed

Lines changed: 21 additions & 6 deletions

File tree

‎packages/db/scripts/project-contract.integration.ts‎

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -381,20 +381,35 @@ describe('Project expand/backfill/contract against PostgreSQL', () => {
381381
await release.promise
382382
})
383383
await archived.promise
384-
const started = createDeferred<void>()
384+
const started = createDeferred<number>()
385385
const second = sql
386386
.begin(`isolation level ${isolation}`, async (tx) => {
387-
await tx`SELECT count(*) FROM workspace WHERE archived_at IS NULL`
388-
started.resolve()
387+
const [connection] =
388+
await tx`SELECT pg_backend_pid() AS pid, count(*) FROM workspace WHERE archived_at IS NULL`
389+
started.resolve(connection.pid)
389390
await tx`UPDATE workspace SET archived_at = now() WHERE id = 'child'`
390391
})
391392
.then(
392393
() => null,
393394
(error: unknown) => error
394395
)
395-
await started.promise
396-
release.resolve()
397-
await first
396+
const secondPid = await started.promise
397+
try {
398+
let waiting = false
399+
for (let attempt = 0; attempt < 100; attempt++) {
400+
const [state] =
401+
await sql`SELECT cardinality(pg_blocking_pids(${secondPid})) > 0 AS waiting`
402+
if (state.waiting) {
403+
waiting = true
404+
break
405+
}
406+
await sleep(10)
407+
}
408+
expect(waiting).toBe(true)
409+
} finally {
410+
release.resolve()
411+
await first
412+
}
398413
const failure = await second
399414
expect(['23514', '40001', '40P01']).toContain(getPostgresErrorCode(failure))
400415
expect(await sql`SELECT 1 FROM workspace WHERE archived_at IS NULL`).toHaveLength(1)

0 commit comments

Comments
 (0)