Files
mesh-sdk/go/internal/testbus/testbus.go
T
jschoubben 1c20b9fb9b
mesh/merge-gate checking again: Issue 1304 step 2. Rejected in review round 1 only because main's go/merge-check.sh still required go/vendor. Step 1, mesh-…
mesh/repo-check checking again: Issue 1304 step 2. Rejected in review round 1 only because main's go/merge-check.sh still required go/vendor. Step 1, mesh-…
mesh/delivery stopped: the pull request closed unmerged
Stop committing mesh-sdk's Go dependencies: resolve them by go.sum (issue 1304)
2026-10-19 21:57:52 +02:00

75 lines
2.7 KiB
Go

// Package testbus is the bus an integration test of the SDK's Go binding runs against: a server of
// its own, of the release go.mod pins, started for the one test and gone after it.
//
// **Why one per test.** A test that reaches a shared bus asserts, reads and removes objects by fixed
// name — a stream, a consumer — and two tests at once would step on each other's: one deleting what
// the other is reading, one leaving a stream behind for the next to trip on. A server per test makes
// every test start from nothing and leave nothing, so the suite is the same run in any order, at any
// parallelism, under the race detector, on a workstation or on the build seat.
//
// **No catalogue pin check.** mesh-controller's testbus also checks the server's release against the
// catalogue's bus image and the mesh's facts snapshot (novox/hq ADR 0227 rule 9) — the SDK has no
// catalogue of its own to check against, so that test is left out here. Only the version this
// module's go.mod pins is what these tests run.
//
// A person may still point a run at a bus of their own — another release, a lab's — with
// MESH_TEST_NATS_EXTERNAL=1 and MESH_TEST_NATS; then the run is theirs to serialise.
package testbus
import (
"os"
"sync"
"testing"
"time"
"github.com/nats-io/nats-server/v2/server"
)
// Version is the release of the server the tests run.
const Version = server.VERSION
// URL is the bus of this test alone: started at its first ask, the same one at every ask after — a
// test that dials twice, or hands the address to the code it tests, reaches one bus — and shut down
// when the test ends.
func URL(t testing.TB) string {
t.Helper()
if os.Getenv("MESH_TEST_NATS_EXTERNAL") == "1" {
if url := os.Getenv("MESH_TEST_NATS"); url != "" {
return url
}
t.Fatal("MESH_TEST_NATS_EXTERNAL=1 and MESH_TEST_NATS names no bus")
}
if url, ok := buses.Load(t); ok {
return url.(string)
}
url := Start(t).ClientURL()
buses.Store(t, url)
t.Cleanup(func() { buses.Delete(t) })
return url
}
// buses are the running tests' buses, by test.
var buses sync.Map
// Start is a server with JetStream, for this test alone: on loopback, on a port the system chooses, its
// store in the test's own directory.
func Start(t testing.TB) *server.Server {
t.Helper()
opts := &server.Options{Host: "127.0.0.1", Port: server.RANDOM_PORT, JetStream: true, StoreDir: t.TempDir(),
NoLog: true, NoSigs: true}
s, err := server.NewServer(opts)
if err != nil {
t.Fatalf("a bus for this test could not be made: %v", err)
}
go s.Start()
if !s.ReadyForConnections(30 * time.Second) {
s.Shutdown()
t.Fatal("a bus for this test did not come up within 30s")
}
t.Cleanup(func() {
s.Shutdown()
s.WaitForShutdown()
})
return s
}