Skip to content

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

TypeScript

test(control-api): add a pool teardown that waits for its clients to close

Alfredo Lopez27ff803ab6a3
diff --git a/control-api/package-lock.json b/control-api/package-lock.json
index 255a7e696..7bc4125fe 100644
--- a/control-api/package-lock.json
+++ b/control-api/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "clerum-control-api",
- "version": "0.1.686",
+ "version": "0.1.687",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "clerum-control-api",
- "version": "0.1.686",
+ "version": "0.1.687",
"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 c80efb5a9..d2e83ca3a 100644
--- a/control-api/package.json
+++ b/control-api/package.json
@@ -1,6 +1,6 @@
{
"name": "clerum-control-api",
- "version": "0.1.686",
+ "version": "0.1.687",
"description": "Internal Clerum control plane REST API + UI for managing CRDs and secrets",
"main": "dist/main.js",
"engines": {
diff --git a/control-api/test/helpers/realPostgresTeardown.ts b/control-api/test/helpers/realPostgresTeardown.ts
new file mode 100644
index 000000000..4e71fe6a2
--- /dev/null
+++ b/control-api/test/helpers/realPostgresTeardown.ts
@@ -0,0 +1,40 @@
+import type { Pool } from 'pg'
+
+/**
+ * Ends a pg pool and resolves only after every client it held has closed its
+ * connection.
+ *
+ * pg-pool's end() resolves as soon as its client list is empty, while each
+ * client.end() it started is still in flight. A real-Postgres teardown that
+ * then runs pg_terminate_backend can reach a client that is still closing; the
+ * 57P01 it receives is re-emitted on the pool and, with no pool `error`
+ * listener, fails the run as an unhandled error (#946). pg-pool emits `remove`
+ * from each client's end callback, so waiting for one `remove` per client open
+ * at end() closes that window without listening for errors.
+ *
+ * A pool that was never created (setup failed first) is left alone, as the
+ * `pool?.end()` it replaces did.
+ */
+export async function endPoolAndWaitForClients(pool: Pool | null | undefined): Promise<void> {
+ if (!pool) return
+ const open = pool.totalCount
+ let removed = 0
+ let onRemove: (() => void) | undefined
+ const closed = new Promise<void>(resolve => {
+ if (open === 0) {
+ resolve()
+ return
+ }
+ onRemove = () => {
+ removed += 1
+ if (removed === open) resolve()
+ }
+ pool.on('remove', onRemove)
+ })
+ try {
+ await pool.end()
+ await closed
+ } finally {
+ if (onRemove) pool.off('remove', onRemove)
+ }
+}