Skip to content

test(server): await the pg-status mount instead of a fixed number of turns (TASK-129) - #1914

Merged
lilyshen0722 merged 1 commit into
mainfrom
kai/129-pg-status-race
Sep 26, 2026
Merged

lilyshen0722 merged 1 commit into
mainfrom
kai/129-pg-status-race

Conversation

@lilyshen0722

@lilyshen0722 lilyshen0722 commented Sep 26, 2026 •

Copy link
Copy Markdown
Contributor

What

backend/__tests__/unit/server.test.js — the four PG-status tests no longer wait a fixed number of macrotask turns for a mount that is asynchronous, and the two later afterEach hooks no longer leak PG_HOST.

The row's stated mechanism does not reproduce — measured

The row reads: the mount "lands inside the connectPG().then chain while the test awaits one setImmediate". That chain is all microtasks (connectPG and initializePGDB are both mocked in this file), so one macrotask turn is not a guess about how far the chain has got: microtasks always drain before any macrotask.

measurement result
turns until /api/pg/status answers non-404, instrumented probe, 20 iterations 1 turn, 20/20
same probe under a concurrent full-tier run 1 turn, 10/10
server.test.js in a full tier, --randomize, solo PASS (19.9 s)
server.test.js in a full tier while a second jest process ran PASS (11.5 s)
loop, 12 randomized runs, unmodified chain 0/12 red

So I could not reproduce a red and I am not claiming this removes one. If the output from the run that produced the row is still around, the test name plus the received body would let me aim at the real cause instead of at the shape below.

The brittleness that is demonstrable, and why the change is still right

The wait encodes the chain's yield profile. Today the chain never yields a macrotask; add one and the fixed-turn wait stops being enough. Mutating server.ts and reverting it:

All injections are the same patch on backend/server.ts, inside if (process.env.PG_HOST) at hunk @@ -378,8 +378,9 @@ — the connectPG() call immediately above initializePGDB():

   connectPG()
-    .then((pgPool: any) => {
+    .then(async (pgPool: any) => {
       if (pgPool) {
+        await new Promise((resolve: any) => { setTimeout(resolve, N); });
         // Initialize PostgreSQL database
         initializePGDB()
arm chain test result
M1a one setImmediate inserted at that point base (fixed turn) 3/3 green — the request path absorbs one yield
M1b a delay inserted at that point base (fixed turn) RED from 2 ms up — see the sweep
M1b same delays this PR green at every delay tested, 0–100 ms
M2 success branch's app.use('/api/pg/status', …) commented out this PR RED — the helper's fallback reports the route's real 404 body (1 of 9, the available:true case)

M2 is the anchored half: the helper cannot turn "never mounted" into a pass. M1a is disclosed because it did not redden the base — one inserted yield is absorbed by the request path, so the threshold is a real delay, not any yield.

The delay arm is machine-dependent, and the sweep is the honest form of it — one run per cell, same box:

chain delay base test this PR
0 ms green green
1 ms green green
2 ms 1 failed green
5 ms 1 failed green
10 ms 3 failed green
20 ms 3 failed green
50 ms 3 failed green
100 ms 3 failed green

A reviewer on a different box measured the same 5 ms patch green. That is the mechanism, not a contradiction: the old wait resolves after one macrotask turn and then starts a supertest request whose own dispatch takes a few macrotasks, so a chain delay shorter than that request path still lands in time. The threshold is ~1–2 ms here and ≥5 ms there. 50 ms is the arm that should reproduce anywhere; if it does not, the benefit is unwitnessed in that environment and the coupling claim is all that is left.

Vera re-verified independently at 2bc06261: removing the mount reddens 1 of 9, and the file's wall-clock is within noise (base 22.8 s / head 24.4 s, with the slow first test slower at base).

The change

  • One local helper, pgStatus(app): request /api/pg/status, return on anything that is not 404, otherwise yield a macrotask and retry, bounded by a 2 s deadline and 200 attempts. On exhaustion it makes one final request so the assertion reports what the route actually answers.
  • The afterEach hooks of the server route precedence and server websocket authorization helpers describes now delete process.env.PG_HOST, as the first describe already did. In a randomized order a PG test could leave it set for whatever ran next. Hygiene — I have not shown it causing a failure.

Scope

Test-only: one helper plus the two hooks; no production code. eslint on the file: no findings before or after. Full file 9/9 green, 6/6 randomized runs.

Gate

@vera — tests.

Corrections after review (body edits do not move the head)

  • The M1b row originally quoted "a 5 ms delay reddens the base test 3/3" as a single number. Vera could not reproduce it. The sweep above replaces that point reading: at 5 ms my box reddens 1 test and hers does not, and the reproducible arm is 50 ms.
  • Her two side notes are accepted without change: the added delete process.env.PG_HOST lines are no-ops today (the variable is only set inside the first describe, which already deletes it — they guard a future setter), and the nine worker processes that outlive a green run are pre-existing at base and head.

…turns (TASK-129)

The four PG-status tests waited one setImmediate for a route that is mounted
inside the connectPG().then chain in server.ts. The chain is all microtasks
(both connectPG and initializePGDB are mocked here), so one macrotask turn is
not a guess about the chain's progress - microtasks drain before any macrotask.
Measured: 20/20 iterations need exactly 1 turn, 10/10 under a concurrent
full-tier run, and the file passes in a full tier both alone and under load.
The row's red therefore does not reproduce here.

The wait does encode the chain's yield profile, though, and that is what this
changes. Mutating server.ts to add a 5ms delay before initializePGDB (the shape
a connect retry has) reddens the old wait 3/3 while this version stays green
3/3; commenting out the mount reddens this version too, so the helper cannot
turn "never mounted" into a pass.
@lilyshen0722
lilyshen0722 added this pull request to the merge queue Sep 26, 2026
Merged via the queue into main with commit 670f723 Sep 26, 2026
19 checks passed
samxu01 pushed a commit that referenced this pull request Sep 26, 2026
Conflict: backend/__tests__/unit/server.test.js, on the one describe this PR
replaces. #1914 (TASK-129) rewrote the four 'server pg status route' tests in
place to await the mount instead of a fixed number of turns; this PR deletes
those four and replaces them with the pg-boot-route suite, because the real
status router is now mounted unconditionally rather than only after a
successful connect. Resolution takes the branch's describe block and keeps
#1914's non-conflicting hunks: 'delete process.env.PG_HOST' in the afterEach of
'server route precedence' and 'server websocket authorization helpers'.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant