Files
mirror-3x-ui/internal/web/service/inbound_online_availability_test.go
T
Hamed fb14c8358a fix(inbounds): distinguish unavailable online attribution (#7289)
Distinguish unavailable, pending, explicitly idle and legacy inbound online reports across REST, WebSocket and node snapshots. Require hosting-node emails and matching inbound activity; use a labeled node-wide fallback only for confirmed unsupported endpoints (HTTP 404/405).

Track Xray freshness separately from sidecar observations and prevent accounting retries from publishing online state across core replacement. Preserve partial sidecar reports through mixed-version relays using the reserved nonempty NUL marker. Retain GUID/tag remapping and attached-client filtering.

Validated on the reviewed head: all 12 CI jobs and Docs CI passed, including 2,332 frontend tests, mandatory PostgreSQL attribution regressions, race checks, Xray/node E2E and all four master/node version combinations. Exact client attribution across simultaneously active shared-client inbounds remains an estimate; older nodes may omit idle GUIDs during rollout.

Fixes #7283.
2026-12-03 02:37:40 +03:30

87 lines
3.4 KiB
Go

package service
import (
"slices"
"testing"
"time"
"github.com/mhsanaei/3x-ui/v3/internal/testpg"
"github.com/mhsanaei/3x-ui/v3/internal/xray"
)
func TestInboundOnlineAvailabilityDistinguishesEmptyFromMissing(t *testing.T) {
cleanup, err := testpg.IsolatePackage("online_availability")
if err != nil {
t.Fatal(err)
}
t.Cleanup(cleanup)
setupConflictDB(t)
process := useOnlineTestProcess(t)
svc := &InboundService{}
guid := svc.panelGuid()
if guid == "" {
t.Fatal("test panel has no GUID")
}
process.RefreshLocalOnline(nil, nil, time.Now().UnixMilli(), onlineGracePeriodMs)
if _, ok := svc.GetActiveInboundsByGuid()[guid]; ok {
t.Fatal("pruning before a local poll must not publish a known-empty result")
}
process = xray.NewTestProcess(nil, "")
xrayState.replace(process)
svc.RefreshLocalOnlineClients([]string{}, []string{})
if _, ok := svc.GetActiveInboundsByGuid()[guid]; ok {
t.Fatal("an idle sidecar tick must not publish activity on a replacement core")
}
process.RecordXrayOnlineSample(nil, nil, time.Now().UnixMilli(), onlineGracePeriodMs)
assertEmpty := func(signal map[string][]string, key string) {
t.Helper()
values, ok := signal[key]
if !ok || values == nil || len(values) != 0 {
t.Fatalf("expected an explicit empty list for %v in %v", key, signal)
}
}
assertEmpty(svc.GetOnlineClientsByGuid(), guid)
assertEmpty(svc.GetActiveInboundsByGuid(), guid)
// A recently observed sidecar remains attributable while the Xray sample
// is stale. The reserved tag marks this as a partial report.
now := time.Now().UnixMilli()
process.RecordXrayOnlineSample([]string{"xray@example"}, []string{"xray"}, now-onlineGracePeriodMs, onlineGracePeriodMs)
svc.RefreshLocalOnlineClients([]string{"sidecar@example"}, []string{"sidecar"})
if got := svc.GetOnlineClientsByGuid()[guid]; !slices.Equal(got, []string{"sidecar@example"}) {
t.Fatalf("partial report lost the sidecar or retained expired Xray clients: %v", got)
}
tags := svc.GetActiveInboundsByGuid()[guid]
if len(tags) != 2 || !slices.Contains(tags, "sidecar") || !slices.Contains(tags, xray.PartialInboundActivityTag) {
t.Fatalf("want only the sidecar tag and the partial report marker, got %v", tags)
}
if got := svc.GetOnlineClients(); !slices.Equal(got, []string{"sidecar@example"}) {
t.Fatalf("combined online list lost sidecar activity: %v", got)
}
process.RefreshLocalOnline(nil, nil, now+onlineGracePeriodMs+1000, onlineGracePeriodMs)
if _, ok := svc.GetActiveInboundsByGuid()[guid]; ok {
t.Fatal("expired sidecar observations must not keep the partial report")
}
xrayState.replace(xray.NewTestProcess(nil, ""))
if _, ok := svc.GetOnlineClientsByGuid()[guid]; ok {
t.Fatal("replacement core has not reported online clients yet")
}
if _, ok := svc.GetActiveInboundsByGuid()[guid]; ok {
t.Fatal("replacement core has not reported active inbounds yet")
}
svc.SetNodeOnlineTree(5, map[string][]string{"remote": {}})
currentXrayProcess().SetNodeActiveInboundTree(5, map[string][]string{"remote": {}})
assertEmpty(svc.GetOnlineClientsByGuid(), "remote")
assertEmpty(svc.GetActiveInboundsByGuid(), "remote")
svc.ClearNodeOnlineClients(5)
if _, ok := svc.GetOnlineClientsByGuid()["remote"]; ok {
t.Fatal("remote timeout must remove the stale online result")
}
if _, ok := svc.GetActiveInboundsByGuid()["remote"]; ok {
t.Fatal("remote timeout must remove the stale active-inbound result")
}
}