stdio.SetupStarved holds "mesh: setup starved:" so the delivery SDK reads it without importing testbus; testbus.SetupStarved names it. Builds: design 47 H2 §800; H-A-368, H-A-399
111 lines
4.2 KiB
Go
111 lines
4.2 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. It is exported so a
|
|
// module's own integration tests start against the same (announcetest, novox/hq ADR 0495 clause 5;
|
|
// issue 1182 names the controller's copy).
|
|
//
|
|
// **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 (and go/vendor holds) 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 (
|
|
"fmt"
|
|
"os"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/nats-io/nats-server/v2/server"
|
|
|
|
stdio "git.novox.be/novox/mesh-sdk/go"
|
|
)
|
|
|
|
// 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 bound := SetupBound(t); !s.ReadyForConnections(bound) {
|
|
s.Shutdown()
|
|
Starved(t, fmt.Sprintf("a bus for this test did not come up within %s", bound))
|
|
}
|
|
t.Cleanup(func() {
|
|
s.Shutdown()
|
|
s.WaitForShutdown()
|
|
})
|
|
return s
|
|
}
|
|
|
|
// SetupStarved opens the one line a test prints when its setup wait on the bus passed the setup bound (design 47 H2
|
|
// §800, H-A-368): a build module reads it in the test output, never an assertion's words, and runs the level again.
|
|
const SetupStarved = stdio.SetupStarved
|
|
|
|
// levelBoundWord is the environment word a build step gives each test level its bound in, as a duration ("10m").
|
|
const levelBoundWord = "MESH_TEST_LEVEL_BOUND"
|
|
|
|
// unsetSetupBound is the setup bound of a run whose level has no bound (a developer's own run).
|
|
const unsetSetupBound = 30 * time.Second
|
|
|
|
// SetupBound is how long a test's setup may wait on the bus: half of the level's bound (MESH_TEST_LEVEL_BOUND), 30 s when
|
|
// it is unset, never a value in the test. A bound that is no duration fails the test, naming it.
|
|
func SetupBound(t testing.TB) time.Duration {
|
|
t.Helper()
|
|
word := os.Getenv(levelBoundWord)
|
|
if word == "" {
|
|
return unsetSetupBound
|
|
}
|
|
level, err := time.ParseDuration(word)
|
|
if err != nil || level <= 0 {
|
|
t.Fatalf("%s=%q is not a duration such as 10m", levelBoundWord, word)
|
|
}
|
|
return level / 2
|
|
}
|
|
|
|
// Starved fails the test with the line SetupStarved and its words: a setup wait on the bus passed SetupBound.
|
|
func Starved(t testing.TB, words string) {
|
|
t.Helper()
|
|
t.Fatalf("%s %s", SetupStarved, words)
|
|
}
|