From 139626daaba0ea7559b10cf5e629e3548f1d0232 Mon Sep 17 00:00:00 2001 From: jochen Date: Sat, 21 Nov 2026 23:57:12 +0100 Subject: [PATCH] S9 setup bound: a starved setup on the bus says so in one line MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A test's setup waits on the bus for half of its level's bound (MESH_TEST_LEVEL_BOUND), 30 s when unset, and fails with the line "mesh: setup starved:" a build module reads to run the level again. Builds: design 47 H2 §800; H-A-368 --- go/testbus/testbus.go | 36 +++++++++++++++++++++++++-- go/testbus/testbus_test.go | 51 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 85 insertions(+), 2 deletions(-) create mode 100644 go/testbus/testbus_test.go diff --git a/go/testbus/testbus.go b/go/testbus/testbus.go index d061ef82..a41b12e4 100644 --- a/go/testbus/testbus.go +++ b/go/testbus/testbus.go @@ -19,6 +19,7 @@ package testbus import ( + "fmt" "os" "sync" "testing" @@ -64,9 +65,9 @@ func Start(t testing.TB) *server.Server { t.Fatalf("a bus for this test could not be made: %v", err) } go s.Start() - if !s.ReadyForConnections(30 * time.Second) { + if bound := SetupBound(t); !s.ReadyForConnections(bound) { s.Shutdown() - t.Fatal("a bus for this test did not come up within 30s") + Starved(t, fmt.Sprintf("a bus for this test did not come up within %s", bound)) } t.Cleanup(func() { s.Shutdown() @@ -74,3 +75,34 @@ func Start(t testing.TB) *server.Server { }) 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 = "mesh: setup starved:" + +// 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) +} diff --git a/go/testbus/testbus_test.go b/go/testbus/testbus_test.go new file mode 100644 index 00000000..382d9a99 --- /dev/null +++ b/go/testbus/testbus_test.go @@ -0,0 +1,51 @@ +package testbus + +import ( + "fmt" + "testing" + "time" +) + +// failed is a testing.TB that keeps what a helper failed with, so the helper's own failure is read, not suffered. +type failed struct { + testing.TB + words string +} + +func (f *failed) Helper() {} +func (f *failed) Fatalf(format string, args ...any) { + f.words = fmt.Sprintf(format, args...) + panic(f) +} + +func catch(f *failed, run func()) { + defer func() { + if r := recover(); r != nil && r != f { + panic(r) + } + }() + run() +} + +// Design 47 H2 §800, H-A-368: the setup bound is half of the level's bound, 30 s when the level has none, and a starved +// setup fails with the one line a build module reads. +func TestTheSetupBoundIsHalfTheLevelsAndAStarvedSetupSaysSo(t *testing.T) { + for _, c := range []struct { + level string + want time.Duration + fails bool + }{{"", 30 * time.Second, false}, {"10m", 5 * time.Minute, false}, {"ten", 0, true}, {"-1s", 0, true}} { + t.Setenv(levelBoundWord, c.level) + f := &failed{TB: t} + var got time.Duration + catch(f, func() { got = SetupBound(f) }) + if c.fails != (f.words != "") || (!c.fails && got != c.want) { + t.Errorf("level %q: bound %s, failed %q", c.level, got, f.words) + } + } + f := &failed{TB: t} + catch(f, func() { Starved(f, "the stream did not answer within 5s") }) + if f.words != "mesh: setup starved: the stream did not answer within 5s" { + t.Errorf("a starved setup said %q", f.words) + } +}