feat(health)!: promote to v1.0.0 — configurable check timeout via NewHandlerWithConfig

Add Config struct and NewHandlerWithConfig(logger, cfg, checks...) constructor.
Config.CheckTimeout sets the per-request deadline for all health checks; zero
value defaults to 5 seconds. NewHandler remains unchanged as a backward-compatible
wrapper. API committed as stable.
This commit is contained in:
2026-05-11 19:05:27 -06:00
parent e1b6b7ddd7
commit 8d6930b087
3 changed files with 67 additions and 6 deletions

View File

@@ -167,6 +167,30 @@ func TestHandler_JSON_Shape(t *testing.T) {
}
}
func TestHandlerWithConfig_CustomTimeout(t *testing.T) {
// 200ms delay check with a 50ms timeout — should be reported as DOWN.
h := NewHandlerWithConfig(&noopLogger{}, Config{CheckTimeout: 50 * time.Millisecond},
&mockCheck{name: "slow", priority: LevelCritical, delay: 200 * time.Millisecond},
)
code, resp := doRequest(t, h)
if code != http.StatusServiceUnavailable {
t.Errorf("want 503, got %d", code)
}
if resp.Status != "DOWN" {
t.Errorf("want DOWN, got %s", resp.Status)
}
}
func TestHandlerWithConfig_ZeroTimeout_UsesDefault(t *testing.T) {
h := NewHandlerWithConfig(&noopLogger{}, Config{},
&mockCheck{name: "db", priority: LevelCritical},
)
code, resp := doRequest(t, h)
if code != http.StatusOK || resp.Status != "UP" {
t.Errorf("want 200 UP, got %d %s", code, resp.Status)
}
}
func TestHandler_ContextTimeout(t *testing.T) {
// Check that times out faster than the 5s global timeout when client cancels.
h := NewHandler(&noopLogger{},