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
101 lines
2.6 KiB
Go
101 lines
2.6 KiB
Go
package alive
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
type said struct {
|
|
mu sync.Mutex
|
|
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 func() { s.mu.Unlock(); s.tried <- struct{}{} }()
|
|
if s.fail != nil {
|
|
return s.fail
|
|
}
|
|
s.subs, s.body = append(s.subs, subject), append(s.body, body)
|
|
return nil
|
|
}
|
|
|
|
func (s *said) count() int {
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
return len(s.subs)
|
|
}
|
|
|
|
// **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). 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 := 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 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)
|
|
}
|
|
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 := newSaid(errors.New("permissions violation"))
|
|
var mu sync.Mutex
|
|
var lines []string
|
|
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()
|
|
})
|
|
<-s.tried
|
|
for i := 0; i < 4; i++ {
|
|
ticks <- time.Time{}
|
|
<-s.tried
|
|
}
|
|
stop()
|
|
mu.Lock()
|
|
defer mu.Unlock()
|
|
if len(lines) != 1 {
|
|
t.Fatalf("logged %d lines for one failure", len(lines))
|
|
}
|
|
}
|