Files
3x-ui/internal/web/service/client_delete_continue_test.go
T
MHSanaei 6aa87f4e57 fix(clients): finish deleting from every inbound when one fails
Delete aborted its per-inbound loop on the first error, so a client
attached to inbounds across several nodes lost at most one per attempt:
the loop never reached the remaining nodes, the record cleanup after
the loop never ran, and each retry started over with whatever was left.
Operators with many nodes had to delete the same client once per node.

Collect per-inbound failures and keep going so every reachable inbound
and node is cleaned in a single pass, then keep the client record only
when something failed - its settings JSON still holds the client there,
so the next delete retries exactly the leftovers - and return the
joined failures instead of silently reporting success. DeleteByEmail's
legacy fallback loop gets the same treatment.

Closes #5845
2026-07-11 22:48:57 +02:00

65 lines
2.1 KiB
Go

package service
import (
"strconv"
"strings"
"testing"
"github.com/mhsanaei/3x-ui/v3/internal/database"
"github.com/mhsanaei/3x-ui/v3/internal/database/model"
)
func TestDeleteContinuesPastFailedInbound(t *testing.T) {
setupBulkDB(t)
svc := &ClientService{}
inboundSvc := &InboundService{}
source := []model.Client{{Email: "spread@x", ID: "cccccccc-0000-0000-0000-000000000001", SubID: "sub-spread", Enable: true}}
ib1 := mkInbound(t, 23001, model.VLESS, clientsSettings(t, source))
ib2 := mkInbound(t, 23002, model.VLESS, clientsSettings(t, source))
ib3 := mkInbound(t, 23003, model.VLESS, clientsSettings(t, source))
for _, ib := range []*model.Inbound{ib1, ib2, ib3} {
if err := svc.SyncInbound(nil, ib.Id, source); err != nil {
t.Fatalf("seed linkage for %d: %v", ib.Id, err)
}
}
rec := lookupClientRecord(t, "spread@x")
missingNode := 9999
if err := database.GetDB().Model(&model.Inbound{}).Where("id = ?", ib2.Id).
Update("node_id", missingNode).Error; err != nil {
t.Fatalf("point inbound 2 at a missing node: %v", err)
}
_, err := svc.Delete(inboundSvc, rec.Id, false)
if err == nil {
t.Fatalf("Delete with a failing inbound succeeded, want error")
}
if !strings.Contains(err.Error(), "inbound "+strconv.Itoa(ib2.Id)) {
t.Fatalf("Delete error = %q, want it to name inbound %d", err, ib2.Id)
}
for _, ib := range []*model.Inbound{ib1, ib3} {
if settingsHoldUUID(t, inboundSvc, ib.Id, "spread@x") {
t.Fatalf("inbound %d still holds the client after Delete", ib.Id)
}
}
if _, err := svc.GetByID(rec.Id); err != nil {
t.Fatalf("record removed despite a failed inbound: %v", err)
}
if err := database.GetDB().Model(&model.Inbound{}).Where("id = ?", ib2.Id).
Update("node_id", nil).Error; err != nil {
t.Fatalf("repair inbound 2: %v", err)
}
if _, err := svc.Delete(inboundSvc, rec.Id, false); err != nil {
t.Fatalf("retry Delete: %v", err)
}
if _, err := svc.GetByID(rec.Id); err == nil {
t.Fatalf("record still present after successful retry")
}
if settingsHoldUUID(t, inboundSvc, ib2.Id, "spread@x") {
t.Fatalf("inbound 2 still holds the client after retry")
}
}