diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index ce19f2ba1..32f26d58e 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -358,6 +358,13 @@ jobs: exit 1 fi + - name: PostgreSQL node fleet count regressions + run: | + set -o pipefail + go test ./internal/web/service -run '^TestNode(FleetQuotaClassificationAtInt64Limits|OverviewCountsHostMembershipsAndUniqueFleetClients)$' -count=1 -v | tee /tmp/postgres-node-fleet.log + grep -q '^--- PASS: TestNodeFleetQuotaClassificationAtInt64Limits ' /tmp/postgres-node-fleet.log + grep -q '^--- PASS: TestNodeOverviewCountsHostMembershipsAndUniqueFleetClients ' /tmp/postgres-node-fleet.log + - name: PostgreSQL schema and migration tests run: | set -o pipefail diff --git a/internal/web/service/node_fleet.go b/internal/web/service/node_fleet.go index 68eba94ba..44cca0ee6 100644 --- a/internal/web/service/node_fleet.go +++ b/internal/web/service/node_fleet.go @@ -98,7 +98,12 @@ func (s *NodeService) countNodeFleet(nodes []*model.Node, selfGUID string, overv ), classified AS ( SELECT s.fleet, s.host, CASE WHEN (ct.expiry_time > 0 AND ct.expiry_time <= ?) - OR (ct.total > 0 AND ct.up + ct.down >= ct.total) THEN 'depleted' + OR CASE + -- Subtract a nonnegative counter from a positive quota to stay in BIGINT range. + WHEN ct.total > 0 AND ct.down >= 0 THEN ct.up >= ct.total - ct.down + WHEN ct.total > 0 AND ct.up >= 0 THEN ct.down >= ct.total - ct.up + ELSE FALSE + END THEN 'depleted' WHEN COALESCE(c.enable, FALSE) = FALSE THEN 'disabled' ELSE 'active' END AS state FROM scoped s JOIN clients c ON c.id = s.client_id diff --git a/internal/web/service/node_fleet_test.go b/internal/web/service/node_fleet_test.go index 824a4d533..9cfb25125 100644 --- a/internal/web/service/node_fleet_test.go +++ b/internal/web/service/node_fleet_test.go @@ -3,6 +3,7 @@ package service import ( "encoding/json" "fmt" + "math" "net/http" "net/http/httptest" "net/url" @@ -16,6 +17,7 @@ import ( "github.com/mhsanaei/3x-ui/v3/internal/database/dbtest" "github.com/mhsanaei/3x-ui/v3/internal/database/model" "github.com/mhsanaei/3x-ui/v3/internal/testpg" + "github.com/mhsanaei/3x-ui/v3/internal/xray" ) func TestNodeOverviewWithoutNodesSkipsFleetReads(t *testing.T) { @@ -148,6 +150,7 @@ func TestNodeOverviewCountsHostMembershipsAndUniqueFleetClients(t *testing.T) { {ClientId: clients[0].Id, InboundId: child.Id}, {ClientId: clients[1].Id, InboundId: child.Id}, {ClientId: clients[1].Id, InboundId: clone.Id}, + {ClientId: math.MaxInt32, InboundId: clone.Id}, } if err := db.Create(&links).Error; err != nil { t.Fatal(err) @@ -191,6 +194,65 @@ func TestNodeOverviewCountsHostMembershipsAndUniqueFleetClients(t *testing.T) { } } +func TestNodeFleetQuotaClassificationAtInt64Limits(t *testing.T) { + cleanup, err := testpg.IsolatePackage("node_fleet_quota") + if err != nil { + t.Fatal(err) + } + t.Cleanup(cleanup) + dbtest.InitDB(t, filepath.Join(t.TempDir(), "fleet-quota.db")) + db := database.GetDB() + node := model.Node{Name: "remote", Address: "192.0.2.1", Port: 2053, Guid: "remote"} + inbound := model.Inbound{Tag: "remote", Port: 443, NodeID: &node.Id, Settings: `{"clients":[]}`} + client := model.ClientRecord{Email: "quota@test", Enable: true} + for _, row := range []any{&node, &inbound, &client} { + if err := db.Create(row).Error; err != nil { + t.Fatal(err) + } + } + membership := model.ClientInbound{ClientId: client.Id, InboundId: inbound.Id} + if err := db.Create(&membership).Error; err != nil { + t.Fatal(err) + } + traffic := xray.ClientTraffic{Email: client.Email, InboundId: inbound.Id} + if err := db.Create(&traffic).Error; err != nil { + t.Fatal(err) + } + + for _, tc := range []struct { + name string + up, down, total int64 + depleted int + }{ + {"above limit", math.MaxInt64, 1, math.MaxInt64, 1}, + {"at limit", 1, math.MaxInt64 - 1, math.MaxInt64, 1}, + {"below limit", math.MaxInt64 - 1, 0, math.MaxInt64, 0}, + {"negative upload", -1, math.MaxInt64, math.MaxInt64, 0}, + {"negative download", math.MaxInt64, -1, math.MaxInt64, 0}, + {"negative underflow", math.MinInt64, -1, 1, 0}, + {"unlimited", math.MaxInt64, 1, 0, 0}, + } { + t.Run(tc.name, func(t *testing.T) { + if err := db.Model(&traffic).Updates(map[string]any{"up": tc.up, "down": tc.down, "total": tc.total}).Error; err != nil { + t.Fatal(err) + } + overview, err := (&NodeService{}).GetNodeOverview(nil) + if err != nil { + t.Fatal(err) + } + want := NodeFleetCounts{InboundCount: 1, ClientCount: 1, ActiveCount: 1 - tc.depleted, DepletedCount: tc.depleted} + if overview.Fleet != want || len(overview.Nodes) != 1 { + t.Fatalf("fleet counts = %+v, nodes = %d, want %+v and one node", overview.Fleet, len(overview.Nodes), want) + } + got := overview.Nodes[0] + if got.ClientCount != want.ClientCount || got.ActiveCount != want.ActiveCount || + got.DisabledCount != 0 || got.DepletedCount != want.DepletedCount { + t.Fatalf("unexpected remote client counts: %+v", got) + } + }) + } +} + func TestNodeOverviewUsesBestDescendantObservation(t *testing.T) { cleanup, err := testpg.IsolatePackage("node_fleet_descendants") if err != nil {