Skip to content

Commit

Permalink
Merge d9b6a77 into 87dcbbf
Browse files Browse the repository at this point in the history
  • Loading branch information
dmarkhas committed Jun 20, 2021
2 parents 87dcbbf + d9b6a77 commit a726b49
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
11 changes: 9 additions & 2 deletions health.go
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
21 changes: 19 additions & 2 deletions health_test.go
Expand Up @@ -44,15 +44,32 @@ 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()

assert.NoError(t, hWithExecPeriod.RegisterCheck(&checks.CustomCheck{CheckName: "non-empty"}))

}

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

Expand Down

0 comments on commit a726b49

Please sign in to comment.