H1 integration bound: the heartbeat and the counter tests hold under load

TestTheRuntimeSaysItIsThereEveryInterval slept 70 ms and wanted three
beats at 20 ms: a loaded machine that did not schedule the heartbeat in
time failed it (main.20's integration step on the build node); and its
stop returned before the heartbeat's goroutine ended, so a beat could be
said after it ("said after it was stopped"). The ticks are now the
caller's (keep, beside Keep's own ticker), the test drives them and waits
on each beat, and stop returns once nothing more is said. Pinned to one
processor at idle priority beside six busy loops, the old test failed 13
of 100 runs; the new passes 100 of 100.

TestTwoWritersIncrementingOneCounterOnTheBusLoseNoUpdate bounded a
writer's tries at 1000, a guess: on one processor the scheduler lets one
writer win every round and the other lost its 1000 tries every time. A
write is lost only to the other writer's write, so the bound is the
other's count plus one.

Builds: design 47 H §774; principle 11
This commit is contained in:
2026-11-20 17:23:59 +01:00
parent eb174d853f
commit 3850eb8eac
3 changed files with 59 additions and 20 deletions
+15 -5
View File
@@ -49,13 +49,20 @@ func Version() string {
// Keep says the runtime is there now and every interval until the returned function is called. A // Keep says the runtime is there now and every interval until the returned function is called. A
// heartbeat that cannot be said is logged when that starts and when it stops — not every minute. // heartbeat that cannot be said is logged when that starts and when it stops — not every minute.
func Keep(bus Sayer, node string, every time.Duration, logf func(string, ...any)) func() { func Keep(bus Sayer, node string, every time.Duration, logf func(string, ...any)) func() {
tick := time.NewTicker(every)
stop := keep(bus, node, every, tick.C, logf)
return func() { stop(); tick.Stop() }
}
// keep is Keep saying it again on every tick: the interval is told in the beat, the ticks are the caller's.
// Its stop returns once nothing more is said.
func keep(bus Sayer, node string, every time.Duration, ticks <-chan time.Time, logf func(string, ...any)) func() {
body, _ := json.Marshal(Beat{Node: node, IntervalSeconds: int(every / time.Second), Version: Version()}) body, _ := json.Marshal(Beat{Node: node, IntervalSeconds: int(every / time.Second), Version: Version()})
done := make(chan struct{}) done, ended := make(chan struct{}), make(chan struct{})
var once sync.Once var once sync.Once
go func() { go func() {
defer close(ended)
failing := "" failing := ""
tick := time.NewTicker(every)
defer tick.Stop()
for { for {
why := "" why := ""
if err := bus.Say(Subject(node), body); err != nil { if err := bus.Say(Subject(node), body); err != nil {
@@ -72,9 +79,12 @@ func Keep(bus Sayer, node string, every time.Duration, logf func(string, ...any)
select { select {
case <-done: case <-done:
return return
case <-tick.C: case <-ticks:
} }
} }
}() }()
return func() { once.Do(func() { close(done) }) } return func() {
once.Do(func() { close(done) })
<-ended
}
} }
+39 -13
View File
@@ -13,11 +13,15 @@ type said struct {
subs []string subs []string
body [][]byte body [][]byte
fail error fail error
// tried is told every Say, said or not, so a test waits on the heartbeat itself and never on the clock.
tried chan struct{}
} }
func newSaid(fail error) *said { return &said{fail: fail, tried: make(chan struct{}, 16)} }
func (s *said) Say(subject string, body []byte) error { func (s *said) Say(subject string, body []byte) error {
s.mu.Lock() s.mu.Lock()
defer s.mu.Unlock() defer func() { s.mu.Unlock(); s.tried <- struct{}{} }()
if s.fail != nil { if s.fail != nil {
return s.fail return s.fail
} }
@@ -32,39 +36,61 @@ func (s *said) count() int {
} }
// **The runtime says it is there at once and every interval, as its machine, with the interval**: the // **The runtime says it is there at once and every interval, as its machine, with the interval**: the
// controller's bound is three of them (to-be 45 S11). // controller's bound is three of them (to-be 45 S11). The ticks are the test's own, so a loaded machine
// cannot make it miss one (principle 11; design 47 H §774); stopped, it says nothing more.
func TestTheRuntimeSaysItIsThereEveryInterval(t *testing.T) { func TestTheRuntimeSaysItIsThereEveryInterval(t *testing.T) {
s := &said{} s := newSaid(nil)
stop := Keep(s, "anchor", 20*time.Millisecond, t.Logf) ticks := make(chan time.Time)
time.Sleep(70 * time.Millisecond) stop := keep(s, "anchor", 20*time.Millisecond, ticks, t.Logf)
<-s.tried
for i := 0; i < 2; i++ {
ticks <- time.Time{}
<-s.tried
}
stop() stop()
stop() stop()
if n := s.count(); n < 3 { if n := s.count(); n != 3 {
t.Fatalf("said %d times in three intervals", n) t.Fatalf("said %d times at once and on two ticks", n)
} }
var beat Beat var beat Beat
if err := json.Unmarshal(s.body[0], &beat); err != nil || beat.Node != "anchor" || beat.IntervalSeconds != 0 || if err := json.Unmarshal(s.body[0], &beat); err != nil || beat.Node != "anchor" || beat.IntervalSeconds != 0 ||
s.subs[0] != "mesh.control.anchor.tools-alive" { s.subs[0] != "mesh.control.anchor.tools-alive" {
t.Fatalf("%s on %s: %v", s.body[0], s.subs[0], err) t.Fatalf("%s on %s: %v", s.body[0], s.subs[0], err)
} }
n := s.count() select {
time.Sleep(50 * time.Millisecond) case ticks <- time.Time{}:
if s.count() != n { t.Fatal("a tick was taken after it was stopped")
default:
}
if s.count() != 3 {
t.Fatal("said after it was stopped") t.Fatal("said after it was stopped")
} }
// Keep itself keeps the interval on a ticker of its own.
kept := newSaid(nil)
stopKept := Keep(kept, "anchor", time.Hour, t.Logf)
<-kept.tried
stopKept()
if kept.count() != 1 {
t.Fatalf("Keep said %d times before its first interval", kept.count())
}
} }
// **A heartbeat the bus will not take is said once, and again when it is taken**: not a line a minute. // **A heartbeat the bus will not take is said once, and again when it is taken**: not a line a minute.
func TestAHeartbeatThatCannotBeSaidIsLoggedOnce(t *testing.T) { func TestAHeartbeatThatCannotBeSaidIsLoggedOnce(t *testing.T) {
s := &said{fail: errors.New("permissions violation")} s := newSaid(errors.New("permissions violation"))
var mu sync.Mutex var mu sync.Mutex
var lines []string var lines []string
stop := Keep(s, "anchor", 10*time.Millisecond, func(f string, a ...any) { ticks := make(chan time.Time)
stop := keep(s, "anchor", 10*time.Millisecond, ticks, func(f string, a ...any) {
mu.Lock() mu.Lock()
lines = append(lines, f) lines = append(lines, f)
mu.Unlock() mu.Unlock()
}) })
time.Sleep(60 * time.Millisecond) <-s.tried
for i := 0; i < 4; i++ {
ticks <- time.Time{}
<-s.tried
}
stop() stop()
mu.Lock() mu.Lock()
defer mu.Unlock() defer mu.Unlock()
@@ -78,7 +78,10 @@ func increment(b launch.Bus, key string, attempts int) (n uint64, lost int, err
// Two writers incrementing one counter a thousand times each, against a real bus, end at two thousand: // Two writers incrementing one counter a thousand times each, against a real bus, end at two thousand:
// every number handed out once and none lost, because the runtime writes only at the revision read // every number handed out once and none lost, because the runtime writes only at the revision read
// (novox/hq research 042: an issue's number is handed out once). // (novox/hq research 042: an issue's number is handed out once). A write is lost only to the other writer's
// write, so one increment loses at most as many times as the other writer increments: that, not a guess,
// is the bound on its tries, and a machine whose scheduler lets one writer win every round still passes
// (one processor starved a writer of a thousand tries; principle 11, design 47 H §774).
func TestTwoWritersIncrementingOneCounterOnTheBusLoseNoUpdate(t *testing.T) { func TestTwoWritersIncrementingOneCounterOnTheBusLoseNoUpdate(t *testing.T) {
b := counterBus(t) b := counterBus(t)
const each = 1000 const each = 1000
@@ -91,7 +94,7 @@ func TestTwoWritersIncrementingOneCounterOnTheBusLoseNoUpdate(t *testing.T) {
go func(w int) { go func(w int) {
defer wg.Done() defer wg.Done()
for i := 0; i < each; i++ { for i := 0; i < each; i++ {
n, l, err := increment(b, "issue", 1000) n, l, err := increment(b, "issue", each+1)
lost[w] += l lost[w] += l
if err != nil { if err != nil {
errs[w] = err errs[w] = err