Skip to content
Snippets Groups Projects
Commit 99528327 authored by Jono Wenger's avatar Jono Wenger
Browse files

Reformat comments and code

parent 9584c95b
Branches
Tags
1 merge request!23Release
......@@ -5,7 +5,8 @@
// LICENSE file //
///////////////////////////////////////////////////////////////////////////////
// Contains functionality related to the event model driven network health tracker
// Contains functionality related to the event model driven network health
// tracker.
package health
......@@ -30,63 +31,68 @@ type Tracker struct {
// Determines the current health status
isHealthy bool
// Denotes the past health status
// wasHealthy is true if isHealthy has ever been true
// Denotes that the past health status wasHealthy is true if isHealthy has
// ever been true
wasHealthy bool
mux sync.RWMutex
}
// Creates a single HealthTracker thread, starts it, and returns a tracker and a stoppable
// Init creates a single HealthTracker thread, starts it, and returns a tracker
// and a stoppable.
func Init(instance *network.Instance, timeout time.Duration) *Tracker {
tracker := newTracker(timeout)
instance.SetNetworkHealthChan(tracker.heartbeat)
return tracker
}
// Builds and returns a new Tracker object given a Context
// newTracker builds and returns a new Tracker object given a Context.
func newTracker(timeout time.Duration) *Tracker {
return &Tracker{
timeout: timeout,
channels: make([]chan bool, 0),
channels: []chan bool{},
heartbeat: make(chan network.Heartbeat, 100),
isHealthy: false,
running: false,
}
}
// Add a channel to the list of Tracker channels
// such that each channel can be notified of network changes
// AddChannel adds a channel to the list of Tracker channels such that each
// channel can be notified of network changes.
func (t *Tracker) AddChannel(c chan bool) {
t.mux.Lock()
t.channels = append(t.channels, c)
t.mux.Unlock()
select {
case c <- t.IsHealthy():
default:
}
}
// Add a function to the list of Tracker function
// such that each function can be run after network changes
// AddFunc adds a function to the list of Tracker functions such that each
// function can be run after network changes.
func (t *Tracker) AddFunc(f func(isHealthy bool)) {
t.mux.Lock()
t.funcs = append(t.funcs, f)
t.mux.Unlock()
go f(t.IsHealthy())
}
func (t *Tracker) IsHealthy() bool {
t.mux.RLock()
defer t.mux.RUnlock()
return t.isHealthy
}
// Returns true if isHealthy has ever been true
// WasHealthy returns true if isHealthy has ever been true.
func (t *Tracker) WasHealthy() bool {
t.mux.RLock()
defer t.mux.RUnlock()
return t.wasHealthy
}
......@@ -94,10 +100,11 @@ func (t *Tracker) setHealth(h bool) {
t.mux.Lock()
// Only set wasHealthy to true if either
// wasHealthy is true or
// wasHealthy false but h value is true
// wasHealthy is false but h value is true
t.wasHealthy = t.wasHealthy || h
t.isHealthy = h
t.mux.Unlock()
t.transmit(h)
}
......@@ -119,7 +126,8 @@ func (t *Tracker) Start() (stoppable.Stoppable, error) {
return stop, nil
}
// Long-running thread used to monitor and report on network health
// start starts a long-running thread used to monitor and report on network
// health.
func (t *Tracker) start(stop *stoppable.Single) {
timer := time.NewTimer(t.timeout)
......@@ -131,8 +139,10 @@ func (t *Tracker) start(stop *stoppable.Single) {
t.isHealthy = false
t.running = false
t.mux.Unlock()
t.transmit(false)
stop.ToStopped()
return
case heartbeat = <-t.heartbeat:
if healthy(heartbeat) {
......
......@@ -9,12 +9,11 @@ package health
import (
"gitlab.com/elixxir/comms/network"
// "gitlab.com/elixxir/comms/network"
"testing"
"time"
)
// Happy path smoke test
// Happy path smoke test.
func TestNewTracker(t *testing.T) {
// Initialize required variables
timeout := 250 * time.Millisecond
......@@ -49,8 +48,7 @@ func TestNewTracker(t *testing.T) {
// Begin the health tracker
_, err := tracker.Start()
if err != nil {
t.Errorf("Unable to start tracker: %+v", err)
return
t.Fatalf("Unable to start tracker: %+v", err)
}
// Send a positive health heartbeat
......@@ -68,14 +66,12 @@ func TestNewTracker(t *testing.T) {
// Verify the network was marked as healthy
if !tracker.IsHealthy() {
t.Errorf("Tracker did not become healthy")
return
t.Fatal("Tracker did not become healthy.")
}
// Check if the tracker was ever healthy
if !tracker.WasHealthy() {
t.Errorf("Tracker did not become healthy")
return
t.Fatal("Tracker did not become healthy.")
}
// Verify the heartbeat triggered the listening chan/func
......@@ -89,15 +85,12 @@ func TestNewTracker(t *testing.T) {
// Verify the network was marked as NOT healthy
if tracker.IsHealthy() {
t.Errorf("Tracker should not report healthy")
return
t.Fatal("Tracker should not report healthy.")
}
// Check if the tracker was ever healthy,
// after setting healthy to false
// Check if the tracker was ever healthy, after setting healthy to false
if !tracker.WasHealthy() {
t.Errorf("Tracker was healthy previously but not reported healthy")
return
t.Fatal("Tracker was healthy previously but not reported healthy.")
}
// Verify the timeout triggered the listening chan/func
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment