| 1 |
# Testing |
| 2 |
|
| 3 |
## TL;DR |
| 4 |
|
| 5 |
```shell |
| 6 |
make test # full race-enabled suite, no docker |
| 7 |
make test T=TestName PKG=./test/... # iterate on a single test |
| 8 |
``` |
| 9 |
|
| 10 |
## How the suite is set up |
| 11 |
|
| 12 |
The repo uses two module files: `go.mod` for production (minimal dependencies) |
| 13 |
and `go_test.mod` for testing. Always pass `-modfile=go_test.mod` to `go test`; |
| 14 |
the make targets below do this for you. |
| 15 |
|
| 16 |
Unit tests live at the repo root (package `nats`, white-box) and run with plain |
| 17 |
`go test`. Integration tests live in `./test`, `./jetstream/test`, and |
| 18 |
`./micro/test`; they bring up real NATS servers by talking to a **tester** |
| 19 |
service, which spawns `nats-server` instances, clusters, and super-clusters on |
| 20 |
demand. Tests drive it through the `github.com/synadia-io/orbit.go/ntf-client` |
| 21 |
package. |
| 22 |
|
| 23 |
There are two ways to run that tester, and tests reach both the same way: |
| 24 |
|
| 25 |
- **In-process (the default)** — a `TestMain` starts |
| 26 |
`github.com/synadia-io/orbit.go/ntf` inside each test binary. No docker, no |
| 27 |
setup, nothing to tear down. |
| 28 |
- **Out-of-process** — the `synadia/ntf-server` docker image, located via the |
| 29 |
`TESTER_NATS_URL` environment variable. This is what CI uses. |
| 30 |
|
| 31 |
`TESTER_NATS_URL` always wins: set it and the tests talk to that tester instead |
| 32 |
of starting their own. The `make test` targets clear it so an exported value |
| 33 |
left over in your shell cannot silently redirect them, but a raw `go test` |
| 34 |
inherits it — unset it if a plain `go test` tries to reach a container you did |
| 35 |
not intend. |
| 36 |
|
| 37 |
The two paths do **not** necessarily run the same server build: in-process links |
| 38 |
whatever `nats-server` `orbit.go/ntf` depends on, while the container ships its |
| 39 |
own (pinned in `.github/workflows/ci.yaml`). To stop CI drifting onto the wrong |
| 40 |
one, `TestMain` refuses to start the in-process tester when `GITHUB_ACTIONS` is |
| 41 |
set but `TESTER_NATS_URL` is not. `GITHUB_ACTIONS` rather than the more general |
| 42 |
`CI` because devcontainers and other runners set that one too, and they have no |
| 43 |
reason to be forced onto a container. |
| 44 |
|
| 45 |
That guard only catches a missing URL, not a version difference, so the first |
| 46 |
test to reach the tester prints the server it got: |
| 47 |
|
| 48 |
```text |
| 49 |
tester at nats://localhost:62354: nats-server 2.14.5 |
| 50 |
``` |
| 51 |
|
| 52 |
`go test` shows this whenever the package is run with `-v` (all CI steps are) or |
| 53 |
when a test in it fails. If a failure reproduces locally but not in CI, or the |
| 54 |
reverse, compare that line against the image tag in `ci.yaml` first. |
| 55 |
|
| 56 |
## In-process mode (default, no docker) |
| 57 |
|
| 58 |
```shell |
| 59 |
make test # everything |
| 60 |
make test T=TestSubSubject PKG=./test/... # one test, verbose |
| 61 |
make test PKG=./jetstream/test/... # one package |
| 62 |
make test-norace # NoRace tests (race detector off) |
| 63 |
``` |
| 64 |
|
| 65 |
Equivalent to: |
| 66 |
|
| 67 |
```shell |
| 68 |
go test -modfile=go_test.mod -tags=internal_testing -race -p=1 ./... --failfast -vet=off |
| 69 |
``` |
| 70 |
|
| 71 |
Because the tester and the `nats-server` instances it spawns run inside the test |
| 72 |
binary, `-race` instruments the server too: expect somewhat slower runs, and note |
| 73 |
that a data race in `nats-server` surfaces here as a nats.go test failure. |
| 74 |
|
| 75 |
This is also why `go_test.mod` declares `go 1.26.0` while `go.mod` stays at |
| 76 |
`1.25.0` — `orbit.go/ntf` requires 1.26, and merely requiring a module raises the |
| 77 |
floor for the whole test module. Production builds are unaffected; 1.26 is the |
| 78 |
floor for the CI test matrix. |
| 79 |
|
| 80 |
## Testing against a different nats-server |
| 81 |
|
| 82 |
The in-process tester links `nats-server` as an ordinary Go dependency, so |
| 83 |
pointing it at another branch, tag, commit, or local checkout is a module |
| 84 |
operation — no docker, no image build: |
| 85 |
|
| 86 |
```shell |
| 87 |
make server-replace V=main # nats-server main |
| 88 |
make server-replace V=v2.13.0 # a specific tag |
| 89 |
make server-replace V=/path/to/your/nats-server # a local checkout |
| 90 |
make test # run against it |
| 91 |
make server-replace-drop # undo |
| 92 |
``` |
| 93 |
|
| 94 |
Verify what you are actually running: |
| 95 |
|
| 96 |
```shell |
| 97 |
go list -m -modfile=go_test.mod github.com/nats-io/nats-server/v2 |
| 98 |
``` |
| 99 |
|
| 100 |
**Do not use `go get` for this.** A branch pseudo-version is derived from the |
| 101 |
last reachable tag, so `@main` can sort *below* the release `orbit.go/ntf` |
| 102 |
requires. `go get` resolves that conflict by removing ntf from the module graph, |
| 103 |
which silently leaves you with no in-process tester: |
| 104 |
|
| 105 |
```text |
| 106 |
go: downgraded github.com/nats-io/nats-server/v2 => v2.14.1-0.2026... |
| 107 |
go: removed github.com/synadia-io/orbit.go/ntf |
| 108 |
``` |
| 109 |
|
| 110 |
A `replace` directive overrides the version outright and has no such failure |
| 111 |
mode, which is what `make server-replace` uses. |
| 112 |
|
| 113 |
`go_test.mod` is a tracked file, so the replace must not be committed. Note that |
| 114 |
`make server-replace-drop` removes the directive but does **not** fully restore |
| 115 |
the file: while the replacement was active, `go mod tidy` recorded the replaced |
| 116 |
module's own newer dependencies, and MVS never downgrades them again. The target |
| 117 |
warns when this happens; for an exact restore use |
| 118 |
`git checkout -- go_test.mod go_test.sum`. |
| 119 |
|
| 120 |
The docker path can also test a different server, but only versions Synadia |
| 121 |
publishes as images — this is what the nightly `latest-server.yaml` workflow |
| 122 |
uses: |
| 123 |
|
| 124 |
```shell |
| 125 |
make tester-up-host TESTER_IMAGE=synadia/ntf-server:nightly-latest-nats-main |
| 126 |
make test-docker |
| 127 |
``` |
| 128 |
|
| 129 |
## Host-side mode (docker, iterating on individual tests) |
| 130 |
|
| 131 |
`make tester-up-host` starts the tester with its ports published, so `go test` |
| 132 |
run from your terminal can reach the spawned servers via localhost. |
| 133 |
|
| 134 |
```shell |
| 135 |
make tester-up-host |
| 136 |
make test-docker T=TestSubSubject PKG=./test/... # one test, verbose |
| 137 |
make test-docker PKG=./jetstream/test/... # one package |
| 138 |
make test-docker # everything |
| 139 |
``` |
| 140 |
|
| 141 |
Use the `test-docker` targets, not `test` — plain `make test` ignores the |
| 142 |
container and uses the in-process tester. |
| 143 |
|
| 144 |
`make test-docker` wraps the full invocation, which is equivalent to: |
| 145 |
|
| 146 |
```shell |
| 147 |
TESTER_NATS_URL=nats://localhost:4222 \ |
| 148 |
go test -modfile=go_test.mod -tags=internal_testing -race -p=1 ./... --failfast -vet=off |
| 149 |
``` |
| 150 |
|
| 151 |
`-p=1` is required here: a shared container tester does not tolerate concurrent |
| 152 |
CreateServer calls from independent test binaries. The in-process tester is |
| 153 |
per-binary and could run packages in parallel — measurably faster — but `-p=1` |
| 154 |
is kept deliberately, since more concurrent server churn risks intermittent |
| 155 |
connection failures and the wall-clock saving is not worth chasing flakes. |
| 156 |
|
| 157 |
Known caveat: on macOS, docker-proxy races the tester's port handover, so in |
| 158 |
heavy suites roughly 5-10% of server creations can fail with |
| 159 |
`bind: address already in use`. Rerun the failing test, or use |
| 160 |
sibling-container mode for full-suite runs. |
| 161 |
|
| 162 |
## Sibling-container mode (full suite, matches CI) |
| 163 |
|
| 164 |
```shell |
| 165 |
make tester-up |
| 166 |
make test-tester |
| 167 |
make tester-down |
| 168 |
``` |
| 169 |
|
| 170 |
`make test-tester` runs the whole suite (NoRace pass, then the race-enabled |
| 171 |
pass) inside a Go container on the same docker network as the tester — no |
| 172 |
published ports, so the docker-proxy race above does not apply. This is the |
| 173 |
same shape CI uses, with the tester attached as a service container |
| 174 |
(`.github/workflows/ci.yaml`). |
| 175 |
|
| 176 |
## NoRace tests |
| 177 |
|
| 178 |
Tests prefixed `TestNoRace` are guarded by `//go:build !race && |
| 179 |
!skip_no_race_tests` and must run with the race detector off: |
| 180 |
`make test-norace`. |
| 181 |
|
| 182 |
## Build tags |
| 183 |
|
| 184 |
- `internal_testing` — exposes internal test hooks from `testing_internal.go`; |
| 185 |
required by some tests in `./test`. The make targets set it. |
| 186 |
- `skip_no_race_tests` — excludes the NoRace tests from a non-race run (used |
| 187 |
by `scripts/cov.sh`). |
| 188 |
- `compat` — compatibility tests in `test/compat_test.go`, which connect to an |
| 189 |
external NATS server via `NATS_URL`. |
| 190 |
- `go1.23` — iterator-based tests in `test/nats_iter_test.go`. |
| 191 |
|
| 192 |
## Coverage |
| 193 |
|
| 194 |
```shell |
| 195 |
TESTER_NATS_URL=nats://localhost:4222 ./scripts/cov.sh |
| 196 |
``` |
| 197 |
|
| 198 |
Merges unit and integration coverage into `acc.out` and opens the HTML report |
| 199 |
(CI passes an argument to skip the browser). |
| 200 |
|
| 201 |
## Troubleshooting |
| 202 |
|
| 203 |
- `cannot reach the tester at ...` — `TESTER_NATS_URL` is set but nothing is |
| 204 |
answering there. Either start the container (`make tester-up-host`) or unset |
| 205 |
the variable to use the in-process tester. |
| 206 |
- `TESTER_NATS_URL must be set in CI` — running under GitHub Actions without |
| 207 |
`TESTER_NATS_URL`. This guard exists so CI cannot silently swap the pinned |
| 208 |
container server for the in-process one, which is a different version. Point |
| 209 |
`TESTER_NATS_URL` at a tester, or unset `GITHUB_ACTIONS`. |
| 210 |
- Tester container misbehaving — `docker logs -f nats-tester` shows server spawn |
| 211 |
and config errors; `docker restart nats-tester` restarts it while keeping |
| 212 |
those logs. `make tester-down` removes the container and discards them. |
| 213 |
- Updating test-only dependencies: `go mod tidy -modfile=go_test.mod` (never |
| 214 |
change the main `go.mod` for test dependencies). |