Skip to content

fix(test): remove the test harness's timing, name-resolution and pool dependence (#798) - #809

Merged
scttfrdmn merged 2 commits into
mainfrom
fix/798-test-harness-timing
Sep 12, 2026
Merged

scttfrdmn merged 2 commits into
mainfrom
fix/798-test-harness-timing

Conversation

@scttfrdmn

Copy link
Copy Markdown
Owner

The failure

--- FAIL: TestConfigChannel_PutIsIdempotentAndASecondNameIsRefused (5.19s)
    configservice_channel_test.go:426: POST PutConfigurationRecorder: Post "http://localhost:59492/":
      read tcp [::1]:59530->[::1]:59492: read: connection reset by peer

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 shared
harness. 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 are
covered.

Three structural causes, all on that one path

1. The probe's result was discarded. startTestServer broke out of a 5-second loop on success
and simply 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 confusing transport error from somewhere
else entirely — which is why the reported 5.19s was invisible. It now t.Fatalfs, naming the address
and the last probe error.

2. The probe was untimed — http.Get on http.DefaultClient, whose Timeout is 0. The
listener 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.DefaultClient pool,
keyed localhost:<port> and shared by 149 call sites across 54 test files. Ports are recycled inside
one go test process, and every test server is shut down with Shutdown at the end of its test. A
pooled 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, so net/http falls back to ReadTimeout. That is not
inferred: the new assertion that the server hangs up takes 30.00s against the old harness and
0.00s against this one, which is ReadTimeout firing.

The fix

  • ServerOptions.DisableKeepAlives (new, defaults to off) — set by StartTestServer. Closing
    server-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.
  • A dedicated probe client with a per-attempt timeout and DisableKeepAlives, so the probe
    cannot block past its turn and pools nothing.
  • t.Fatalf when the probe never answered. Defensive — the server is in-process with its
    listener already bound — and commented as such rather than tested.
  • Bind and dial the literal 127.0.0.1, not the name localhost, which has two answers on a
    dual-stack host: net.Listen picks one family for the bind while a client resolves the name
    independently, and the failure showed [::1] on both ends. It also removes a resolver call from a
    path CLAUDE.md forbids from touching the network.

docs/testing-guide.md already documented TestServer.URL as http://127.0.0.1:54321 — the code
now agrees with the docs rather than the other way round.

Tests — emulator/testing_harness_test.go

Four cases, of which two fail on main:

main this branch
…IsReachedByAddressNotByName FAIL (http://localhost:…) pass
…PoolsNoConnectionThatCouldOutliveIt FAIL after 30.00s (server never hangs up) pass, 0.00s
…AnswersHealthBeforeItReturns pass pass
…ManyServersInOneProcessDoNotInterfere pass pass

The last two pass on main deliberately: a race cannot be proven absent by a test. They pin the
documented 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.DefaultClient on
purpose, since that shared pool is where the stale connection used to live.

Verification

gofmt clean; go build, go vet, golangci-lint run ./... (0 issues);
go test -race -run TestConfigChannel ./emulator/ -count=20 (clean — the reported test and its
siblings, 20×); make test (race) on go1.27.1 and go1.26.6; two GOTOOLCHAIN=go1.26.6 make coverage runs, 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 build was not run
locally (this branch was built in a throwaway worktree with no docs/node_modules); the VitePress
build 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.go or server.go.

Compatibility

No production behaviour changes. A downstream test using emulator.StartTestServer will see
ts.URL on 127.0.0.1 rather than localhost, and its server will close each connection after one
response. ServerOptions.DisableKeepAlives is additive and off by default.

Closes #798

… 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

codecov Bot commented Sep 11, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 92.00000% with 2 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
emulator/server.go 75.00% 1 Missing ⚠️
emulator/testing.go 95.23% 1 Missing ⚠️

📢 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).
@scttfrdmn
scttfrdmn merged commit 34254a3 into main Sep 12, 2026
14 checks passed
@scttfrdmn
scttfrdmn deleted the fix/798-test-harness-timing branch September 12, 2026 00:12
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.

test: TestConfigChannel_PutIsIdempotentAndASecondNameIsRefused flakes with connection reset

1 participant