Merged in feature/functestcov (pull request #84)
Function Test Coverage * test * scripts
This commit is contained in:
@@ -9,6 +9,8 @@ import (
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func GetFreePortForTesting() (int, error) {
|
||||
@@ -34,9 +36,7 @@ func TestMetricsIntegration(t *testing.T) {
|
||||
defer cancel()
|
||||
|
||||
testingPort, errorGettingPort := GetFreePortForTesting()
|
||||
if errorGettingPort != nil {
|
||||
t.Fatalf("Failed to get a free port for testing: %v", errorGettingPort)
|
||||
}
|
||||
require.NoError(t, errorGettingPort)
|
||||
|
||||
// Step 1: Define our test metrics configuration
|
||||
config := MetricsConfig{
|
||||
@@ -84,9 +84,7 @@ func TestMetricsIntegration(t *testing.T) {
|
||||
|
||||
// Step 2: Create new metrics instance with context
|
||||
metrics, err := New(ctx, config)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create metrics: %v", err)
|
||||
}
|
||||
require.NoError(t, err)
|
||||
|
||||
// Ensure cleanup after test
|
||||
defer func() {
|
||||
@@ -104,33 +102,25 @@ func TestMetricsIntegration(t *testing.T) {
|
||||
"type": "user_login",
|
||||
"status": "success",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to record counter metric: %v", err)
|
||||
}
|
||||
require.NoError(t, err)
|
||||
|
||||
// Record a gauge metric
|
||||
err = metrics.RecordMetric("queue_size", 15, map[string]string{
|
||||
"queue_name": "default",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to record gauge metric: %v", err)
|
||||
}
|
||||
require.NoError(t, err)
|
||||
|
||||
// Record a histogram metric
|
||||
err = metrics.RecordMetric("response_time_seconds", 0.7, map[string]string{
|
||||
"endpoint": "/api/users",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to record histogram metric: %v", err)
|
||||
}
|
||||
require.NoError(t, err)
|
||||
|
||||
// Record a summary metric
|
||||
err = metrics.RecordMetric("request_size_bytes", 1024, map[string]string{
|
||||
"method": "POST",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to record summary metric: %v", err)
|
||||
}
|
||||
require.NoError(t, err)
|
||||
|
||||
// Step 4: Wait for HTTP server to start
|
||||
// Give the server a moment to start up
|
||||
@@ -139,16 +129,12 @@ func TestMetricsIntegration(t *testing.T) {
|
||||
// Step 5: Fetch metrics from the HTTP endpoint
|
||||
URLForMetrics := fmt.Sprintf("http://localhost:%d/metrics", testingPort)
|
||||
resp, err := http.Get(URLForMetrics)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to fetch metrics: %v", err)
|
||||
}
|
||||
require.NoError(t, err)
|
||||
defer resp.Body.Close()
|
||||
|
||||
// Read the response body
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to read response body: %v", err)
|
||||
}
|
||||
require.NoError(t, err)
|
||||
|
||||
metricsOutput := string(body)
|
||||
// if needed for debugging.
|
||||
@@ -180,9 +166,7 @@ func TestMetricsIntegration(t *testing.T) {
|
||||
t.Run("Context cancellation", func(t *testing.T) {
|
||||
// Create a new metrics instance with a separate context
|
||||
testingPort, errorGettingPort := GetFreePortForTesting()
|
||||
if errorGettingPort != nil {
|
||||
t.Fatalf("Failed to get a free port for testing: %v", errorGettingPort)
|
||||
}
|
||||
require.NoError(t, errorGettingPort)
|
||||
|
||||
cancelCtx, cancelFunc := context.WithCancel(context.Background())
|
||||
_, err := New(cancelCtx, MetricsConfig{
|
||||
@@ -190,9 +174,7 @@ func TestMetricsIntegration(t *testing.T) {
|
||||
Port: testingPort,
|
||||
ServeHttp: true,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create metrics for cancellation test: %v", err)
|
||||
}
|
||||
require.NoError(t, err)
|
||||
|
||||
// Wait for server to start
|
||||
time.Sleep(time.Second)
|
||||
|
||||
Reference in New Issue
Block a user