Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
Signed-off-by: Gyuho Lee <[email protected]>
  • Loading branch information
gyuho committed Feb 11, 2025
1 parent 3ced02e commit bbf4f3e
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 276 deletions.
9 changes: 6 additions & 3 deletions client/v1/healthz.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func CheckHealthz(ctx context.Context, addr string, opts ...OpOption) error {
return fmt.Errorf("failed to marshal expected healthz response: %w", err)
}

return checkHealthz(op.httpClient, req, exp)
return checkHealthz(createDefaultHTTPClient(), req, exp)
}

func checkHealthz(cli *http.Client, req *http.Request, exp []byte) error {
Expand Down Expand Up @@ -72,12 +72,15 @@ func BlockUntilServerReady(ctx context.Context, addr string, opts ...OpOption) e
return fmt.Errorf("failed to marshal expected healthz response: %w", err)
}

ticker := time.NewTicker(op.checkInterval)
httpClient := createDefaultHTTPClient()

ticker := time.NewTicker(time.Second)

Check warning on line 77 in client/v1/healthz.go

View check run for this annotation

Codecov / codecov/patch

client/v1/healthz.go#L75-L77

Added lines #L75 - L77 were not covered by tests
defer ticker.Stop()

Check warning on line 79 in client/v1/healthz.go

View check run for this annotation

Codecov / codecov/patch

client/v1/healthz.go#L79

Added line #L79 was not covered by tests
for range 30 {
select {
case <-ticker.C:
if err := checkHealthz(op.httpClient, req, exp); err == nil {
if err := checkHealthz(httpClient, req, exp); err == nil {

Check warning on line 83 in client/v1/healthz.go

View check run for this annotation

Codecov / codecov/patch

client/v1/healthz.go#L83

Added line #L83 was not covered by tests
return nil
}
case <-ctx.Done():
Expand Down
44 changes: 2 additions & 42 deletions client/v1/healthz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ import (
"testing"
"time"

"github.com/leptonai/gpud/internal/server"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/leptonai/gpud/internal/server"
)

func TestCheckHealthz(t *testing.T) {
Expand Down Expand Up @@ -166,44 +167,3 @@ func TestCheckHealthzContextCancellation(t *testing.T) {
t.Error("CheckHealthz() with canceled context should return error")
}
}

func TestCheckHealthzWithCustomClient(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/healthz" {
t.Errorf("Expected /healthz path, got %s", r.URL.Path)
http.NotFound(w, r)
return
}
w.WriteHeader(http.StatusOK)
json, _ := server.DefaultHealthz.JSON()
if _, err := w.Write(json); err != nil {
t.Errorf("Error writing response: %v", err)
}
}))
defer srv.Close()

customClient := &http.Client{Timeout: 1 * time.Second}
err := CheckHealthz(context.Background(), srv.URL, WithHTTPClient(customClient))
if err != nil {
t.Errorf("CheckHealthz() with custom client error = %v, want nil", err)
}
}

func TestCheckHealthzTimeout(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
time.Sleep(200 * time.Millisecond)
w.WriteHeader(http.StatusOK)
json, _ := server.DefaultHealthz.JSON()
_, err := w.Write(json)
if err != nil {
t.Errorf("Error writing response: %v", err)
}
}))
defer srv.Close()

client := &http.Client{Timeout: 100 * time.Millisecond}
err := CheckHealthz(context.Background(), srv.URL, WithHTTPClient(client))
if err == nil {
t.Error("CheckHealthz() with timeout should return error")
}
}
30 changes: 0 additions & 30 deletions client/v1/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,10 @@
package v1

import (
"crypto/tls"
"net/http"
"time"

"github.com/leptonai/gpud/internal/server"
)

type Op struct {
httpClient *http.Client
checkInterval time.Duration
requestContentType string
requestAcceptEncoding string
components map[string]any
Expand All @@ -24,33 +18,9 @@ func (op *Op) applyOpts(opts []OpOption) error {
opt(op)
}

if op.httpClient == nil {
op.httpClient = &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
},
}
}

if op.checkInterval == 0 {
op.checkInterval = time.Second
}

return nil
}

func WithHTTPClient(cli *http.Client) OpOption {
return func(op *Op) {
op.httpClient = cli
}
}

func WithCheckInterval(interval time.Duration) OpOption {
return func(op *Op) {
op.checkInterval = interval
}
}

// WithRequestContentTypeYAML sets the request content type to YAML.
func WithRequestContentTypeYAML() OpOption {
return func(op *Op) {
Expand Down
193 changes: 0 additions & 193 deletions client/v1/options_test.go

This file was deleted.

3 changes: 1 addition & 2 deletions client/v1/package_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,12 @@ func GetPackageStatus(ctx context.Context, url string, opts ...OpOption) ([]pack
return nil, err
}

httpClient := op.httpClient
req, err := http.NewRequestWithContext(ctx, "GET", url, nil)
if err != nil {
return nil, err
}

resp, err := httpClient.Do(req)
resp, err := createDefaultHTTPClient().Do(req)
if err != nil {
return nil, err
}
Expand Down
Loading

0 comments on commit bbf4f3e

Please sign in to comment.