Files
mirror-3x-ui/internal/web/runtime/remote_inbound_lookup_test.go
T
Hamed 0afa11ddff fix(nodes): share lightweight remote inbound catalog refreshes (#7313)
Replace full inbound/client-list downloads on cold remote ID lookups with an owner-scoped six-field catalog projected directly from the database. Coalesce concurrent catalog refreshes per remote while preserving independent caller cancellation and rejecting stale ID mappings after topology mutations.

Seed reconciliation from the catalog, create missing inbounds directly, preserve prefixed/adopted aliases and invalidate pushed state when remote IDs change. Avoid invalidating catalog generations on unchanged-ID settings pushes. Retain client-membership-aware reconciliation state and legacy full-list fallback only on HTTP 404/405.

Cover permissions, query projection, concurrent refresh/cancellation, deletion/recreation, reconciliation request counts and mixed-version node compatibility. Integrated CI and Docs CI passed, including race, node E2E and PostgreSQL catalog projection. Synthetic response-size comparison is not a production latency benchmark.

Fixes #7303.
2026-12-03 03:26:44 +03:30

66 lines
2.5 KiB
Go

package runtime
import (
"context"
"errors"
"net/http"
"net/http/httptest"
"strings"
"sync/atomic"
"testing"
"github.com/mhsanaei/3x-ui/v3/internal/database/model"
)
func TestInboundLookupFailureDoesNotAcknowledgeMutation(t *testing.T) {
for _, tc := range []struct {
name string
status int
body string
want string
}{
{"forbidden", http.StatusForbidden, "", "HTTP 403"},
{"unavailable", http.StatusServiceUnavailable, "", "HTTP 503"},
{"application error", http.StatusOK, `{"success":false,"msg":"node database unavailable"}`, "node database unavailable"},
{"invalid catalog", http.StatusOK, `{"success":true,"obj":{"id":4}}`, "decode inbound list"},
{"missing catalog", http.StatusOK, `{"success":true}`, "decode inbound list"},
{"invalid row", http.StatusOK, `{"success":true,"obj":[{"id":0,"tag":"in-lookup"}]}`, "decode inbound list"},
} {
t.Run(tc.name, func(t *testing.T) {
var mutations atomic.Int32
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
if req.Method != http.MethodGet || !strings.HasSuffix(req.URL.Path, "/panel/api/inbounds/catalog") {
mutations.Add(1)
_, _ = w.Write([]byte(`{"success":true}`))
return
}
w.WriteHeader(tc.status)
_, _ = w.Write([]byte(tc.body))
}))
t.Cleanup(srv.Close)
remote := NewRemote(nodeForPlainServer(t, srv, "verify", "tok"), nil)
inbound := &model.Inbound{Tag: "in-lookup", Protocol: model.VLESS, Port: 443, Settings: `{"clients":[]}`}
if err := remote.DelInbound(context.Background(), inbound); err == nil || !strings.Contains(err.Error(), tc.want) {
t.Errorf("DelInbound error = %v, want %q", err, tc.want)
}
if err := remote.UpdateInbound(context.Background(), inbound, inbound); err == nil || !strings.Contains(err.Error(), tc.want) {
t.Errorf("UpdateInbound error = %v, want %q", err, tc.want)
}
if got := mutations.Load(); got != 0 {
t.Errorf("mutation or fallback requests after failed lookup = %d, want 0", got)
}
})
}
}
func TestDelInboundPreservesCanceledContext(t *testing.T) {
srv, _ := newCountingNodeServer(t, `{"success":true}`)
remote := NewRemote(nodeForPlainServer(t, srv, "verify", "tok"), nil)
ctx, cancel := context.WithCancel(context.Background())
cancel()
inbound := &model.Inbound{Tag: "in-canceled", Protocol: model.VLESS}
if err := remote.DelInbound(ctx, inbound); !errors.Is(err, context.Canceled) {
t.Errorf("DelInbound error = %v, want canceled context", err)
}
}