Skip to content

Commit

Permalink
return an error if execution period is unset
Browse files Browse the repository at this point in the history
  • Loading branch information
dmarkhas committed Jun 20, 2021
1 parent 3e94174 commit b09a7a0
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 7 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
15 changes: 15 additions & 0 deletions health_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,21 @@ func TestHealthWithBogusCheck(t *testing.T) {
assert.Empty(t, results, "results after bogus register")
}

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

// 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))
assert.NoError(t, hWithExecPeriod.RegisterCheck(&checks.CustomCheck{CheckName: "non-empty"}))

}

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

Expand Down
6 changes: 1 addition & 5 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,7 @@ func WithHealthListeners(listener ...HealthListener) HealthOption {
// WithDefaults sets all the Health object settings. It's not required to use this as no options is always default
// This is a simple placeholder for any future defaults
func WithDefaults() HealthOption {
return healthOptionFunc(func(h *health) {
if h.defaultExecutionPeriod <= 0 {
h.defaultExecutionPeriod = 1 * time.Minute
}
})
return healthOptionFunc(func(h *health) {})
}

// CheckOption configures a health check using the functional options paradigm
Expand Down

0 comments on commit b09a7a0

Please sign in to comment.