Skip to content

Commit

Permalink
Merge pull request #37 from InVisionApp/dselans/failed-update
Browse files Browse the repository at this point in the history
Dselans/failed update
  • Loading branch information
dselans authored Feb 10, 2018
2 parents 93121e5 + 80341f2 commit f294fa7
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
7 changes: 3 additions & 4 deletions health.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,10 +214,9 @@ func (h *Health) startRunner(cfg *Config, ticker *time.Ticker, stop <-chan struc
stateEntry.Status = "failed"
}

// Toggle the global state failure if this check is allowed to cause
// a complete healthcheck failure.
if err != nil && cfg.Fatal {
h.failed.setTrue()
// Toggle the global failed state if check is configured as fatal
if cfg.Fatal {
h.failed.set(err != nil)
}

h.safeUpdateState(stateEntry)
Expand Down
13 changes: 12 additions & 1 deletion health_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ func TestState(t *testing.T) {
Expect(states["foo"].Err).To(Equal("things broke"))
})

t.Run("When a fatally-configured check fails, states should return 'true' for failed bool", func(t *testing.T) {
t.Run("When a fatally-configured check fails and recovers, state should get updated accordingly", func(t *testing.T) {
h := New()
checker1 := &fakes.FakeICheckable{}
checker1.StatusReturns(nil, fmt.Errorf("things broke"))
Expand Down Expand Up @@ -239,6 +239,17 @@ func TestState(t *testing.T) {
Expect(failed).To(BeTrue())
Expect(states).To(HaveKey("foo"))
Expect(states["foo"].Err).To(Equal("things broke"))

// And now, let's let it recover
checker1.StatusReturns(nil, nil)
time.Sleep(time.Duration(15) * time.Millisecond)

statesRecov, failedRecov, errRecov := h.State()
Expect(errRecov).ToNot(HaveOccurred())
Expect(failedRecov).To(BeFalse())
Expect(statesRecov).To(HaveKey("foo"))
Expect(statesRecov["foo"].Err).To(BeEmpty())

})

}
Expand Down
6 changes: 6 additions & 0 deletions safe.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ func newBool() *sBool {
return &sBool{v: false}
}

func (b *sBool) set(v bool) {
b.mu.Lock()
defer b.mu.Unlock()
b.v = v
}

func (b *sBool) setFalse() {
b.mu.Lock()
defer b.mu.Unlock()
Expand Down

0 comments on commit f294fa7

Please sign in to comment.