Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update readinesshandler_test.go #1952

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 29 additions & 21 deletions health/readinesshandler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,39 +12,47 @@ import (

func TestReadinessHandler(t *testing.T) {
ctx := context.Background()

// Create a test server and defer its closure
server := httptest.NewServer(health.ReadinessHandler())
defer server.Close()

client := server.Client()

req, err := httputil.NewRequestWithContext(ctx, http.MethodGet, server.URL, nil)
if err != nil {
t.Fatalf("failed to create request: %v", err)
makeRequest := func() (*http.Response, error) {
req, err := httputil.NewRequestWithContext(ctx, http.MethodGet, server.URL, nil)
if err != nil {
return nil, fmt.Errorf("failed to create request: %v", err)
}
return client.Do(req)
}
// default handler state should return StatusServiceUnavailable
resp, err := client.Do(req)
if err != nil {
t.Fatalf("failed to do request: %v", err)

checkStatusCode := func(expected, actual int) {
if expected != actual {
t.Errorf("expected status code %d, got %d", expected, actual)
}
}
if resp.StatusCode != http.StatusServiceUnavailable {
t.Fatalf("expected %d got %d", http.StatusServiceUnavailable, resp.StatusCode)

// Default handler state should return StatusServiceUnavailable
resp, err := makeRequest()
if err != nil {
t.Errorf("failed to do request: %v", err)
}
checkStatusCode(http.StatusServiceUnavailable, resp.StatusCode)

// signal to handler that process is ready. should return StatusOK
// Signal to handler that the process is ready. Should return StatusOK
health.Ready()
resp, err = client.Do(req)
resp, err = makeRequest()
if err != nil {
t.Fatalf("failed to do request: %v", err)
}
if resp.StatusCode != http.StatusOK {
t.Fatalf("expected %d got %d", http.StatusOK, resp.StatusCode)
t.Errorf("failed to do request: %v", err)
}
checkStatusCode(http.StatusOK, resp.StatusCode)

// signal to handler that process is unready. should return StatusServiceUnavailable
// Signal to handler that the process is unready. Should return StatusServiceUnavailable
health.Unready()
resp, err = client.Do(req)
resp, err = makeRequest()
if err != nil {
t.Fatalf("failed to do request: %v", err)
}
if resp.StatusCode != http.StatusServiceUnavailable {
t.Fatalf("expected %d got %d", http.StatusServiceUnavailable, resp.StatusCode)
t.Errorf("failed to do request: %v", err)
}
checkStatusCode(http.StatusServiceUnavailable, resp.StatusCode)
}