Skip to content

Commit

Permalink
Merge pull request #51 from dmarkhas/sensible_defaults
Browse files Browse the repository at this point in the history
set default execution period to avoid panic
  • Loading branch information
eranharel authored Jul 1, 2021
2 parents 87dcbbf + 5a33dc6 commit fb5b7bd
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
11 changes: 9 additions & 2 deletions health.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,19 @@ type health struct {
}

func (h *health) RegisterCheck(check Check, opts ...CheckOption) error {
if check == nil || check.Name() == "" {
return errors.Errorf("misconfigured check %v", check)
if check == nil {
return errors.New("check must not be nil")
}
if check.Name() == "" {
return errors.New("check name must not be empty")
}

cfg := h.initCheckConfig(opts)

if cfg.executionPeriod <= 0 {
return errors.New("execution period must be greater than 0")
}

// checks are initially failing by default, but we allow overrides...
var initialErr error
if !cfg.initiallyPassing {
Expand Down
22 changes: 20 additions & 2 deletions health_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,33 @@ func TestHealthWithBogusCheck(t *testing.T) {
err := h.RegisterCheck(nil)
defer h.DeregisterAll()

assert.Error(t, err, "register bogus check should fail")
assert.Contains(t, err.Error(), "misconfigured check", "fail message")
assert.EqualError(t, err, "check must not be nil")
assert.True(t, h.IsHealthy(), "health after bogus register")

results, healthy := h.Results()
assert.True(t, healthy, "results after bogus register")
assert.Empty(t, results, "results after bogus register")
}

func TestRegisterCheckValidations(t *testing.T) {
h := New()
defer h.DeregisterAll()

// should return an error for nil check
assert.EqualError(t, h.RegisterCheck(nil), "check must not be nil")
// should return an error for missing check name
assert.EqualError(t, h.RegisterCheck(&checks.CustomCheck{}), "check name must not be empty")
// Should return an error for missing execution period
assert.EqualError(t, h.RegisterCheck(&checks.CustomCheck{CheckName: "non-empty"}), "execution period must be greater than 0")

hWithExecPeriod := New(ExecutionPeriod(1 * time.Minute))
defer hWithExecPeriod.DeregisterAll()

// should inherit the execution period from the health instance
assert.NoError(t, hWithExecPeriod.RegisterCheck(&checks.CustomCheck{CheckName: "non-empty"}))

}

func TestRegisterDeregister(t *testing.T) {
leaktest.Check(t)

Expand Down

0 comments on commit fb5b7bd

Please sign in to comment.