client_sync_orphan_test.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. // A partial snapshot (node alive, still serving another client) authoritatively drops one;
  28. // the merge soft-orphans, recoverable until the grace elapses and the reaper confirms 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. const keep = "keep@x"
  36. createNodeInboundWithClient(t, db, 1, "n1-in", 41001, keep)
  37. bothSettings := fmt.Sprintf(`{"clients":[{"email":%q,"enable":true},{"email":%q,"enable":true}]}`, keep, email)
  38. syncNodeWithSettings(t, svc, 1, "n1-in", bothSettings,
  39. xray.ClientTraffic{Email: keep, Enable: true},
  40. xray.ClientTraffic{Email: email, Up: 5, Down: 5, Enable: true})
  41. if rec, traf := countClientRows(t, db, email); rec != 1 || traf != 1 {
  42. t.Fatalf("setup: clients=%d client_traffics=%d, want 1/1", rec, traf)
  43. }
  44. keepOnly := fmt.Sprintf(`{"clients":[{"email":%q,"enable":true}]}`, keep)
  45. if _, err := svc.setRemoteTrafficLocked(1, snapshotWithClients(t, "n1-in", keepOnly,
  46. xray.ClientTraffic{Email: keep, Enable: true}), false, false); err != nil {
  47. t.Fatalf("orphaning merge: %v", err)
  48. }
  49. if rec, traf := countClientRows(t, db, email); rec != 1 || traf != 1 {
  50. t.Fatalf("merge hard-deleted the client: clients=%d client_traffics=%d, want 1/1", rec, traf)
  51. }
  52. if readOrphanMark(t, db, email) <= 0 {
  53. t.Fatal("merge did not stamp sync_orphaned_at")
  54. }
  55. reaped, err := clientSvc.ReapSyncOrphans()
  56. if err != nil {
  57. t.Fatalf("reap inside grace: %v", err)
  58. }
  59. if reaped != 0 {
  60. t.Fatalf("reaped %d client(s) inside the grace period, want 0", reaped)
  61. }
  62. if rec, _ := countClientRows(t, db, email); rec != 1 {
  63. t.Fatal("client removed before the grace period elapsed")
  64. }
  65. backdateOrphanMark(t, db, email)
  66. reaped, err = clientSvc.ReapSyncOrphans()
  67. if err != nil {
  68. t.Fatalf("reap after grace: %v", err)
  69. }
  70. if reaped != 1 {
  71. t.Fatalf("reaped %d client(s) after the grace period, want 1", reaped)
  72. }
  73. rec, traf := countClientRows(t, db, email)
  74. if rec != 0 || traf != 0 {
  75. t.Fatalf("after reap: clients=%d client_traffics=%d, want 0/0", rec, traf)
  76. }
  77. }
  78. // A client the node reports again (partial snapshot) was never gone: clearing the mark
  79. // turns a bad merge into a recoverable blip instead of a delayed deletion.
  80. func TestSyncOrphanMarkClearedOnReattach(t *testing.T) {
  81. db := initTrafficTestDB(t)
  82. svc := &InboundService{}
  83. clientSvc := &ClientService{}
  84. seedNodeRow(t, db, &model.Node{Id: 1, Name: "n1", Address: "127.0.0.1", Port: 2096, ApiToken: "tok", Enable: true})
  85. const email = "flaky@x"
  86. const keep = "keep@x"
  87. createNodeInboundWithClient(t, db, 1, "n1-in", 41001, keep)
  88. bothSettings := fmt.Sprintf(`{"clients":[{"email":%q,"enable":true},{"email":%q,"enable":true}]}`, keep, email)
  89. syncNodeWithSettings(t, svc, 1, "n1-in", bothSettings,
  90. xray.ClientTraffic{Email: keep, Enable: true},
  91. xray.ClientTraffic{Email: email, Up: 5, Down: 5, Enable: true})
  92. keepOnly := fmt.Sprintf(`{"clients":[{"email":%q,"enable":true}]}`, keep)
  93. if _, err := svc.setRemoteTrafficLocked(1, snapshotWithClients(t, "n1-in", keepOnly,
  94. xray.ClientTraffic{Email: keep, Enable: true}), false, false); err != nil {
  95. t.Fatalf("orphaning merge: %v", err)
  96. }
  97. if readOrphanMark(t, db, email) <= 0 {
  98. t.Fatal("setup: expected the merge to mark the client")
  99. }
  100. syncNodeWithSettings(t, svc, 1, "n1-in", bothSettings,
  101. xray.ClientTraffic{Email: keep, Enable: true},
  102. xray.ClientTraffic{Email: email, Up: 6, Down: 6, Enable: true})
  103. if orphanedAt := readOrphanMark(t, db, email); orphanedAt != 0 {
  104. t.Fatalf("re-attached client kept its orphan mark: sync_orphaned_at=%d", orphanedAt)
  105. }
  106. backdateOrphanMark(t, db, email)
  107. if reaped, err := clientSvc.ReapSyncOrphans(); err != nil || reaped != 0 {
  108. t.Fatalf("reaped %d client(s) (err=%v) that the node still reports, want 0", reaped, err)
  109. }
  110. }
  111. // The reaper is scoped to the node sweep. Orphans from any other cause carry no
  112. // mark and keep their existing manual-cleanup semantics.
  113. func TestReapSyncOrphansIgnoresUnmarkedOrphans(t *testing.T) {
  114. db := initTrafficTestDB(t)
  115. clientSvc := &ClientService{}
  116. const email = "manual@x"
  117. rec := &model.ClientRecord{Email: email, Enable: true, UUID: "44444444-4444-4444-4444-444444444444"}
  118. if err := db.Create(rec).Error; err != nil {
  119. t.Fatalf("create client: %v", err)
  120. }
  121. reaped, err := clientSvc.ReapSyncOrphans()
  122. if err != nil {
  123. t.Fatalf("reap: %v", err)
  124. }
  125. if reaped != 0 {
  126. t.Fatalf("reaped %d unmarked orphan(s), want 0", reaped)
  127. }
  128. var surviving int64
  129. if err := db.Model(&model.ClientRecord{}).Where("email = ?", email).Count(&surviving).Error; err != nil {
  130. t.Fatalf("count clients: %v", err)
  131. }
  132. if surviving != 1 {
  133. t.Fatalf("unmarked orphan was reaped: %d rows survive, want 1", surviving)
  134. }
  135. }