Skip to content
Merged
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
98 changes: 98 additions & 0 deletions pkg/blobplugin/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,15 @@ limitations under the License.
package main

import (
"context"
"fmt"
"io"
"net"
"net/http"
"os"
"reflect"
"testing"
"time"
)

func TestMain(t *testing.T) {
Expand Down Expand Up @@ -80,3 +84,97 @@ func TestTrapClosedConnErr(t *testing.T) {
}
}
}

func TestServeMetrics(t *testing.T) {
// Open random test port
l, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatalf("listen: %v", err)
}

// Start serveMetrics in background
errCh := make(chan error, 1)
go func() { errCh <- serveMetrics(l) }()

// Build URL
url := "http://" + l.Addr().String() + "/metrics"

// Client timeout for each request
client := &http.Client{Timeout: 500 * time.Millisecond}

// Poll with 3-second deadlines until server is ready
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()

done := make(chan struct{})
go func(ctx context.Context) {
defer close(done)
for {
req, _ := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
// Execute probe, expecting status 200
resp, err := client.Do(req)
if err == nil {
if resp.StatusCode == http.StatusOK {
resp.Body.Close()
return
}
resp.Body.Close()
}
// Abort probe if context deadline is expired or canceled
select {
case <-ctx.Done():
return
default:
time.Sleep(20 * time.Millisecond)
}
}
}(ctx)

// Wait for readiness or fail on timeout
select {
case <-done:
case <-ctx.Done():
t.Fatalf("server not ready: %v", ctx.Err())
}

// Perform the request
req, _ := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
resp, err := client.Do(req)
if err != nil {
t.Fatalf("Get /metrics: %v", err)
}
t.Cleanup(func() {
if resp != nil && resp.Body != nil {
_ = resp.Body.Close()
}
})

// Check for HTTP status 200
if resp.StatusCode != http.StatusOK {
t.Fatalf("unexpected status: %d", resp.StatusCode)
}
// Validate response body is non-empty
body, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatalf("read body: %v", err)
}
// Validate response body is non-empty
Copy link
Preview

Copilot AI Aug 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment is duplicated on lines 156 and 161. Remove the duplicate comment to avoid redundancy.

Suggested change
// Validate response body is non-empty

Copilot uses AI. Check for mistakes.

if len(body) == 0 {
t.Fatalf("empty metrics body")
}

// Trigger graceful shutdown
if err := l.Close(); err != nil {
t.Fatalf("close listener %v", err)
}

// Fail if errCh exits non-graceful or is not closed after 2 sec
select {
case err := <-errCh:
if err != nil {
t.Fatalf("serveMetrics error after close: %v", err)
}
case <-time.After(2 * time.Second):
t.Fatalf("serveMetrics did not exit after listener close")
}
}
Loading