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 @@ ...@@ -5,7 +5,8 @@
// LICENSE file // // 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 package health
...@@ -30,63 +31,68 @@ type Tracker struct { ...@@ -30,63 +31,68 @@ type Tracker struct {
// Determines the current health status // Determines the current health status
isHealthy bool 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 wasHealthy bool
mux sync.RWMutex 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 { func Init(instance *network.Instance, timeout time.Duration) *Tracker {
tracker := newTracker(timeout) tracker := newTracker(timeout)
instance.SetNetworkHealthChan(tracker.heartbeat) instance.SetNetworkHealthChan(tracker.heartbeat)
return tracker 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 { func newTracker(timeout time.Duration) *Tracker {
return &Tracker{ return &Tracker{
timeout: timeout, timeout: timeout,
channels: make([]chan bool, 0), channels: []chan bool{},
heartbeat: make(chan network.Heartbeat, 100), heartbeat: make(chan network.Heartbeat, 100),
isHealthy: false, isHealthy: false,
running: false, running: false,
} }
} }
// Add a channel to the list of Tracker channels // AddChannel adds a channel to the list of Tracker channels such that each
// such that each channel can be notified of network changes // channel can be notified of network changes.
func (t *Tracker) AddChannel(c chan bool) { func (t *Tracker) AddChannel(c chan bool) {
t.mux.Lock() t.mux.Lock()
t.channels = append(t.channels, c) t.channels = append(t.channels, c)
t.mux.Unlock() t.mux.Unlock()
select { select {
case c <- t.IsHealthy(): case c <- t.IsHealthy():
default: default:
} }
} }
// Add a function to the list of Tracker function // AddFunc adds a function to the list of Tracker functions such that each
// such that each function can be run after network changes // function can be run after network changes.
func (t *Tracker) AddFunc(f func(isHealthy bool)) { func (t *Tracker) AddFunc(f func(isHealthy bool)) {
t.mux.Lock() t.mux.Lock()
t.funcs = append(t.funcs, f) t.funcs = append(t.funcs, f)
t.mux.Unlock() t.mux.Unlock()
go f(t.IsHealthy()) go f(t.IsHealthy())
} }
func (t *Tracker) IsHealthy() bool { func (t *Tracker) IsHealthy() bool {
t.mux.RLock() t.mux.RLock()
defer t.mux.RUnlock() defer t.mux.RUnlock()
return t.isHealthy 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 { func (t *Tracker) WasHealthy() bool {
t.mux.RLock() t.mux.RLock()
defer t.mux.RUnlock() defer t.mux.RUnlock()
return t.wasHealthy return t.wasHealthy
} }
...@@ -94,10 +100,11 @@ func (t *Tracker) setHealth(h bool) { ...@@ -94,10 +100,11 @@ func (t *Tracker) setHealth(h bool) {
t.mux.Lock() t.mux.Lock()
// Only set wasHealthy to true if either // Only set wasHealthy to true if either
// wasHealthy is true or // 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.wasHealthy = t.wasHealthy || h
t.isHealthy = h t.isHealthy = h
t.mux.Unlock() t.mux.Unlock()
t.transmit(h) t.transmit(h)
} }
...@@ -119,7 +126,8 @@ func (t *Tracker) Start() (stoppable.Stoppable, error) { ...@@ -119,7 +126,8 @@ func (t *Tracker) Start() (stoppable.Stoppable, error) {
return stop, nil 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) { func (t *Tracker) start(stop *stoppable.Single) {
timer := time.NewTimer(t.timeout) timer := time.NewTimer(t.timeout)
...@@ -131,8 +139,10 @@ func (t *Tracker) start(stop *stoppable.Single) { ...@@ -131,8 +139,10 @@ func (t *Tracker) start(stop *stoppable.Single) {
t.isHealthy = false t.isHealthy = false
t.running = false t.running = false
t.mux.Unlock() t.mux.Unlock()
t.transmit(false) t.transmit(false)
stop.ToStopped() stop.ToStopped()
return return
case heartbeat = <-t.heartbeat: case heartbeat = <-t.heartbeat:
if healthy(heartbeat) { if healthy(heartbeat) {
......
...@@ -9,12 +9,11 @@ package health ...@@ -9,12 +9,11 @@ package health
import ( import (
"gitlab.com/elixxir/comms/network" "gitlab.com/elixxir/comms/network"
// "gitlab.com/elixxir/comms/network"
"testing" "testing"
"time" "time"
) )
// Happy path smoke test // Happy path smoke test.
func TestNewTracker(t *testing.T) { func TestNewTracker(t *testing.T) {
// Initialize required variables // Initialize required variables
timeout := 250 * time.Millisecond timeout := 250 * time.Millisecond
...@@ -49,8 +48,7 @@ func TestNewTracker(t *testing.T) { ...@@ -49,8 +48,7 @@ func TestNewTracker(t *testing.T) {
// Begin the health tracker // Begin the health tracker
_, err := tracker.Start() _, err := tracker.Start()
if err != nil { if err != nil {
t.Errorf("Unable to start tracker: %+v", err) t.Fatalf("Unable to start tracker: %+v", err)
return
} }
// Send a positive health heartbeat // Send a positive health heartbeat
...@@ -68,14 +66,12 @@ func TestNewTracker(t *testing.T) { ...@@ -68,14 +66,12 @@ func TestNewTracker(t *testing.T) {
// Verify the network was marked as healthy // Verify the network was marked as healthy
if !tracker.IsHealthy() { if !tracker.IsHealthy() {
t.Errorf("Tracker did not become healthy") t.Fatal("Tracker did not become healthy.")
return
} }
// Check if the tracker was ever healthy // Check if the tracker was ever healthy
if !tracker.WasHealthy() { if !tracker.WasHealthy() {
t.Errorf("Tracker did not become healthy") t.Fatal("Tracker did not become healthy.")
return
} }
// Verify the heartbeat triggered the listening chan/func // Verify the heartbeat triggered the listening chan/func
...@@ -89,15 +85,12 @@ func TestNewTracker(t *testing.T) { ...@@ -89,15 +85,12 @@ func TestNewTracker(t *testing.T) {
// Verify the network was marked as NOT healthy // Verify the network was marked as NOT healthy
if tracker.IsHealthy() { if tracker.IsHealthy() {
t.Errorf("Tracker should not report healthy") t.Fatal("Tracker should not report healthy.")
return
} }
// Check if the tracker was ever healthy, // Check if the tracker was ever healthy, after setting healthy to false
// after setting healthy to false
if !tracker.WasHealthy() { if !tracker.WasHealthy() {
t.Errorf("Tracker was healthy previously but not reported healthy") t.Fatal("Tracker was healthy previously but not reported healthy.")
return
} }
// Verify the timeout triggered the listening chan/func // 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 to comment