Merged in bugfix/dynamic-test-ports (pull request #70)

use dynamic ports for prometheus tests

* dynamic port

* Merge branch 'main' of bitbucket.org:aarete/query-orchestration into bugfix/dynamic-test-ports


Approved-by: Michael McGuinness
This commit is contained in:
Jay Brown
2025-02-17 16:06:43 +00:00
parent 4bbc24e8aa
commit e357555725
@@ -4,23 +4,46 @@ import (
"context" "context"
"fmt" "fmt"
"io" "io"
"net"
"net/http" "net/http"
"strings" "strings"
"testing" "testing"
"time" "time"
) )
func GetFreePortForTesting() (int, error) {
// Create a TCP listener on port 0 (lets OS choose a free port)
addr, err := net.ResolveTCPAddr("tcp", "localhost:0")
if err != nil {
return 0, fmt.Errorf("failed to resolve TCP address: %v", err)
}
l, err := net.ListenTCP("tcp", addr)
if err != nil {
return 0, fmt.Errorf("failed to create TCP listener: %v", err)
}
defer l.Close()
// Get the port that was assigned by the OS
return l.Addr().(*net.TCPAddr).Port, nil
}
func TestMetricsIntegration(t *testing.T) { func TestMetricsIntegration(t *testing.T) {
// Create a context with cancellation for the test // Create a context with cancellation for the test
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
defer cancel() defer cancel()
testingPort, errorGettingPort := GetFreePortForTesting()
if errorGettingPort != nil {
t.Fatalf("Failed to get a free port for testing: %v", errorGettingPort)
}
// Step 1: Define our test metrics configuration // Step 1: Define our test metrics configuration
config := MetricsConfig{ config := MetricsConfig{
Namespace: "test", Namespace: "test",
Subsystem: "metrics", Subsystem: "metrics",
Port: 9090, // Using 9090 for tests Port: testingPort, // Using 9090 for tests
ServeHttp: true, // Enable the built-in HTTP server ServeHttp: true, // Enable the built-in HTTP server
Definitions: []MetricDefinition{ Definitions: []MetricDefinition{
{ {
// Counter example: counting total events // Counter example: counting total events
@@ -114,7 +137,8 @@ func TestMetricsIntegration(t *testing.T) {
time.Sleep(time.Second) time.Sleep(time.Second)
// Step 5: Fetch metrics from the HTTP endpoint // Step 5: Fetch metrics from the HTTP endpoint
resp, err := http.Get("http://localhost:9090/metrics") URLForMetrics := fmt.Sprintf("http://localhost:%d/metrics", testingPort)
resp, err := http.Get(URLForMetrics)
if err != nil { if err != nil {
t.Fatalf("Failed to fetch metrics: %v", err) t.Fatalf("Failed to fetch metrics: %v", err)
} }
@@ -155,10 +179,15 @@ func TestMetricsIntegration(t *testing.T) {
// Test cancellation // Test cancellation
t.Run("Context cancellation", func(t *testing.T) { t.Run("Context cancellation", func(t *testing.T) {
// Create a new metrics instance with a separate context // 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)
}
cancelCtx, cancelFunc := context.WithCancel(context.Background()) cancelCtx, cancelFunc := context.WithCancel(context.Background())
_, err := New(cancelCtx, MetricsConfig{ _, err := New(cancelCtx, MetricsConfig{
Namespace: "test_cancel", Namespace: "test_cancel",
Port: 9091, Port: testingPort,
ServeHttp: true, ServeHttp: true,
}) })
if err != nil { if err != nil {
@@ -173,7 +202,8 @@ func TestMetricsIntegration(t *testing.T) {
// Try to connect to the server - it should fail after a short delay // Try to connect to the server - it should fail after a short delay
time.Sleep(time.Second) time.Sleep(time.Second)
_, err = http.Get("http://localhost:9091/metrics") testingUrl := fmt.Sprintf("http://localhost:%d/metrics", testingPort)
_, err = http.Get(testingUrl)
if err == nil { if err == nil {
t.Error("Expected server to be shutdown after context cancellation") t.Error("Expected server to be shutdown after context cancellation")
} }