feat: drain readiness before graceful shutdown

This commit is contained in:
2026-09-20 19:41:28 +08:00
parent 656a81251e
commit 00d4156b25
5 changed files with 66 additions and 9 deletions

View File

@@ -22,6 +22,26 @@ func TestHealthSeparatesLivenessAndReadiness(t *testing.T) {
}
}
func TestReadinessEntersDrainBeforeShutdown(t *testing.T) {
checks := 0
readiness := NewReadiness(func(context.Context) error {
checks++
return nil
})
server := New("127.0.0.1:0", readiness.Check)
ready := httptest.NewRecorder()
server.Handler.ServeHTTP(ready, httptest.NewRequest(http.MethodGet, "/health/ready", nil))
if ready.Code != http.StatusOK || checks != 1 {
t.Fatalf("before drain: status=%d checks=%d", ready.Code, checks)
}
readiness.BeginDrain()
draining := httptest.NewRecorder()
server.Handler.ServeHTTP(draining, httptest.NewRequest(http.MethodGet, "/health/ready", nil))
if draining.Code != http.StatusServiceUnavailable || checks != 1 || strings.Contains(draining.Body.String(), "server is draining") {
t.Fatalf("during drain: status=%d checks=%d body=%s", draining.Code, checks, draining.Body.String())
}
}
func TestVersionIsPublicAndBounded(t *testing.T) {
server := New("127.0.0.1:0", nil)
rr := httptest.NewRecorder()