sub_balancer_inbound_cleanup_test.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536
  1. package service
  2. import (
  3. "testing"
  4. "github.com/mhsanaei/3x-ui/v3/internal/database"
  5. "github.com/mhsanaei/3x-ui/v3/internal/database/model"
  6. )
  7. // Deleting an inbound that a sub-balancer selects must strip its id from
  8. // InboundIds, leaving no dangling member reference (#5648 mirrors the hosts
  9. // cascade). With the only member gone the balancer stops emitting a doc.
  10. func TestDelInboundClearsSubBalancerInboundIds(t *testing.T) {
  11. setupSubBalancerDB(t)
  12. ib := &model.Inbound{UserId: 1, Tag: "cleanup", Enable: false, Listen: "203.0.113.7", Port: 5001, Protocol: model.VLESS, Remark: "cleanup", Settings: `{}`, StreamSettings: `{}`}
  13. if err := database.GetDB().Create(ib).Error; err != nil {
  14. t.Fatalf("seed inbound: %v", err)
  15. }
  16. balSvc := &SubBalancerService{}
  17. bal, err := balSvc.Create(&model.SubBalancer{Remark: "bal", Strategy: "random", InboundIds: []int{ib.Id}, SortOrder: 1, Enabled: true})
  18. if err != nil {
  19. t.Fatalf("create balancer: %v", err)
  20. }
  21. if _, err := (&InboundService{}).DelInbound(ib.Id); err != nil {
  22. t.Fatalf("DelInbound: %v", err)
  23. }
  24. stored, err := balSvc.Get(bal.Id)
  25. if err != nil {
  26. t.Fatalf("get balancer: %v", err)
  27. }
  28. if len(stored.InboundIds) != 0 {
  29. t.Fatalf("InboundIds = %v, want empty (no dangling id)", stored.InboundIds)
  30. }
  31. }