Skip to content
Snippets Groups Projects
Commit 6df18a88 authored by Richard T. Carback III's avatar Richard T. Carback III
Browse files

Fix health tracker timer logic

parent c87fe956
No related branches found
No related tags found
No related merge requests found
......@@ -168,7 +168,7 @@ var rootCmd = &cobra.Command{
for !isConnected {
select {
case isConnected = <-connected:
jww.INFO.Printf("health status: %b\n",
jww.INFO.Printf("health status: %v\n",
isConnected)
break
case <-timeoutTick.C:
......
......@@ -113,9 +113,7 @@ func (t *Tracker) Start() (stoppable.Stoppable, error) {
// Long-running thread used to monitor and report on network health
func (t *Tracker) start(quitCh <-chan struct{}) {
var timerChan <-chan time.Time
timerChan = make(chan time.Time)
timer := time.NewTimer(t.timeout)
for {
var heartbeat network.Heartbeat
......@@ -124,13 +122,23 @@ func (t *Tracker) start(quitCh <-chan struct{}) {
// Handle thread kill
break
case heartbeat = <-t.heartbeat:
jww.INFO.Printf("heartbeat: %+v", heartbeat)
jww.INFO.Printf("heartbeat: %v", heartbeat)
// Stop and reset timer
if !timer.Stop() {
select {
case <-timer.C: // per docs explicitly drain
default:
}
}
timer.Reset(t.timeout)
if healthy(heartbeat) {
timerChan = time.NewTimer(t.timeout).C
t.setHealth(true)
}
case <-timerChan:
break
case <-timer.C:
t.setHealth(false)
timer.Reset(t.timeout)
break
}
}
}
......
......@@ -16,9 +16,9 @@ import (
// Happy path smoke test
func TestNewTracker(t *testing.T) {
// Initialize required variables
timeout := 500 * time.Millisecond
timeout := 250 * time.Millisecond
tracker := newTracker(timeout)
counter := 0
counter := 2 // First signal is "false/unhealthy"
positiveHb := network.Heartbeat{
HasWaitingRound: true,
IsRoundComplete: true,
......@@ -57,7 +57,7 @@ func TestNewTracker(t *testing.T) {
tracker.heartbeat <- positiveHb
// Wait for the heartbeat to register
for i := 0; i < 5; i++ {
for i := 0; i < 4; i++ {
if tracker.IsHealthy() && counter == expectedCount {
break
} else {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment