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
This commit is contained in:
+34
-2
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user