Files
coder/coderd/nats
Michael Suchacz 90a8d51418 test(coderd/nats): use a random cluster port in TestPubsub/CloseIdempotent (#31056)
`TestPubsub/CloseIdempotent` in `coderd/nats` flakes in CI with
"embedded nats server not ready within 10s" (for example in
[`test-go-pg-17` on
main](https://github.com/coder/coder/actions/runs/39412671012/job/118594196090)).

It is the only subtest that calls `nats.New` with `nats.Options{}`
directly. With `ClusterPort` unset, `buildServerOptions` falls back to
the fixed production cluster port 6222. Test packages run in parallel,
and `cli` `TestServer/SpammyLogs` runs `coder server --cluster-host
127.0.0.1`, which holds 127.0.0.1:6222. When the route listener cannot
bind, nats-server logs nothing under `NoLog`, and `ReadyForConnections`
waits out the 10 s `readyTimeout`.

The fix gives the subtest `ClusterPort: natsserver.RANDOM_PORT`, the
option the `newPubsub` helper already sets for every other subtest. It
is a one-line, test-only change.

## Testing

- Red on the merge base: with 127.0.0.1:6222 held by a plain listener,
`TestPubsub/CloseIdempotent` fails after 10.01 s with the CI message.
- Green on this branch: with the port held, `go test ./coderd/nats/...`
passes.
- `go test ./coderd/nats/... -count=3` and `make pre-commit` pass.
- Remote dogfood UAT ran on the tested SHA 451c4ecd16 and passed. It
repeated red and green with 127.0.0.1:6222 and 0.0.0.0:6222 held, ran
`-count=5` with no port held, and showed that an overlay whose second
`Close` returns an error makes the subtest fail, so it still checks
idempotent `Close`.

Polish and the test audit made no changes, so both stayed inside the
implementation diff (`coderd/nats/pubsub_test.go`).

Draft #30759 carries the same one-line edit as a drive-by, so whichever
lands second merges cleanly.

> Xum, an AI agent, opened this PR for @ibetitsmike and wrote this
description.
2026-10-20 19:50:13 +02:00
..