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:
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user