node-tools: a test's wait on the bus takes the test's own deadline (issue 1754, review round 1)
The lead's answer on issue 1754 (note of 2026-11-23 20:00, principles 11 and 7): no bound written into the code. meshtest.Bound(t) is t.Deadline() less 30 s for the test's cleanup, go test's default of 10 min where a run names no deadline, and what is left, at least 1 ms, once less than the margin is (the lead's round-2 answer, note 11; principles 3 and 7). Until stops at it; callWithin is gone, call() takes it. The guard now refuses any constant bound of a second or more. Each upper limit is half the bound it tells apart: the watch's refusal is held to half the default StateTimeout the test replaced (a watch timer fixed at 10 s failed it in 10.0 s, round 1 passed it); the console's close to half its backstop ShutdownWithin (a Close that never cancels failed it in 5.1 s, round 1 passed it); the console waits to half WaitDefault. Builds: issue 1754 notes 10 (lead answer, 20:00) and 11 (round 2: margin, AC1, AC4); principles 3, 7 and 11
This commit is contained in:
@@ -57,9 +57,9 @@ func URL(t *testing.T) string {
|
||||
t.Fatalf("a bus for this test could not be made: %v", err)
|
||||
}
|
||||
go s.Start()
|
||||
if !s.ReadyForConnections(Bound) {
|
||||
if !s.ReadyForConnections(Bound(t)) {
|
||||
s.Shutdown()
|
||||
t.Fatalf("a bus for this test did not come up within %s", Bound)
|
||||
t.Fatal("a bus for this test did not come up within its bound")
|
||||
}
|
||||
url := s.ClientURL()
|
||||
buses.Store(t, url)
|
||||
@@ -260,20 +260,46 @@ func Fixture(name string) string {
|
||||
return filepath.Join(filepath.Dir(here), "..", "..", "test", "fixtures", name)
|
||||
}
|
||||
|
||||
// Bound is how long a test waits for what the bus is to carry (an event, an answer, a watch's change, a key's expiry, a
|
||||
// bus coming up) before it fails: 30 s, which a loaded build node stays within, where a few seconds did not (issue 1754:
|
||||
// a 1 s key's expire not heard in 5 s on a loaded build node; fix-a-bug-met-twice). It only ends a wait that never hears
|
||||
// what it waits for: a wait that hears ends then, so a passing test is no longer for it. One bound for every such wait,
|
||||
// well under the integration step's own 30 minutes (principle 11), never a few seconds written into a test (1754/AC1).
|
||||
const Bound = 30 * time.Second
|
||||
// Bound is how long, from now, a test waits for what the bus is to carry (an event, an answer, a watch's change, a key's
|
||||
// expiry, a bus coming up) before it fails: the test's own deadline, go test's -timeout that the build step sets, less
|
||||
// cleanupMargin for the test's cleanup, never a few seconds written into a test (issue 1754: a 1 s key's expire not heard
|
||||
// in 5 s on a loaded build node; the lead's answers on the issue of 2026-11-23 20:00 and of round 2, principles 11 and 7;
|
||||
// 1754/AC1). It only ends a wait that never hears what it waits for: a wait that hears ends then, so a passing test is no
|
||||
// longer for it. With less than the margin left it answers what is left, at least a millisecond, so a cleanup's wait runs
|
||||
// to the deadline and go test's own timeout names the test (principle 3).
|
||||
func Bound(t *testing.T) time.Duration {
|
||||
deadline, ok := t.Deadline()
|
||||
if !ok {
|
||||
deadline = started.Add(goTestTimeout)
|
||||
}
|
||||
return boundOf(time.Until(deadline))
|
||||
}
|
||||
|
||||
// Until retries while the bus answers "no responders" — something not yet served — until Bound has passed: a deadline, so
|
||||
// a try that itself waits does not multiply it (fix-a-bug-met-twice: a fixed bound a loaded machine overruns, fixed for
|
||||
// every package that waits here).
|
||||
// boundOf is Bound for what is left until the deadline.
|
||||
func boundOf(left time.Duration) time.Duration {
|
||||
if left > cleanupMargin {
|
||||
return left - cleanupMargin
|
||||
}
|
||||
return max(left, time.Millisecond)
|
||||
}
|
||||
|
||||
// cleanupMargin is what Bound leaves of a test's deadline for its cleanup: 30 s, the longest bound these tests used
|
||||
// (Until's), which covers their cleanups (the lead's answer on issue 1754, round 2).
|
||||
const cleanupMargin = 30 * time.Second
|
||||
|
||||
// goTestTimeout is go test's own default -timeout, the deadline of a run that names none, from the run's start.
|
||||
const goTestTimeout = 10 * time.Minute
|
||||
|
||||
// started is when this test binary began.
|
||||
var started = time.Now()
|
||||
|
||||
// Until retries while the bus answers "no responders" — something not yet served — until the test's Bound has passed: a
|
||||
// deadline, so a try that itself waits does not multiply it (fix-a-bug-met-twice: a fixed bound a loaded machine overruns,
|
||||
// fixed for every package that waits here).
|
||||
func Until(t *testing.T, try func() error) {
|
||||
t.Helper()
|
||||
var err error
|
||||
for deadline := time.Now().Add(Bound); ; time.Sleep(100 * time.Millisecond) {
|
||||
for deadline := time.Now().Add(Bound(t)); ; time.Sleep(100 * time.Millisecond) {
|
||||
if err = try(); err == nil {
|
||||
return
|
||||
}
|
||||
@@ -390,7 +416,7 @@ func (m *Mesh) BucketKeyTTL(t *testing.T, name string) {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(context.Background(), Bound)
|
||||
ctx, cancel := context.WithTimeout(context.Background(), Bound(t))
|
||||
defer cancel()
|
||||
if _, err := js.CreateKeyValue(ctx, jetstream.KeyValueConfig{Bucket: name, History: 1, MaxValueSize: 256 * 1024,
|
||||
LimitMarkerTTL: time.Minute}); err != nil {
|
||||
|
||||
@@ -13,6 +13,31 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
// 1754/AC1: a wait's bound is the test's deadline less the cleanup margin, and what is left once less than the margin is,
|
||||
// never zero or less (the lead's answer on issue 1754, round 2).
|
||||
func TestABoundIsTheTestsDeadlineLessItsCleanupMargin(t *testing.T) {
|
||||
for _, row := range []struct{ left, want time.Duration }{
|
||||
{10 * time.Minute, 10*time.Minute - cleanupMargin},
|
||||
{cleanupMargin + time.Second, time.Second},
|
||||
{cleanupMargin, cleanupMargin},
|
||||
{time.Second, time.Second},
|
||||
{0, time.Millisecond},
|
||||
{-time.Minute, time.Millisecond},
|
||||
} {
|
||||
if got := boundOf(row.left); got != row.want {
|
||||
t.Errorf("with %s left the bound is %s, want %s", row.left, got, row.want)
|
||||
}
|
||||
}
|
||||
deadline, ok := t.Deadline()
|
||||
if !ok {
|
||||
deadline = started.Add(goTestTimeout)
|
||||
}
|
||||
left := time.Until(deadline)
|
||||
if b := Bound(t); b <= 0 || b > left-cleanupMargin {
|
||||
t.Errorf("this test's bound is %s, with %s left until its deadline", b, left)
|
||||
}
|
||||
}
|
||||
|
||||
// waitBound is, for each call that waits, which argument is its bound.
|
||||
var waitBound = map[string]int{
|
||||
"After": 0, "NewTimer": 0, "WithTimeout": 1, "NextMsg": 0, "ReadyForConnections": 0, "Request": 2, "RequestMsg": 1,
|
||||
@@ -20,10 +45,10 @@ var waitBound = map[string]int{
|
||||
}
|
||||
|
||||
// 1754/AC1: no test that starts a bus (a test file importing this package, and this package itself) waits for what the
|
||||
// bus carries with a fixed bound of a second or more under Bound: each such wait names Bound. A few seconds is what a
|
||||
// loaded build node overran (issue 1754: state_counter_test.go's 5 s for a 1 s key's expire, failed on a build node);
|
||||
// a bound under a second is the bound under test or the window an absence is given, and is not this guard's.
|
||||
func TestNoWaitOnTheBusHasAFixedBoundUnderTheSharedOne(t *testing.T) {
|
||||
// bus carries with a fixed bound of a second or more: each such wait takes the test's own deadline, Bound(t). A few
|
||||
// seconds is what a loaded build node overran (issue 1754: state_counter_test.go's 5 s for a 1 s key's expire, failed on
|
||||
// a build node); a bound under a second is the bound under test or the window an absence is given, and is not this guard's.
|
||||
func TestNoWaitOnTheBusHasAFixedBound(t *testing.T) {
|
||||
root := moduleRoot()
|
||||
var found []string
|
||||
err := filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error {
|
||||
@@ -48,12 +73,12 @@ func TestNoWaitOnTheBusHasAFixedBoundUnderTheSharedOne(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(found) > 0 {
|
||||
t.Errorf("waits on the bus with a fixed bound under meshtest.Bound (%s), which a loaded build node overruns: give each meshtest.Bound\n%s",
|
||||
Bound, strings.Join(found, "\n"))
|
||||
t.Errorf("waits on the bus with a fixed bound, which a loaded build node overruns: give each the test's own, meshtest.Bound(t)\n%s",
|
||||
strings.Join(found, "\n"))
|
||||
}
|
||||
}
|
||||
|
||||
// fixedWaits are the waits of one file that starts a bus whose bound is a constant of a second or more under Bound.
|
||||
// fixedWaits are the waits of one file that starts a bus whose bound is a constant of a second or more.
|
||||
func fixedWaits(t *testing.T, path string, ours bool) []string {
|
||||
t.Helper()
|
||||
fset := token.NewFileSet()
|
||||
@@ -67,7 +92,7 @@ func fixedWaits(t *testing.T, path string, ours bool) []string {
|
||||
consts := packageConsts(t, filepath.Dir(path))
|
||||
var found []string
|
||||
flag := func(what string, bound ast.Expr) {
|
||||
if d, ok := duration(bound, consts); ok && d >= time.Second && d < Bound {
|
||||
if d, ok := duration(bound, consts); ok && d >= time.Second {
|
||||
rel, _ := filepath.Rel(moduleRoot(), fset.Position(bound.Pos()).Filename)
|
||||
found = append(found, rel+":"+strconv.Itoa(fset.Position(bound.Pos()).Line)+": "+what+" "+d.String())
|
||||
}
|
||||
@@ -173,7 +198,7 @@ func packageConsts(t *testing.T, dir string) map[string]ast.Expr {
|
||||
}
|
||||
|
||||
// duration is the value of a constant duration written as the tests write one: a number, time's units, a constant of
|
||||
// the package, and their products and sums. Anything else (a variable, Bound, another package's bound) is not constant.
|
||||
// the package, and their products and sums. Anything else (a variable, Bound(t), another package's bound) is not constant.
|
||||
func duration(e ast.Expr, consts map[string]ast.Expr) (time.Duration, bool) {
|
||||
switch e := e.(type) {
|
||||
case *ast.ParenExpr:
|
||||
@@ -194,7 +219,7 @@ func duration(e ast.Expr, consts map[string]ast.Expr) (time.Duration, bool) {
|
||||
return unit, ok
|
||||
}
|
||||
case *ast.Ident:
|
||||
if v, ok := consts[e.Name]; ok && e.Name != "Bound" {
|
||||
if v, ok := consts[e.Name]; ok {
|
||||
delete(consts, e.Name) // a constant read once, so none refers to itself
|
||||
d, ok := duration(v, consts)
|
||||
consts[e.Name] = v
|
||||
|
||||
Reference in New Issue
Block a user