Skip to content

Commit bb2bf90

Browse files
authored
Update Connections to handle negative values (#568)
* Update connections to allow negative values * fix test naming
1 parent 95894c0 commit bb2bf90

File tree

2 files changed

+64
-4
lines changed

2 files changed

+64
-4
lines changed

client/nginx.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -262,10 +262,10 @@ type ExtendedCacheStats struct {
262262

263263
// Connections represents connection related stats.
264264
type Connections struct {
265-
Accepted uint64
266-
Dropped uint64
267-
Active uint64
268-
Idle uint64
265+
Accepted int64
266+
Dropped int64
267+
Active int64
268+
Idle int64
269269
}
270270

271271
// Slabs is map of slab stats by zone name.

client/nginx_test.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -765,6 +765,66 @@ func TestGetStats_NoStreamEndpoint(t *testing.T) {
765765
}
766766
}
767767

768+
func TestGetStats_Connections(t *testing.T) {
769+
t.Parallel()
770+
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
771+
switch {
772+
case r.RequestURI == "/":
773+
_, err := w.Write([]byte(`[4, 5, 6, 7, 8, 9]`))
774+
if err != nil {
775+
t.Fatalf("unexpected error: %v", err)
776+
}
777+
case r.RequestURI == "/8/":
778+
_, err := w.Write([]byte(`["nginx","processes","connections","slabs","http","resolvers","ssl","workers"]`))
779+
if err != nil {
780+
t.Fatalf("unexpected error: %v", err)
781+
}
782+
case strings.HasPrefix(r.RequestURI, "/8/connections"):
783+
_, err := w.Write([]byte(`{
784+
"active": -1,
785+
"idle": 3,
786+
"accepted": 5,
787+
"dropped": 2
788+
}`))
789+
if err != nil {
790+
t.Fatalf("unexpected error: %v", err)
791+
}
792+
default:
793+
_, err := w.Write([]byte(`{}`))
794+
if err != nil {
795+
t.Fatalf("unexpected error: %v", err)
796+
}
797+
}
798+
}))
799+
800+
defer ts.Close()
801+
802+
// Test creating a new client with a supported API version on the server
803+
client, err := NewNginxClient(ts.URL, WithAPIVersion(8), WithCheckAPI())
804+
if err != nil {
805+
t.Fatalf("unexpected error: %v", err)
806+
}
807+
if client == nil {
808+
t.Fatalf("client is nil")
809+
}
810+
811+
stats, err := client.GetStats(context.Background())
812+
if err != nil {
813+
t.Fatalf("unexpected error: %v", err)
814+
}
815+
816+
testStats := Connections{
817+
Accepted: 5,
818+
Dropped: 2,
819+
Active: -1,
820+
Idle: 3,
821+
}
822+
823+
if !reflect.DeepEqual(stats.Connections, testStats) {
824+
t.Fatalf("Connection stats: expected %v, actual %v", testStats, stats.Connections)
825+
}
826+
}
827+
768828
func TestGetStats_SSL(t *testing.T) {
769829
t.Parallel()
770830
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {

0 commit comments

Comments
 (0)