client_sync_orphan_test.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. package service
  2. import (
  3. "fmt"
  4. "testing"
  5. "time"
  6. "github.com/mhsanaei/3x-ui/v3/internal/database/model"
  7. "github.com/mhsanaei/3x-ui/v3/internal/xray"
  8. "gorm.io/gorm"
  9. )
  10. func readOrphanMark(t *testing.T, db *gorm.DB, email string) int64 {
  11. t.Helper()
  12. var row model.ClientRecord
  13. if err := db.Where("email = ?", email).First(&row).Error; err != nil {
  14. t.Fatalf("read client %q: %v", email, err)
  15. }
  16. return row.SyncOrphanedAt
  17. }
  18. func backdateOrphanMark(t *testing.T, db *gorm.DB, email string) {
  19. t.Helper()
  20. past := time.Now().Add(-2 * syncOrphanReapGrace).UnixMilli()
  21. if err := db.Model(&model.ClientRecord{}).
  22. Where("email = ?", email).
  23. Update("sync_orphaned_at", past).Error; err != nil {
  24. t.Fatalf("backdate orphan mark: %v", err)
  25. }
  26. }
  27. // The merge must soft-orphan, not delete: everything stays recoverable until
  28. // the grace period has elapsed and the reaper confirms nothing reclaimed it.
  29. func TestSyncOrphanSurvivesMergeUntilGraceElapses(t *testing.T) {
  30. db := initTrafficTestDB(t)
  31. svc := &InboundService{}
  32. clientSvc := &ClientService{}
  33. seedNodeRow(t, db, &model.Node{Id: 1, Name: "n1", Address: "127.0.0.1", Port: 2096, ApiToken: "tok", Enable: true})
  34. const email = "gone@x"
  35. createNodeInboundWithClient(t, db, 1, "n1-in", 41001, email)
  36. settings := fmt.Sprintf(`{"clients":[{"email":%q,"enable":true}]}`, email)
  37. syncNodeWithSettings(t, svc, 1, "n1-in", settings,
  38. xray.ClientTraffic{Email: email, Up: 5, Down: 5, Enable: true})
  39. if rec, traf := countClientRows(t, db, email); rec != 1 || traf != 1 {
  40. t.Fatalf("setup: clients=%d client_traffics=%d, want 1/1", rec, traf)
  41. }
  42. if _, err := svc.setRemoteTrafficLocked(1, snapshotWithoutClients(t, "n1-in"), false); err != nil {
  43. t.Fatalf("orphaning merge: %v", err)
  44. }
  45. if rec, traf := countClientRows(t, db, email); rec != 1 || traf != 1 {
  46. t.Fatalf("merge hard-deleted the client: clients=%d client_traffics=%d, want 1/1", rec, traf)
  47. }
  48. if readOrphanMark(t, db, email) <= 0 {
  49. t.Fatal("merge did not stamp sync_orphaned_at")
  50. }
  51. reaped, err := clientSvc.ReapSyncOrphans()
  52. if err != nil {
  53. t.Fatalf("reap inside grace: %v", err)
  54. }
  55. if reaped != 0 {
  56. t.Fatalf("reaped %d client(s) inside the grace period, want 0", reaped)
  57. }
  58. if rec, _ := countClientRows(t, db, email); rec != 1 {
  59. t.Fatal("client removed before the grace period elapsed")
  60. }
  61. backdateOrphanMark(t, db, email)
  62. reaped, err = clientSvc.ReapSyncOrphans()
  63. if err != nil {
  64. t.Fatalf("reap after grace: %v", err)
  65. }
  66. if reaped != 1 {
  67. t.Fatalf("reaped %d client(s) after the grace period, want 1", reaped)
  68. }
  69. rec, traf := countClientRows(t, db, email)
  70. if rec != 0 || traf != 0 {
  71. t.Fatalf("after reap: clients=%d client_traffics=%d, want 0/0", rec, traf)
  72. }
  73. }
  74. // A client the node reports again was never gone: clearing the mark is what
  75. // turns a bad merge into a recoverable blip instead of a delayed deletion.
  76. func TestSyncOrphanMarkClearedOnReattach(t *testing.T) {
  77. db := initTrafficTestDB(t)
  78. svc := &InboundService{}
  79. clientSvc := &ClientService{}
  80. seedNodeRow(t, db, &model.Node{Id: 1, Name: "n1", Address: "127.0.0.1", Port: 2096, ApiToken: "tok", Enable: true})
  81. const email = "flaky@x"
  82. createNodeInboundWithClient(t, db, 1, "n1-in", 41001, email)
  83. settings := fmt.Sprintf(`{"clients":[{"email":%q,"enable":true}]}`, email)
  84. syncNodeWithSettings(t, svc, 1, "n1-in", settings,
  85. xray.ClientTraffic{Email: email, Up: 5, Down: 5, Enable: true})
  86. if _, err := svc.setRemoteTrafficLocked(1, snapshotWithoutClients(t, "n1-in"), false); err != nil {
  87. t.Fatalf("orphaning merge: %v", err)
  88. }
  89. if readOrphanMark(t, db, email) <= 0 {
  90. t.Fatal("setup: expected the merge to mark the client")
  91. }
  92. syncNodeWithSettings(t, svc, 1, "n1-in", settings,
  93. xray.ClientTraffic{Email: email, Up: 6, Down: 6, Enable: true})
  94. if orphanedAt := readOrphanMark(t, db, email); orphanedAt != 0 {
  95. t.Fatalf("re-attached client kept its orphan mark: sync_orphaned_at=%d", orphanedAt)
  96. }
  97. backdateOrphanMark(t, db, email)
  98. if reaped, err := clientSvc.ReapSyncOrphans(); err != nil || reaped != 0 {
  99. t.Fatalf("reaped %d client(s) (err=%v) that the node still reports, want 0", reaped, err)
  100. }
  101. }
  102. // The reaper is scoped to the node sweep. Orphans from any other cause carry no
  103. // mark and keep their existing manual-cleanup semantics.
  104. func TestReapSyncOrphansIgnoresUnmarkedOrphans(t *testing.T) {
  105. db := initTrafficTestDB(t)
  106. clientSvc := &ClientService{}
  107. const email = "manual@x"
  108. rec := &model.ClientRecord{Email: email, Enable: true, UUID: "44444444-4444-4444-4444-444444444444"}
  109. if err := db.Create(rec).Error; err != nil {
  110. t.Fatalf("create client: %v", err)
  111. }
  112. reaped, err := clientSvc.ReapSyncOrphans()
  113. if err != nil {
  114. t.Fatalf("reap: %v", err)
  115. }
  116. if reaped != 0 {
  117. t.Fatalf("reaped %d unmarked orphan(s), want 0", reaped)
  118. }
  119. var surviving int64
  120. if err := db.Model(&model.ClientRecord{}).Where("email = ?", email).Count(&surviving).Error; err != nil {
  121. t.Fatalf("count clients: %v", err)
  122. }
  123. if surviving != 1 {
  124. t.Fatalf("unmarked orphan was reaped: %d rows survive, want 1", surviving)
  125. }
  126. }