Files
mesh-sdk/go/testbus/testbus_test.go
T
jschoubben 139626daab S9 setup bound: a starved setup on the bus says so in one line
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
2026-11-21 23:57:12 +01:00

52 lines
1.4 KiB
Go

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)
}
}