Skip to content

Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure

TypeScript

fix: wait for Postgres test connections before database cleanup

Josuéd693856c1773
diff --git a/control-api/package-lock.json b/control-api/package-lock.json
index 9c880649c..681d569f4 100644
--- a/control-api/package-lock.json
+++ b/control-api/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "clerum-control-api",
- "version": "0.1.681",
+ "version": "0.1.682",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "clerum-control-api",
- "version": "0.1.681",
+ "version": "0.1.682",
"dependencies": {
"@clerum/display-field": "file:../packages/display-field",
"@clerum/grok-provider-attempt-contract": "file:../packages/grok-provider-attempt-contract",
diff --git a/control-api/package.json b/control-api/package.json
index 2e64e8220..e82aab44f 100644
--- a/control-api/package.json
+++ b/control-api/package.json
@@ -1,6 +1,6 @@
{
"name": "clerum-control-api",
- "version": "0.1.681",
+ "version": "0.1.682",
"description": "Internal Clerum control plane REST API + UI for managing CRDs and secrets",
"main": "dist/main.js",
"engines": {
diff --git a/control-api/test/realPostgresCleanup.ts b/control-api/test/realPostgresCleanup.ts
new file mode 100644
index 000000000..7a6f19bee
--- /dev/null
+++ b/control-api/test/realPostgresCleanup.ts
@@ -0,0 +1,17 @@
+import type { Pool } from 'pg'
+
+export async function waitForDatabaseConnectionsToClose(
+ adminPool: Pool,
+ database: string
+): Promise<void> {
+ for (let attempt = 0; attempt < 40; attempt += 1) {
+ const result = await adminPool.query(
+ `SELECT 1 FROM pg_stat_activity WHERE datname = $1 LIMIT 1`,
+ [database]
+ )
+ if (result.rowCount === 0) return
+ await new Promise(resolve => setTimeout(resolve, 50))
+ }
+
+ throw new Error('test database still has PostgreSQL connections after its pool shut down')
+}
diff --git a/control-api/test/services.adminAuthDelete.realPostgres.integration.test.ts b/control-api/test/services.adminAuthDelete.realPostgres.integration.test.ts
index be5db320f..601ff4df6 100644
--- a/control-api/test/services.adminAuthDelete.realPostgres.integration.test.ts
+++ b/control-api/test/services.adminAuthDelete.realPostgres.integration.test.ts
@@ -4,6 +4,7 @@ import { Pool } from 'pg'
import { initDb, pool } from '../src/db.js'
import { deleteControlAdmin } from '../src/services/adminAuthService.js'
import './realPostgres.requirement.ts'
+import { waitForDatabaseConnectionsToClose } from './realPostgresCleanup.ts'
const adminUrl = process.env.CONTROL_API_REAL_PG_ADMIN_URL
const describeRealPostgres = adminUrl ? describe : describe.skip
@@ -74,15 +75,14 @@ describeRealPostgres('deleteControlAdmin on real PostgreSQL', () => {
afterAll(async () => {
querySpy?.mockRestore()
connectSpy?.mockRestore()
- await testPool?.end()
if (!adminPool) return
- await adminPool.query(
- `SELECT pg_terminate_backend(pid) FROM pg_stat_activity
- WHERE datname = $1 AND pid <> pg_backend_pid()`,
- [database]
- )
- await adminPool.query(`DROP DATABASE IF EXISTS ${quoteIdent(database)}`)
- await adminPool.end()
+ try {
+ await testPool?.end()
+ await waitForDatabaseConnectionsToClose(adminPool, database)
+ await adminPool.query(`DROP DATABASE IF EXISTS ${quoteIdent(database)}`)
+ } finally {
+ await adminPool.end()
+ }
})
// Regression for the plain DELETE /admin/control-admins/:id path (replaceInviter
diff --git a/control-api/test/services.controlAdminReplaceInviter.realPostgres.integration.test.ts b/control-api/test/services.controlAdminReplaceInviter.realPostgres.integration.test.ts
index c332562ca..59ca26618 100644
--- a/control-api/test/services.controlAdminReplaceInviter.realPostgres.integration.test.ts
+++ b/control-api/test/services.controlAdminReplaceInviter.realPostgres.integration.test.ts
@@ -24,6 +24,7 @@ import {
} from '../src/services/directory/membership.js'
import { retireDesktopUser } from '../src/services/directory/users.js'
import './realPostgres.requirement.ts'
+import { waitForDatabaseConnectionsToClose } from './realPostgresCleanup.ts'
const adminUrl = process.env.CONTROL_API_REAL_PG_ADMIN_URL
const describeRealPostgres = adminUrl ? describe : describe.skip
@@ -181,15 +182,14 @@ describeRealPostgres('control admin replace-inviter invitations on real PostgreS
afterAll(async () => {
querySpy?.mockRestore()
connectSpy?.mockRestore()
- await testPool?.end()
if (!adminPool) return
- await adminPool.query(
- `SELECT pg_terminate_backend(pid) FROM pg_stat_activity
- WHERE datname = $1 AND pid <> pg_backend_pid()`,
- [database]
- )
- await adminPool.query(`DROP DATABASE IF EXISTS ${quoteIdent(database)}`)
- await adminPool.end()
+ try {
+ await testPool?.end()
+ await waitForDatabaseConnectionsToClose(adminPool, database)
+ await adminPool.query(`DROP DATABASE IF EXISTS ${quoteIdent(database)}`)
+ } finally {
+ await adminPool.end()
+ }
})
it('stores replaceInviter and exposes it with the inviter on every invitation read', async () => {
diff --git a/control-api/test/services.directory.userRetirement.realPostgres.integration.test.ts b/control-api/test/services.directory.userRetirement.realPostgres.integration.test.ts
index 1f451658a..245b4d5d1 100644
--- a/control-api/test/services.directory.userRetirement.realPostgres.integration.test.ts
+++ b/control-api/test/services.directory.userRetirement.realPostgres.integration.test.ts
@@ -4,6 +4,7 @@ import { Pool } from 'pg'
import { initDb, pool } from '../src/db.js'
import { retireDesktopUser } from '../src/services/directory/users.js'
import { gfsDesktopOperatorLinkService } from '../src/services/gfsDesktopOperatorLinkService.js'
+import { waitForDatabaseConnectionsToClose } from './realPostgresCleanup.ts'
const adminUrl = process.env.CONTROL_API_REAL_PG_ADMIN_URL
const describeRealPostgres = adminUrl ? describe : describe.skip
@@ -56,15 +57,14 @@ describeRealPostgres('retireDesktopUser on real PostgreSQL', () => {
afterAll(async () => {
corePoolConnectSpy?.mockRestore()
- await testPool?.end()
if (!adminPool) return
- await adminPool.query(
- `SELECT pg_terminate_backend(pid) FROM pg_stat_activity
- WHERE datname = $1 AND pid <> pg_backend_pid()`,
- [database]
- )
- await adminPool.query(`DROP DATABASE IF EXISTS ${quoteIdent(database)}`)
- await adminPool.end()
+ try {
+ await testPool?.end()
+ await waitForDatabaseConnectionsToClose(adminPool, database)
+ await adminPool.query(`DROP DATABASE IF EXISTS ${quoteIdent(database)}`)
+ } finally {
+ await adminPool.end()
+ }
})
it('persists the legacy deleted outcome and returns it on an identical replay', async () => {