Repository navigation
test(server): await the pg-status mount instead of a fixed number of turns (TASK-129) - #1914
Merged
Merged
Conversation
…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.
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'.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 laterafterEachhooks no longer leakPG_HOST.The row's stated mechanism does not reproduce — measured
The row reads: the mount "lands inside the
connectPG().thenchain while the test awaits onesetImmediate". That chain is all microtasks (connectPGandinitializePGDBare 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./api/pg/statusanswers non-404, instrumented probe, 20 iterationsserver.test.jsin a full tier,--randomize, soloserver.test.jsin a full tier while a second jest process ranSo 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.tsand reverting it:All injections are the same patch on
backend/server.ts, insideif (process.env.PG_HOST)at hunk@@ -378,8 +378,9 @@— theconnectPG()call immediately aboveinitializePGDB():setImmediateinserted at that pointapp.use('/api/pg/status', …)commented outavailable:truecase)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:
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
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.afterEachhooks of theserver route precedenceandserver websocket authorization helpersdescribes nowdelete 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)
delete process.env.PG_HOSTlines 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.