1
0

inbound_create_race_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. package service
  2. import (
  3. "fmt"
  4. "sync"
  5. "testing"
  6. "github.com/mhsanaei/3x-ui/v3/internal/database"
  7. "github.com/mhsanaei/3x-ui/v3/internal/database/model"
  8. )
  9. // A wildcard listener and a specific one on the same port overlap, but they are
  10. // two different rows: only the in-transaction check can reject the pair, and it
  11. // can only do so if the check and the insert cannot interleave.
  12. func TestAddInboundConcurrentOverlappingListenersSingleWinner(t *testing.T) {
  13. setupConflictDB(t)
  14. const rounds = 25
  15. for round := range rounds {
  16. port := 24000 + round
  17. claims := []*model.Inbound{
  18. {
  19. Tag: fmt.Sprintf("race-%d-wildcard", round), Listen: "",
  20. Port: port, Protocol: model.VLESS,
  21. StreamSettings: `{"network":"tcp"}`, Settings: `{"clients":[]}`,
  22. },
  23. {
  24. Tag: fmt.Sprintf("race-%d-specific", round), Listen: "127.0.0.1",
  25. Port: port, Protocol: model.Trojan,
  26. StreamSettings: `{"network":"tcp"}`, Settings: `{"clients":[]}`,
  27. },
  28. }
  29. start := make(chan struct{})
  30. errs := make(chan error, len(claims))
  31. var wg sync.WaitGroup
  32. for _, claim := range claims {
  33. wg.Add(1)
  34. go func(inbound *model.Inbound) {
  35. defer wg.Done()
  36. <-start
  37. _, _, err := (&InboundService{}).AddInbound(inbound)
  38. errs <- err
  39. }(claim)
  40. }
  41. close(start)
  42. wg.Wait()
  43. close(errs)
  44. committed := 0
  45. rejections := make([]string, 0, len(claims))
  46. for err := range errs {
  47. if err == nil {
  48. committed++
  49. continue
  50. }
  51. rejections = append(rejections, err.Error())
  52. }
  53. if committed != 1 {
  54. t.Fatalf("round %d port %d: concurrent AddInbound committed=%d, want exactly 1 (rejections: %v)",
  55. round, port, committed, rejections)
  56. }
  57. }
  58. }
  59. // Editing an inbound onto a port another one already holds must be rejected —
  60. // the check moved inside the transaction, and nothing else guards this path.
  61. func TestUpdateInboundRejectsPortTakenByAnother(t *testing.T) {
  62. setupConflictDB(t)
  63. svc := &InboundService{}
  64. first := &model.Inbound{
  65. Tag: "update-holder", Listen: "", Port: 25101, Protocol: model.VLESS,
  66. StreamSettings: `{"network":"tcp"}`, Settings: `{"clients":[]}`,
  67. }
  68. if _, _, err := svc.AddInbound(first); err != nil {
  69. t.Fatalf("seed holder: %v", err)
  70. }
  71. second := &model.Inbound{
  72. Tag: "update-mover", Listen: "", Port: 25102, Protocol: model.VLESS,
  73. StreamSettings: `{"network":"tcp"}`, Settings: `{"clients":[]}`,
  74. }
  75. if _, _, err := svc.AddInbound(second); err != nil {
  76. t.Fatalf("seed mover: %v", err)
  77. }
  78. second.Port = first.Port
  79. if _, _, err := svc.UpdateInbound(second); err == nil {
  80. t.Fatal("moving an inbound onto a port already in use was accepted")
  81. }
  82. var stored model.Inbound
  83. if err := database.GetDB().First(&stored, second.Id).Error; err != nil {
  84. t.Fatal(err)
  85. }
  86. if stored.Port != 25102 {
  87. t.Fatalf("rejected update still changed the stored port to %d", stored.Port)
  88. }
  89. }