Files
mesh-tools/node-tools/internal/bus/state_pernode_test.go
T
jschoubben 56d20314ba 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
2026-11-23 20:11:13 +01:00

129 lines
4.3 KiB
Go

//go:build integration
package bus_test
import (
"errors"
"fmt"
"reflect"
"sort"
"strings"
"sync"
"testing"
"time"
"github.com/novox/mesh-tools/node-tools/internal/bus"
mt "github.com/novox/mesh-tools/node-tools/internal/meshtest"
)
// **A state its owner keeps one bucket per node is served to a bundle as one state** (novox/hq ADR 0593 §6: each node's
// engine writes its own `composed-<node>`, and the provisioner reads them all). Get goes to the bucket the key's first
// part names, Keys and Watch over every bucket of the prefix, a bucket created during the watch included, and a bucket
// that only starts like the prefix is never read.
func TestAPerNodeStateIsServedAcrossEveryNodesBucket(t *testing.T) {
mesh := mt.New(t)
a, b := mesh.Bucket(t, "composed-a"), mesh.Bucket(t, "composed-b")
other := mesh.Bucket(t, "composedx-z")
for kv, key := range map[interface {
Put(string, []byte) (uint64, error)
}]string{a: "a.m1.f1", b: "b.m2.f2", other: "z.m3.f3"} {
if _, err := kv.Put(key, []byte(`{"v":1}`)); err != nil {
t.Fatal(err)
}
}
prov := mt.MembershipOf("mesh-provisioner", "anchor", false, nil)
prov.State = []bus.StateIssued{{Name: "mesh-host.composed", Bucket: "composed-", PerNode: true}}
mesh.Issue(t, prov)
conn, err := bus.Connect(bus.Credential{URL: mt.URL(t), Module: "node-tools", Node: "anchor"})
if err != nil {
t.Fatal(err)
}
conn.Logf = func(string, ...any) {}
t.Cleanup(conn.Close)
conn.Follow("mesh-provisioner")
mt.Until(t, func() error {
if conn.Membership("mesh-provisioner") == nil {
return errors.New("not issued yet")
}
return nil
})
if e, err := conn.StateGet("mesh-provisioner", "mesh-host.composed", "b.m2.f2"); err != nil || e == nil || e.Key != "b.m2.f2" || e.At.IsZero() || time.Since(e.At) > time.Minute {
t.Fatalf("get from the second node's bucket: %+v, %v", e, err)
}
if e, err := conn.StateGet("mesh-provisioner", "mesh-host.composed", "q.m.f"); err != nil || e != nil {
t.Fatalf("a key of a node with no bucket should read as none: %+v, %v", e, err)
}
if e, err := conn.StateGet("mesh-provisioner", "mesh-host.composed", "z.m3.f3"); err != nil || e != nil {
t.Fatalf("the look-alike bucket was read: %+v, %v", e, err)
}
if _, err := conn.StateGet("mesh-provisioner", "mesh-host.composed", "nonode"); err == nil {
t.Fatal("a key naming no node should be refused")
}
if keys, err := conn.StateKeys("mesh-provisioner", "mesh-host.composed"); err != nil || !reflect.DeepEqual(keys, []string{"a.m1.f1", "b.m2.f2"}) {
t.Fatalf("keys across the buckets: %v, %v", keys, err)
}
if _, err := conn.StatePut("mesh-provisioner", "mesh-host.composed", "a.m1.f9", []byte(`{}`)); err == nil {
t.Fatal("a per-node state is read only")
}
var mu sync.Mutex
var current, live []string
heard := make(chan struct{}, 1)
stop, err := conn.StateWatch("mesh-provisioner", "mesh-host.composed", "", func(ch bus.StateChange) error {
mu.Lock()
defer mu.Unlock()
if ch.Current {
current = append(current, ch.Key)
} else {
live = append(live, ch.Key)
}
select {
case heard <- struct{}{}:
default:
}
return nil
})
if err != nil {
t.Fatal(err)
}
t.Cleanup(stop)
mu.Lock()
sort.Strings(current)
if !reflect.DeepEqual(current, []string{"a.m1.f1", "b.m2.f2"}) {
t.Fatalf("current values: %v", current)
}
mu.Unlock()
// A third node's bucket, created during the watch, and a change in a first one.
c := mesh.Bucket(t, "composed-c")
if _, err := c.Put("c.m4.f4", []byte(`{"v":1}`)); err != nil {
t.Fatal(err)
}
if _, err := a.Put("a.m1.f5", []byte(`{"v":1}`)); err != nil {
t.Fatal(err)
}
// Waited for as the watch hands each change over, not polled within a fixed five seconds: the new bucket is taken
// in on the bus's creation advisory, which a loaded machine delivers later (design 47 H, H-A-363). The bound only
// ends a watch that never hears it.
missing := func() error {
mu.Lock()
defer mu.Unlock()
got := strings.Join(append(append([]string(nil), current...), live...), ",")
for _, want := range []string{"a.m1.f5", "c.m4.f4"} {
if !strings.Contains(got, want) {
return fmt.Errorf("%s not heard yet: current %v, changes %v", want, current, live)
}
}
return nil
}
bound := time.After(mt.Bound(t))
for err := missing(); err != nil; err = missing() {
select {
case <-heard:
case <-bound:
t.Fatal(err)
}
}
}