fix: P1-3 address heartbeat timer leak, test drain, and store consistency

- COR-001: drain runHeartbeat goroutine via done channel before test returns,
  eliminating race against concurrent SetHeartbeatInterval tests
- COR-002: replace time.After with time.NewTimer + defer t.Stop() + t.Reset()
  to prevent timer goroutine leaks on context cancellation
- COR-005: guard HeartbeatInterval store write with > 0 check in OnClusterUpdated,
  keeping store consistent with SetHeartbeatInterval's zero-ignore semantics
This commit is contained in:
Rongsong Shen 2026-04-17 14:20:02 +00:00
parent ffc5b20cdb
commit 0a3de8b040
3 changed files with 16 additions and 9 deletions

View file

@ -35,17 +35,18 @@ func TestRunHeartbeat_CallsFunction(t *testing.T) {
atomic.StoreInt32(&heartbeatIntervalSecs, 1)
var callCount int64
fn := func(_ int) {
atomic.AddInt64(&callCount, 1)
}
fn := func(_ int) { atomic.AddInt64(&callCount, 1) }
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
go runHeartbeat(ctx, fn)
done := make(chan struct{})
go func() {
runHeartbeat(ctx, fn)
close(done)
}()
time.Sleep(1500 * time.Millisecond)
cancel()
<-done // wait for goroutine to exit before the test returns
if got := atomic.LoadInt64(&callCount); got < 1 {
t.Errorf("fn was called %d times, want at least 1", got)