Files
mirror-3x-ui/internal/web/job/node_client_speed_test.go
T
Bazil Bazikov e65b5cbd65 feat(nodes): show per-client live speed for clients served by nodes (#7100)
The clients page showed live upload/download speed only for clients the master's own Xray or TUIC served; a client connected through a managed node showed none, although the master already polls that node's counters.

Derive per-client deltas from each node snapshot in the traffic sync job and broadcast them as their own speed source, so they add to local speed instead of overwriting it. A client's diverging per-inbound counter copies fold to the per-field max, the rule the traffic merge applies (#5274).
2026-11-24 20:07:14 +03:30

89 lines
3.3 KiB
Go

package job
import (
"testing"
"github.com/mhsanaei/3x-ui/v3/internal/database/model"
"github.com/mhsanaei/3x-ui/v3/internal/web/runtime"
"github.com/mhsanaei/3x-ui/v3/internal/xray"
)
// A legacy per-inbound row can lag the shared counter (#5274); listed last, it must not win.
func TestNodeClientTotals_FoldsDivergentCopiesToTheHighest(t *testing.T) {
row := xray.ClientTraffic{Email: "a@x", Up: 100, Down: 200}
stale := xray.ClientTraffic{Email: "a@x", Up: 40, Down: 60}
snap := &runtime.TrafficSnapshot{Inbounds: []*model.Inbound{
{Tag: "in-443", ClientStats: []xray.ClientTraffic{row, {Email: "b@x", Up: 1, Down: 2}}},
{Tag: "in-9452", ClientStats: []xray.ClientTraffic{stale}},
nil,
}}
got := nodeClientTotals(snap)
if len(got) != 2 || got["a@x"] != [2]int64{100, 200} || got["b@x"] != [2]int64{1, 2} {
t.Fatalf("unexpected totals: %v", got)
}
}
func TestDiffClientTotals_FirstPollOnlyBaselines(t *testing.T) {
next, deltas := diffClientTotals(nil, map[string][2]int64{"a@x": {10, 20}}, 1000)
if len(deltas) != 0 {
t.Fatalf("first poll must not emit speed, got %v", deltas)
}
if next["a@x"] != (inboundSample{up: 10, down: 20, at: 1000}) {
t.Fatalf("baseline not recorded: %v", next)
}
}
func TestDiffClientTotals_DeltaAndIdle(t *testing.T) {
prev := map[string]inboundSample{
"a@x": {up: 10, down: 20, at: 0},
"b@x": {up: 5, down: 5, at: 0},
}
cur := map[string][2]int64{"a@x": {1010, 5020}, "b@x": {5, 5}}
next, deltas := diffClientTotals(prev, cur, nodeInboundSpeedWindowMs)
if len(deltas) != 1 || deltas[0].Email != "a@x" || deltas[0].Up != 1000 || deltas[0].Down != 5000 {
t.Fatalf("unexpected deltas: %+v", deltas)
}
// Idle client keeps its change timestamp so a later jump averages over the gap.
if next["b@x"].at != 0 {
t.Fatalf("idle client timestamp advanced: %v", next["b@x"])
}
}
func TestDiffClientTotals_GapIsAveraged(t *testing.T) {
prev := map[string]inboundSample{"a@x": {up: 0, down: 0, at: 0}}
// 4000 bytes moved over two windows -> shown as 2000 per window.
_, deltas := diffClientTotals(prev, map[string][2]int64{"a@x": {0, 4000}}, 2*nodeInboundSpeedWindowMs)
if len(deltas) != 1 || deltas[0].Down != 2000 {
t.Fatalf("gap not averaged: %+v", deltas)
}
}
func TestDiffClientTotals_ResetRebaselines(t *testing.T) {
prev := map[string]inboundSample{"a@x": {up: 500, down: 500, at: 0}}
next, deltas := diffClientTotals(prev, map[string][2]int64{"a@x": {0, 0}}, 7000)
if len(deltas) != 0 {
t.Fatalf("reset must not emit speed: %+v", deltas)
}
if next["a@x"] != (inboundSample{up: 0, down: 0, at: 7000}) {
t.Fatalf("reset not rebaselined: %v", next["a@x"])
}
}
func TestRecordNodeClientSpeed_PerNodeBaselinesAndPrune(t *testing.T) {
j := NewNodeTrafficSyncJob()
mk := func(up, down int64) *runtime.TrafficSnapshot {
return &runtime.TrafficSnapshot{Inbounds: []*model.Inbound{
{ClientStats: []xray.ClientTraffic{{Email: "a@x", Up: up, Down: down}}},
}}
}
j.recordNodeClientSpeed(1, mk(0, 0))
j.recordNodeClientSpeed(2, mk(1_000_000, 1_000_000))
if len(j.tickClientSpeed) != 0 || len(j.prevClientTotals) != 2 {
t.Fatalf("baselines: tick=%v prev=%v", j.tickClientSpeed, j.prevClientTotals)
}
j.pruneClientSpeedNodes([]*model.Node{{Id: 1}})
if _, ok := j.prevClientTotals[2]; ok || len(j.prevClientTotals) != 1 {
t.Fatalf("deleted node not pruned: %v", j.prevClientTotals)
}
}