node-tools: every wait on the bus in a test waits the shared bound, not a few seconds (issue 1754)
TestAStaleWriteIsRefusedAndWritesNothing gave the watch 5 s to hear a 1 s key's expire and failed main.505's integration step on a loaded build node; TestARunnerAtItsStopAnswersNoCallAndItsCallersMeetNoResponders failed its rerun while it asked 1 s of two round trips and 5 s and 10 s of its waits. The second of a kind after3850eb8, so the kind is fixed: meshtest.Bound (30 s, Until's bound, named once) is what every node-tools test that starts a bus waits for what the bus carries; Until becomes a deadline of it, so a try that waits does not multiply it; an upper bound on how long a call or a wait took is half the bound it tells apart (bus.RequestTimeout, the call's or the wait's own), at most meshtest.Bound. A wait that hears ends then: no passing test is longer (per-test durations before and after, equal). A guard (meshtest waits_test.go) reads every test file that imports meshtest and refuses a wait whose constant bound is a second or more under meshtest.Bound; it names 34 such waits at309069de. Builds: issue 1754 note 8 (the quick fix), note 9 (1754/AC1 to AC4); principle 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(30 * time.Second) {
|
||||
if !s.ReadyForConnections(Bound) {
|
||||
s.Shutdown()
|
||||
t.Fatal("a bus for this test did not come up within 30s")
|
||||
t.Fatalf("a bus for this test did not come up within %s", Bound)
|
||||
}
|
||||
url := s.ClientURL()
|
||||
buses.Store(t, url)
|
||||
@@ -260,17 +260,26 @@ func Fixture(name string) string {
|
||||
return filepath.Join(filepath.Dir(here), "..", "..", "test", "fixtures", name)
|
||||
}
|
||||
|
||||
// Until retries while the bus answers "no responders" — something not yet served. Its bound only ends a wait that never
|
||||
// hears what it waits for: 30 s, which a loaded machine stays within, where the earlier 5 s did not (design 47 H,
|
||||
// H-A-363; fix-a-bug-met-twice: a fixed bound a loaded machine overruns, fixed for every package that waits here).
|
||||
// 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
|
||||
|
||||
// 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).
|
||||
func Until(t *testing.T, try func() error) {
|
||||
t.Helper()
|
||||
var err error
|
||||
for i := 0; i < 300; i++ {
|
||||
for deadline := time.Now().Add(Bound); ; time.Sleep(100 * time.Millisecond) {
|
||||
if err = try(); err == nil {
|
||||
return
|
||||
}
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
if time.Now().After(deadline) {
|
||||
break
|
||||
}
|
||||
}
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -381,7 +390,7 @@ func (m *Mesh) BucketKeyTTL(t *testing.T, name string) {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
ctx, cancel := context.WithTimeout(context.Background(), Bound)
|
||||
defer cancel()
|
||||
if _, err := js.CreateKeyValue(ctx, jetstream.KeyValueConfig{Bucket: name, History: 1, MaxValueSize: 256 * 1024,
|
||||
LimitMarkerTTL: time.Minute}); err != nil {
|
||||
|
||||
@@ -0,0 +1,227 @@
|
||||
package meshtest
|
||||
|
||||
import (
|
||||
"go/ast"
|
||||
"go/parser"
|
||||
"go/token"
|
||||
"io/fs"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
// 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,
|
||||
"AskWithin": 3, "HearFirst": 2, "MaxWait": 0,
|
||||
}
|
||||
|
||||
// 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) {
|
||||
root := moduleRoot()
|
||||
var found []string
|
||||
err := filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if d.IsDir() {
|
||||
if path != root && (d.Name() == "node_modules" || strings.HasPrefix(d.Name(), ".")) {
|
||||
return filepath.SkipDir
|
||||
}
|
||||
return nil
|
||||
}
|
||||
dir := filepath.Dir(path)
|
||||
ours := filepath.Base(dir) == "meshtest" && filepath.Base(filepath.Dir(dir)) == "internal"
|
||||
if !strings.HasSuffix(path, ".go") || !ours && !strings.HasSuffix(path, "_test.go") {
|
||||
return nil
|
||||
}
|
||||
found = append(found, fixedWaits(t, path, ours)...)
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
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"))
|
||||
}
|
||||
}
|
||||
|
||||
// fixedWaits are the waits of one file that starts a bus whose bound is a constant of a second or more under Bound.
|
||||
func fixedWaits(t *testing.T, path string, ours bool) []string {
|
||||
t.Helper()
|
||||
fset := token.NewFileSet()
|
||||
f, err := parser.ParseFile(fset, path, nil, 0)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !ours && !importsMeshtest(f) {
|
||||
return nil
|
||||
}
|
||||
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 {
|
||||
rel, _ := filepath.Rel(moduleRoot(), fset.Position(bound.Pos()).Filename)
|
||||
found = append(found, rel+":"+strconv.Itoa(fset.Position(bound.Pos()).Line)+": "+what+" "+d.String())
|
||||
}
|
||||
}
|
||||
ast.Inspect(f, func(n ast.Node) bool {
|
||||
switch n := n.(type) {
|
||||
case *ast.CallExpr:
|
||||
name := ""
|
||||
switch fn := n.Fun.(type) {
|
||||
case *ast.SelectorExpr:
|
||||
name = fn.Sel.Name
|
||||
case *ast.Ident:
|
||||
name = fn.Name
|
||||
}
|
||||
if i, ok := waitBound[name]; ok && i < len(n.Args) {
|
||||
flag(name, n.Args[i])
|
||||
}
|
||||
case *ast.AssignStmt:
|
||||
// a deadline kept in a name, for a loop that polls until it passes
|
||||
for _, v := range n.Rhs {
|
||||
if bound, ok := nowAdd(v); ok {
|
||||
flag("a deadline", bound)
|
||||
}
|
||||
}
|
||||
case *ast.ValueSpec:
|
||||
for _, v := range n.Values {
|
||||
if bound, ok := nowAdd(v); ok {
|
||||
flag("a deadline", bound)
|
||||
}
|
||||
}
|
||||
}
|
||||
return true
|
||||
})
|
||||
return found
|
||||
}
|
||||
|
||||
func importsMeshtest(f *ast.File) bool {
|
||||
for _, i := range f.Imports {
|
||||
if strings.HasSuffix(strings.Trim(i.Path.Value, `"`), "/internal/meshtest") {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// nowAdd is d of time.Now().Add(d).
|
||||
func nowAdd(e ast.Expr) (ast.Expr, bool) {
|
||||
call, ok := e.(*ast.CallExpr)
|
||||
if !ok || len(call.Args) != 1 {
|
||||
return nil, false
|
||||
}
|
||||
sel, ok := call.Fun.(*ast.SelectorExpr)
|
||||
if !ok || sel.Sel.Name != "Add" {
|
||||
return nil, false
|
||||
}
|
||||
now, ok := sel.X.(*ast.CallExpr)
|
||||
if !ok {
|
||||
return nil, false
|
||||
}
|
||||
if s, ok := now.Fun.(*ast.SelectorExpr); ok && s.Sel.Name == "Now" && isIdent(s.X, "time") {
|
||||
return call.Args[0], true
|
||||
}
|
||||
return nil, false
|
||||
}
|
||||
|
||||
func isIdent(e ast.Expr, name string) bool {
|
||||
id, ok := e.(*ast.Ident)
|
||||
return ok && id.Name == name
|
||||
}
|
||||
|
||||
// packageConsts are the constants a package's files declare at their top, by name, unevaluated.
|
||||
func packageConsts(t *testing.T, dir string) map[string]ast.Expr {
|
||||
t.Helper()
|
||||
entries, err := os.ReadDir(dir)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
consts := map[string]ast.Expr{}
|
||||
for _, e := range entries {
|
||||
if !strings.HasSuffix(e.Name(), ".go") {
|
||||
continue
|
||||
}
|
||||
f, err := parser.ParseFile(token.NewFileSet(), filepath.Join(dir, e.Name()), nil, 0)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, d := range f.Decls {
|
||||
g, ok := d.(*ast.GenDecl)
|
||||
if !ok || g.Tok != token.CONST {
|
||||
continue
|
||||
}
|
||||
for _, s := range g.Specs {
|
||||
v := s.(*ast.ValueSpec)
|
||||
for i, n := range v.Names {
|
||||
if i < len(v.Values) {
|
||||
consts[n.Name] = v.Values[i]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return consts
|
||||
}
|
||||
|
||||
// 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.
|
||||
func duration(e ast.Expr, consts map[string]ast.Expr) (time.Duration, bool) {
|
||||
switch e := e.(type) {
|
||||
case *ast.ParenExpr:
|
||||
return duration(e.X, consts)
|
||||
case *ast.BasicLit:
|
||||
if e.Kind == token.INT {
|
||||
n, err := strconv.ParseInt(e.Value, 0, 64)
|
||||
return time.Duration(n), err == nil
|
||||
}
|
||||
if e.Kind == token.FLOAT {
|
||||
f, err := strconv.ParseFloat(e.Value, 64)
|
||||
return time.Duration(f), err == nil
|
||||
}
|
||||
case *ast.SelectorExpr:
|
||||
if isIdent(e.X, "time") {
|
||||
unit, ok := map[string]time.Duration{"Nanosecond": time.Nanosecond, "Microsecond": time.Microsecond,
|
||||
"Millisecond": time.Millisecond, "Second": time.Second, "Minute": time.Minute, "Hour": time.Hour}[e.Sel.Name]
|
||||
return unit, ok
|
||||
}
|
||||
case *ast.Ident:
|
||||
if v, ok := consts[e.Name]; ok && e.Name != "Bound" {
|
||||
delete(consts, e.Name) // a constant read once, so none refers to itself
|
||||
d, ok := duration(v, consts)
|
||||
consts[e.Name] = v
|
||||
return d, ok
|
||||
}
|
||||
case *ast.UnaryExpr:
|
||||
if d, ok := duration(e.X, consts); ok && e.Op == token.SUB {
|
||||
return -d, true
|
||||
}
|
||||
case *ast.BinaryExpr:
|
||||
a, okA := duration(e.X, consts)
|
||||
b, okB := duration(e.Y, consts)
|
||||
if !okA || !okB {
|
||||
return 0, false
|
||||
}
|
||||
switch e.Op {
|
||||
case token.MUL:
|
||||
return a * b, true
|
||||
case token.ADD:
|
||||
return a + b, true
|
||||
case token.SUB:
|
||||
return a - b, true
|
||||
case token.QUO:
|
||||
if b != 0 {
|
||||
return a / b, true
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0, false
|
||||
}
|
||||
Reference in New Issue
Block a user