Repository navigation
fix(test): remove the test harness's timing, name-resolution and pool dependence (#798) - #809
Merged
Merged
Conversation
… dependence (#798) A test server flaked once with read tcp [::1]:59530->[::1]:59492: read: connection reset by peer in a subtest that took 5.19s where its siblings took milliseconds. The sibling case in the same file then failed identically while verifying #801, which places the defect in the shared harness rather than in either test. Substrate sells "a red test is a real signal, not timing noise", so the harness is held to it. Three structural causes, fixed together: 1. The probe's result was discarded — it broke out of a 5s loop on success and fell out of it on failure, returning a *TestServer either way. A server that never came up produced no message, and the first API call reported a transport error from somewhere else. It now t.Fatalf's, naming the address and the last error. 2. The probe was untimed, on http.DefaultClient (Timeout 0). The listener is opened before the serving goroutine, so an attempt lands in the accept backlog and blocks rather than failing — one attempt could consume the whole deadline instead of retrying every 10ms, which is how a subtest reached 5.19s. 3. The probe pooled a keep-alive connection into http.DefaultClient, shared by 149 call sites across 54 test files, while ports are recycled inside one `go test` process and every test server is shut down at the end of its test. A pooled connection to a dead server fails on *read* — after the request was written — which is exactly "connection reset by peer", and it surfaces in whichever later test drew the recycled port. No IdleTimeout is configured, so net/http falls back to ReadTimeout: measured at 30s by the new assertion that the server hangs up, run against the old harness. A test server now disables keep-alives via a new ServerOptions.DisableKeepAlives (off by default), which removes the hazard server-side for every caller at once rather than converting 149 clients. It also binds and is dialled on the literal 127.0.0.1 rather than the name "localhost", which has two answers on a dual-stack host — docs/testing-guide.md already documented TestServer.URL that way. No production behaviour changes. Closes #798
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
… is tested Refactor awaitTestServer to return an error and take its deadline, so the StartTestServer call site does the t.Fatalf and the give-up path is reachable from a test — it cannot be reached through StartTestServer, whose listener is bound before the probe runs (#798).
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.
The failure
The diagnosis was confirmed mid-milestone. While verifying #801, the sibling case in the same
file —
TestConfigChannel_ThePolicyMatcherRefusesWhatAdmitsNothing— failed the same way, same[::1]address, on go1.26.6. Nothing in either test is at fault: the shape belongs to the sharedharness. That answers the issue's third criterion ("if the same shape exists in sibling tests, they
are fixed with it") by construction — the fix lives in
startTestServer, so all 149 call sites arecovered.
Three structural causes, all on that one path
1. The probe's result was discarded.
startTestServerbroke out of a 5-second loop on successand simply fell out of it on failure, returning a
*TestServereither way. A server that never cameup produced no message, and the first API call reported a confusing transport error from somewhere
else entirely — which is why the reported 5.19s was invisible. It now
t.Fatalfs, naming the addressand the last probe error.
2. The probe was untimed —
http.Getonhttp.DefaultClient, whoseTimeoutis0. Thelistener is opened before the serving goroutine starts, so a connection lands in the kernel's accept
backlog and blocks rather than failing: one attempt could consume the entire deadline instead of
retrying every 10ms. That is how a subtest reached 5.19s. Each attempt is now bounded at 250ms.
3. The probe deposited a keep-alive connection into the process-wide
http.DefaultClientpool,keyed
localhost:<port>and shared by 149 call sites across 54 test files. Ports are recycled insideone
go testprocess, and every test server is shut down withShutdownat the end of its test. Apooled connection to a dead server fails on read, after the request was written — precisely
read: connection reset by peer— and it surfaces in whichever later test drew the recycled port,not in the test that leaked it.
Substrate configures no
IdleTimeout, sonet/httpfalls back toReadTimeout. That is notinferred: the new assertion that the server hangs up takes 30.00s against the old harness and
0.00s against this one, which is
ReadTimeoutfiring.The fix
ServerOptions.DisableKeepAlives(new, defaults to off) — set byStartTestServer. Closingserver-side is what makes a stale pooled connection impossible for every caller at once, rather
than converting 149 clients to private transports. A real emulator run is unaffected: an AWS SDK
wants keep-alives and gets them.
DisableKeepAlives, so the probecannot block past its turn and pools nothing.
t.Fatalfwhen the probe never answered. Defensive — the server is in-process with itslistener already bound — and commented as such rather than tested.
127.0.0.1, not the namelocalhost, which has two answers on adual-stack host:
net.Listenpicks one family for the bind while a client resolves the nameindependently, and the failure showed
[::1]on both ends. It also removes a resolver call from apath
CLAUDE.mdforbids from touching the network.docs/testing-guide.mdalready documentedTestServer.URLashttp://127.0.0.1:54321— the codenow agrees with the docs rather than the other way round.
Tests —
emulator/testing_harness_test.goFour cases, of which two fail on
main:main…IsReachedByAddressNotByNamehttp://localhost:…)…PoolsNoConnectionThatCouldOutliveIt…AnswersHealthBeforeItReturns…ManyServersInOneProcessDoNotInterfereThe last two pass on
maindeliberately: a race cannot be proven absent by a test. They pin thedocumented contract and reproduce the setup the reported failure needed — 25 servers started and
torn down in one process so ports recycle, each request going through
http.DefaultClientonpurpose, since that shared pool is where the stale connection used to live.
Verification
gofmtclean;go build,go vet,golangci-lint run ./...(0 issues);go test -race -run TestConfigChannel ./emulator/ -count=20(clean — the reported test and itssiblings, 20×);
make test(race) on go1.27.1 and go1.26.6; twoGOTOOLCHAIN=go1.26.6 make coverageruns, both at baseline (emulator 83.6%, total 83.0%);make docs-reference docs-reference-check docs-versions authz-reference-check;go vet -C test/e2e ./... && go test -C test/e2e ./....npm --prefix docs run buildwas not runlocally (this branch was built in a throwaway worktree with no
docs/node_modules); the VitePressbuild job covers it, and no file the site renders changed except
CHANGELOG.md.Ordering note
This is PR 6 of the approved v0.112.0 plan, taken second. The flake reproduced during #801's own
verification gate, so leaving it until last meant every remaining PR's gate carried a known false
red. Nothing else in the milestone touches
testing.goorserver.go.Compatibility
No production behaviour changes. A downstream test using
emulator.StartTestServerwill seets.URLon127.0.0.1rather thanlocalhost, and its server will close each connection after oneresponse.
ServerOptions.DisableKeepAlivesis additive and off by default.Closes #798