H1 integration bound: a reload's stand-in serves no repairs; stop waits its Say

Review round 1. A ledger binding heard while a module was loaded again
made refreshRepairs serve the repairs on the reload's stand-in, whose
subscriptions nobody stopped: the repairs were served twice, and still
answered after the runtime stopped. The stand-in is now marked as
reloading and the refresh leaves it alone; the reload serves the module's
repairs as its binding is when the load ends. The reload test holds a
bundle's start, issues the binding inside the reload, and asks a repair
after the runtime stopped: without the change it still answered (6 of 6
runs, GOMAXPROCS=1 and 24).

No test failed without keep's stop waiting for the heartbeat's goroutine.
The heartbeat test now holds one Say and stops meanwhile: stop must not
return before that Say has; without the wait it failed 50 of 50 runs at
GOMAXPROCS=1 and at 24.

Builds: design 47 H §774, H-A-160; principle 11
This commit is contained in:
2026-11-20 17:45:50 +01:00
parent 05deea5952
commit 8ee4fdfb8e
3 changed files with 100 additions and 5 deletions
+41 -1
View File
@@ -3,7 +3,9 @@ package alive
import (
"encoding/json"
"errors"
"runtime"
"sync"
"sync/atomic"
"testing"
"time"
)
@@ -15,13 +17,27 @@ type said struct {
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{}
// held, when set, keeps each Say from returning until it is closed; inSay says one is held there now.
held chan struct{}
inSay atomic.Bool
}
func newSaid(fail error) *said { return &said{fail: fail, tried: make(chan struct{}, 16)} }
func (s *said) Say(subject string, body []byte) error {
if s.held != nil {
s.inSay.Store(true)
s.tried <- struct{}{}
<-s.held
defer s.inSay.Store(false)
}
s.mu.Lock()
defer func() { s.mu.Unlock(); s.tried <- struct{}{} }()
defer func() {
s.mu.Unlock()
if s.held == nil {
s.tried <- struct{}{}
}
}()
if s.fail != nil {
return s.fail
}
@@ -65,6 +81,30 @@ func TestTheRuntimeSaysItIsThereEveryInterval(t *testing.T) {
if s.count() != 3 {
t.Fatal("said after it was stopped")
}
// Stopped while a beat is being said, stop returns only once that Say has returned: nothing is said after it.
slow := newSaid(nil)
slow.held = make(chan struct{})
stopSlow := keep(slow, "anchor", 20*time.Millisecond, make(chan time.Time), t.Logf)
<-slow.tried
returned := make(chan bool)
go func() {
stopSlow()
returned <- slow.inSay.Load()
}()
// The stop is given its turn before the Say is let go (no clock: a returned stop is seen at once).
for i := 0; i < 1000; i++ {
select {
case stillSaying := <-returned:
close(slow.held)
t.Fatalf("stop returned while a beat was still being said (held: %v)", stillSaying)
default:
runtime.Gosched()
}
}
close(slow.held)
if stillSaying := <-returned; stillSaying {
t.Fatal("stop returned while a beat was still being said")
}
// Keep itself keeps the interval on a ticker of its own.
kept := newSaid(nil)
stopKept := Keep(kept, "anchor", time.Hour, t.Logf)