From 5607dfbedce58232e17812fa356e1e6402e8956d Mon Sep 17 00:00:00 2001 From: Aphral Griffin Date: Wed, 8 Oct 2025 12:00:26 +0100 Subject: [PATCH 1/2] Update connections to allow negative values --- client/nginx.go | 8 +++--- client/nginx_test.go | 60 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 4 deletions(-) diff --git a/client/nginx.go b/client/nginx.go index e6c9c0ad..2f99dabf 100644 --- a/client/nginx.go +++ b/client/nginx.go @@ -262,10 +262,10 @@ type ExtendedCacheStats struct { // Connections represents connection related stats. type Connections struct { - Accepted uint64 - Dropped uint64 - Active uint64 - Idle uint64 + Accepted int64 + Dropped int64 + Active int64 + Idle int64 } // Slabs is map of slab stats by zone name. diff --git a/client/nginx_test.go b/client/nginx_test.go index 33e6416c..30e31efd 100644 --- a/client/nginx_test.go +++ b/client/nginx_test.go @@ -765,6 +765,66 @@ func TestGetStats_NoStreamEndpoint(t *testing.T) { } } +func TestGetStats_Connections(t *testing.T) { + t.Parallel() + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + switch { + case r.RequestURI == "/": + _, err := w.Write([]byte(`[4, 5, 6, 7, 8, 9]`)) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + case r.RequestURI == "/8/": + _, err := w.Write([]byte(`["nginx","processes","connections","slabs","http","resolvers","ssl","workers"]`)) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + case strings.HasPrefix(r.RequestURI, "/8/connections"): + _, err := w.Write([]byte(`{ + "active": -1, + "idle": 3, + "accepted": 5, + "dropped": 2 + }`)) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + default: + _, err := w.Write([]byte(`{}`)) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + } + })) + + defer ts.Close() + + // Test creating a new client with a supported API version on the server + client, err := NewNginxClient(ts.URL, WithAPIVersion(8), WithCheckAPI()) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + if client == nil { + t.Fatalf("client is nil") + } + + stats, err := client.GetStats(context.Background()) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + + testStats := Connections{ + Accepted: 5, + Dropped: 2, + Active: -1, + Idle: 3, + } + + if !reflect.DeepEqual(stats.Connections, testStats) { + t.Fatalf("SSL stats: expected %v, actual %v", testStats, stats.Connections) + } +} + func TestGetStats_SSL(t *testing.T) { t.Parallel() ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { From bfbf6047ab711ad4f1653c7d7fb654d305ebe0ce Mon Sep 17 00:00:00 2001 From: Aphral Griffin Date: Wed, 8 Oct 2025 12:03:37 +0100 Subject: [PATCH 2/2] fix test naming --- client/nginx_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/nginx_test.go b/client/nginx_test.go index 30e31efd..f4fd569f 100644 --- a/client/nginx_test.go +++ b/client/nginx_test.go @@ -821,7 +821,7 @@ func TestGetStats_Connections(t *testing.T) { } if !reflect.DeepEqual(stats.Connections, testStats) { - t.Fatalf("SSL stats: expected %v, actual %v", testStats, stats.Connections) + t.Fatalf("Connection stats: expected %v, actual %v", testStats, stats.Connections) } }