diff --git a/node-tools/internal/alive/alive.go b/node-tools/internal/alive/alive.go index 843b57f..fc10987 100644 --- a/node-tools/internal/alive/alive.go +++ b/node-tools/internal/alive/alive.go @@ -49,13 +49,20 @@ func Version() string { // 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. 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()}) - done := make(chan struct{}) + done, ended := make(chan struct{}), make(chan struct{}) var once sync.Once go func() { + defer close(ended) failing := "" - tick := time.NewTicker(every) - defer tick.Stop() for { why := "" 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 { case <-done: return - case <-tick.C: + case <-ticks: } } }() - return func() { once.Do(func() { close(done) }) } + return func() { + once.Do(func() { close(done) }) + <-ended + } } diff --git a/node-tools/internal/alive/alive_test.go b/node-tools/internal/alive/alive_test.go index f7cd680..888605e 100644 --- a/node-tools/internal/alive/alive_test.go +++ b/node-tools/internal/alive/alive_test.go @@ -13,11 +13,15 @@ type said struct { subs []string body [][]byte 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 { s.mu.Lock() - defer s.mu.Unlock() + defer func() { s.mu.Unlock(); s.tried <- struct{}{} }() if s.fail != nil { 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 -// 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) { - s := &said{} - stop := Keep(s, "anchor", 20*time.Millisecond, t.Logf) - time.Sleep(70 * time.Millisecond) + s := newSaid(nil) + ticks := make(chan time.Time) + 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() - if n := s.count(); n < 3 { - t.Fatalf("said %d times in three intervals", n) + if n := s.count(); n != 3 { + t.Fatalf("said %d times at once and on two ticks", n) } var beat Beat if err := json.Unmarshal(s.body[0], &beat); err != nil || beat.Node != "anchor" || beat.IntervalSeconds != 0 || s.subs[0] != "mesh.control.anchor.tools-alive" { t.Fatalf("%s on %s: %v", s.body[0], s.subs[0], err) } - n := s.count() - time.Sleep(50 * time.Millisecond) - if s.count() != n { + select { + case ticks <- time.Time{}: + t.Fatal("a tick was taken after it was stopped") + default: + } + if s.count() != 3 { 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. func TestAHeartbeatThatCannotBeSaidIsLoggedOnce(t *testing.T) { - s := &said{fail: errors.New("permissions violation")} + s := newSaid(errors.New("permissions violation")) var mu sync.Mutex 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() lines = append(lines, f) mu.Unlock() }) - time.Sleep(60 * time.Millisecond) + <-s.tried + for i := 0; i < 4; i++ { + ticks <- time.Time{} + <-s.tried + } stop() mu.Lock() defer mu.Unlock() diff --git a/node-tools/internal/runtime/state_counter_test.go b/node-tools/internal/runtime/state_counter_test.go index 3cbbfd9..b8594eb 100644 --- a/node-tools/internal/runtime/state_counter_test.go +++ b/node-tools/internal/runtime/state_counter_test.go @@ -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: // 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) { b := counterBus(t) const each = 1000 @@ -91,7 +94,7 @@ func TestTwoWritersIncrementingOneCounterOnTheBusLoseNoUpdate(t *testing.T) { go func(w int) { defer wg.Done() for i := 0; i < each; i++ { - n, l, err := increment(b, "issue", 1000) + n, l, err := increment(b, "issue", each+1) lost[w] += l if err != nil { errs[w] = err