test: shut the embedded server down when readiness times out

main

Commit

Andrea Nodari3mo ago

All four embedded-server helpers registered t.Cleanup(s.Shutdown) only AFTER waiting for ReadyForConnections, so a readiness timeout hit t.Fatal with the server still running — still bound to its port, still holding its JetStream store.

The sharper half is the ordering with t.TempDir. Three of these four take StoreDir: t.TempDir(), which registers its own cleanup when it is called, i.e. BEFORE the Cleanup that was never reached. Cleanups run LIFO, so the store directory is removed underneath a live server rather than after it stopped. The consequence surfaces as a flake in a LATER parallel test, which is the kind of failure that gets blamed on anything but its cause.

Cleanup is now registered before the wait, where it covers both paths:

  • internal/brokersemantics/harness_test.go — startServer (carries the full explanation; it is the canonical harness and the suite others are modelled on)
  • jsconsumer/jsconsumer_test.go
  • natsmsg/publish_test.go
  • helpers_test.go — runEmbeddedServerWith (no JetStream store, so only the port and the goroutine leak, but the same shape and the same fix)

Found downstream: mirror-pipeline modelled internal/natstest on startServer here and inherited the bug with it, where Cursor Bugbot flagged it (mirror-pipeline #245). Fixing it at the source so the next repo to copy this pattern copies the right one.

No regression test. Forcing ReadyForConnections to time out needs a hook that does not exist, and an assertion exercising only the happy path would pin nothing — which this package's own doc is explicit about being worse than no test at all. The ordering requirement is stated in a comment instead, so a future edit has to argue with it rather than silently undo it.

Gate: build, vet, gofmt -s, go test ./..., go test -race ./..., golangci-lint (0 issues) — all clean.

Co-Authored-By: Claude Opus 5 (1M context) noreply@anthropic.com Claude-Session: https://claude.ai/code/session_019zK85NVAs1Enxh2eFidFpo Entire-Checkpoint: 00ee2390b216