node_client_traffic_sum_test.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. package service
  2. import (
  3. "fmt"
  4. "path/filepath"
  5. "testing"
  6. "github.com/mhsanaei/3x-ui/v3/internal/database"
  7. "github.com/mhsanaei/3x-ui/v3/internal/database/model"
  8. "github.com/mhsanaei/3x-ui/v3/internal/web/runtime"
  9. "github.com/mhsanaei/3x-ui/v3/internal/xray"
  10. "gorm.io/gorm"
  11. )
  12. func initTrafficTestDB(t *testing.T) *gorm.DB {
  13. t.Helper()
  14. dbDir := t.TempDir()
  15. t.Setenv("XUI_DB_FOLDER", dbDir)
  16. if err := database.InitDB(filepath.Join(dbDir, "x-ui.db")); err != nil {
  17. t.Fatalf("InitDB: %v", err)
  18. }
  19. t.Cleanup(func() { _ = database.CloseDB() })
  20. return database.GetDB()
  21. }
  22. func createNodeInbound(t *testing.T, db *gorm.DB, nodeID int, tag string, port int) {
  23. t.Helper()
  24. nid := nodeID
  25. ib := &model.Inbound{UserId: 1, Tag: tag, Enable: true, Port: port, Protocol: model.VLESS, NodeID: &nid}
  26. if err := db.Create(ib).Error; err != nil {
  27. t.Fatalf("create node inbound %q: %v", tag, err)
  28. }
  29. }
  30. // createNodeInboundWithClient mirrors createNodeInbound but stores the client
  31. // in the settings JSON so emailUsedByOtherInbounds can see the attachment.
  32. func createNodeInboundWithClient(t *testing.T, db *gorm.DB, nodeID int, tag string, port int, email string) {
  33. t.Helper()
  34. nid := nodeID
  35. settings := fmt.Sprintf(`{"clients": [{"email": %q, "enable": true}]}`, email)
  36. ib := &model.Inbound{UserId: 1, Tag: tag, Enable: true, Port: port, Protocol: model.VLESS, NodeID: &nid, Settings: settings}
  37. if err := db.Create(ib).Error; err != nil {
  38. t.Fatalf("create node inbound %q: %v", tag, err)
  39. }
  40. }
  41. func syncNode(t *testing.T, svc *InboundService, nodeID int, tag string, stats ...xray.ClientTraffic) {
  42. t.Helper()
  43. snap := &runtime.TrafficSnapshot{
  44. Inbounds: []*model.Inbound{{Tag: tag, ClientStats: stats}},
  45. }
  46. if _, err := svc.setRemoteTrafficLocked(nodeID, snap, false); err != nil {
  47. t.Fatalf("setRemoteTrafficLocked node %d: %v", nodeID, err)
  48. }
  49. }
  50. // syncNodeWithSettings mirrors syncNode but carries a real settings JSON on
  51. // the snapshot inbound, like production nodes do — the sync mirrors snapshot
  52. // settings onto the central row, and the shared-accumulator guard reads the
  53. // clients out of those settings.
  54. func syncNodeWithSettings(t *testing.T, svc *InboundService, nodeID int, tag, settings string, stats ...xray.ClientTraffic) {
  55. t.Helper()
  56. snap := &runtime.TrafficSnapshot{
  57. Inbounds: []*model.Inbound{{Tag: tag, Settings: settings, ClientStats: stats}},
  58. }
  59. if _, err := svc.setRemoteTrafficLocked(nodeID, snap, false); err != nil {
  60. t.Fatalf("setRemoteTrafficLocked node %d: %v", nodeID, err)
  61. }
  62. }
  63. func readTraffic(t *testing.T, db *gorm.DB, email string) xray.ClientTraffic {
  64. t.Helper()
  65. var ct xray.ClientTraffic
  66. if err := db.Model(xray.ClientTraffic{}).Where("email = ?", email).First(&ct).Error; err != nil {
  67. t.Fatalf("read client_traffics %q: %v", email, err)
  68. }
  69. return ct
  70. }
  71. func assertUpDown(t *testing.T, ct xray.ClientTraffic, wantUp, wantDown int64, when string) {
  72. t.Helper()
  73. if ct.Up != wantUp || ct.Down != wantDown {
  74. t.Errorf("%s: up=%d down=%d, want %d/%d", when, ct.Up, ct.Down, wantUp, wantDown)
  75. }
  76. }
  77. func TestTwoNodesShareEmail_SumsCorrectly(t *testing.T) {
  78. db := initTrafficTestDB(t)
  79. createNodeInbound(t, db, 1, "n1-in", 41001)
  80. createNodeInbound(t, db, 2, "n2-in", 41002)
  81. svc := &InboundService{}
  82. const email = "shared"
  83. // First-ever sync from each node: the row is created at 0 and the current
  84. // node counters become the baseline. Only deltas beyond those baselines count.
  85. syncNode(t, svc, 1, "n1-in", xray.ClientTraffic{Email: email, Up: 100, Down: 100, Enable: true})
  86. syncNode(t, svc, 2, "n2-in", xray.ClientTraffic{Email: email, Up: 200, Down: 200, Enable: true})
  87. assertUpDown(t, readTraffic(t, db, email), 0, 0, "after baselines — historical node traffic not imported")
  88. syncNode(t, svc, 1, "n1-in", xray.ClientTraffic{Email: email, Up: 150, Down: 150, Enable: true})
  89. syncNode(t, svc, 2, "n2-in", xray.ClientTraffic{Email: email, Up: 260, Down: 260, Enable: true})
  90. assertUpDown(t, readTraffic(t, db, email), 110, 110, "after both nodes grow — deltas (50+60) accrue")
  91. }
  92. func TestSingleNode_MirrorsCorrectly(t *testing.T) {
  93. db := initTrafficTestDB(t)
  94. createNodeInbound(t, db, 1, "n1-in", 41001)
  95. svc := &InboundService{}
  96. const email = "solo"
  97. // First sync: row seeded at 0, current counter becomes the baseline.
  98. syncNode(t, svc, 1, "n1-in", xray.ClientTraffic{Email: email, Up: 500, Down: 600, Enable: true})
  99. assertUpDown(t, readTraffic(t, db, email), 0, 0, "first sync — historical traffic not imported")
  100. // Second sync: delta (700-500=200 / 800-600=200) accrues normally.
  101. syncNode(t, svc, 1, "n1-in", xray.ClientTraffic{Email: email, Up: 700, Down: 800, Enable: true})
  102. assertUpDown(t, readTraffic(t, db, email), 200, 200, "second sync — delta accrues")
  103. }
  104. func TestUpgrade_PreExistingRow_NoDoubleCount(t *testing.T) {
  105. db := initTrafficTestDB(t)
  106. createNodeInbound(t, db, 1, "n1-in", 41001)
  107. svc := &InboundService{}
  108. const email = "legacy"
  109. var ib model.Inbound
  110. if err := db.Where("tag = ?", "n1-in").First(&ib).Error; err != nil {
  111. t.Fatalf("load inbound: %v", err)
  112. }
  113. if err := db.Create(&xray.ClientTraffic{InboundId: ib.Id, Email: email, Up: 1000, Down: 2000, Enable: true}).Error; err != nil {
  114. t.Fatalf("seed pre-existing row: %v", err)
  115. }
  116. syncNode(t, svc, 1, "n1-in", xray.ClientTraffic{Email: email, Up: 1000, Down: 2000, Enable: true})
  117. assertUpDown(t, readTraffic(t, db, email), 1000, 2000, "first snapshot must not double-count")
  118. syncNode(t, svc, 1, "n1-in", xray.ClientTraffic{Email: email, Up: 1100, Down: 2100, Enable: true})
  119. assertUpDown(t, readTraffic(t, db, email), 1100, 2100, "growth after upgrade accrues")
  120. }
  121. // TestGhostData_NoPhantomTraffic reproduces the 50 GB phantom traffic bug:
  122. // a node retains stale data for a deleted client (e.g. de-1 was offline during
  123. // deletion). When the master first syncs this email again it must NOT import the
  124. // stale counter — it sets Up=0 and records the current node value as the
  125. // baseline so only future deltas count.
  126. func TestGhostData_NoPhantomTraffic(t *testing.T) {
  127. db := initTrafficTestDB(t)
  128. createNodeInbound(t, db, 1, "de1-in", 41001)
  129. svc := &InboundService{}
  130. const email = "pouya"
  131. const staleBytes int64 = 50 * 1024 * 1024 * 1024 // 50 GB stale on de-1
  132. // Node reports a client with a large pre-existing counter (ghost data from a
  133. // deleted account that survived on the node). Master has no row for this email.
  134. syncNode(t, svc, 1, "de1-in", xray.ClientTraffic{Email: email, Up: staleBytes, Down: staleBytes, Enable: true})
  135. ct := readTraffic(t, db, email)
  136. if ct.Up >= staleBytes || ct.Down >= staleBytes {
  137. t.Errorf("ghost data imported: up=%d down=%d; stale node counter must not count as new traffic", ct.Up, ct.Down)
  138. }
  139. assertUpDown(t, ct, 0, 0, "first sync of ghost email — imported as 0, not 50 GB")
  140. // Subsequent syncs only add incremental usage beyond the baseline.
  141. syncNode(t, svc, 1, "de1-in", xray.ClientTraffic{Email: email, Up: staleBytes + 1024, Down: staleBytes + 2048, Enable: true})
  142. assertUpDown(t, readTraffic(t, db, email), 1024, 2048, "only incremental traffic beyond baseline counts")
  143. }
  144. func TestNodeCounterReset_Clamped(t *testing.T) {
  145. db := initTrafficTestDB(t)
  146. createNodeInbound(t, db, 1, "n1-in", 41001)
  147. svc := &InboundService{}
  148. const email = "restart"
  149. // First sync seeds at 0 (baseline=900). Second sync adds delta 950-900=50.
  150. syncNode(t, svc, 1, "n1-in", xray.ClientTraffic{Email: email, Up: 900, Down: 900, Enable: true})
  151. syncNode(t, svc, 1, "n1-in", xray.ClientTraffic{Email: email, Up: 950, Down: 950, Enable: true})
  152. assertUpDown(t, readTraffic(t, db, email), 50, 50, "before node reset")
  153. // Counter resets to 50 (Xray restart). delta=50-950=-900 → clamped → adds 50.
  154. syncNode(t, svc, 1, "n1-in", xray.ClientTraffic{Email: email, Up: 50, Down: 50, Enable: true})
  155. ct := readTraffic(t, db, email)
  156. if ct.Up < 0 || ct.Down < 0 {
  157. t.Fatalf("row went negative after node reset: up=%d down=%d", ct.Up, ct.Down)
  158. }
  159. assertUpDown(t, ct, 100, 100, "after node counter reset (clamped)")
  160. }
  161. func TestCentralReset_NoReAdd(t *testing.T) {
  162. db := initTrafficTestDB(t)
  163. createNodeInbound(t, db, 1, "n1-in", 41001)
  164. createNodeInbound(t, db, 2, "n2-in", 41002)
  165. svc := &InboundService{}
  166. const email = "reset"
  167. syncNode(t, svc, 1, "n1-in", xray.ClientTraffic{Email: email, Up: 100, Down: 100, Enable: true})
  168. syncNode(t, svc, 2, "n2-in", xray.ClientTraffic{Email: email, Up: 100, Down: 100, Enable: true})
  169. syncNode(t, svc, 1, "n1-in", xray.ClientTraffic{Email: email, Up: 200, Down: 200, Enable: true})
  170. syncNode(t, svc, 2, "n2-in", xray.ClientTraffic{Email: email, Up: 200, Down: 200, Enable: true})
  171. if err := db.Model(xray.ClientTraffic{}).Where("email = ?", email).
  172. Updates(map[string]any{"up": 0, "down": 0}).Error; err != nil {
  173. t.Fatalf("simulate central reset: %v", err)
  174. }
  175. syncNode(t, svc, 1, "n1-in", xray.ClientTraffic{Email: email, Up: 210, Down: 210, Enable: true})
  176. syncNode(t, svc, 2, "n2-in", xray.ClientTraffic{Email: email, Up: 205, Down: 205, Enable: true})
  177. assertUpDown(t, readTraffic(t, db, email), 15, 15, "after central reset only increments accrue")
  178. }
  179. func TestInboundRemoval_KeepsSharedEmailRow(t *testing.T) {
  180. db := initTrafficTestDB(t)
  181. createNodeInboundWithClient(t, db, 1, "n1-in", 41001, "shared")
  182. createNodeInboundWithClient(t, db, 2, "n2-in", 41002, "shared")
  183. svc := &InboundService{}
  184. const email = "shared"
  185. settings := fmt.Sprintf(`{"clients": [{"email": %q, "enable": true}]}`, email)
  186. // Baselines set: node1=100, node2=200. Row seeded at 0.
  187. syncNodeWithSettings(t, svc, 1, "n1-in", settings, xray.ClientTraffic{Email: email, Up: 100, Down: 100, Enable: true})
  188. syncNodeWithSettings(t, svc, 2, "n2-in", settings, xray.ClientTraffic{Email: email, Up: 200, Down: 200, Enable: true})
  189. // node1 delta=50, node2 delta=60 → total=110.
  190. syncNodeWithSettings(t, svc, 1, "n1-in", settings, xray.ClientTraffic{Email: email, Up: 150, Down: 150, Enable: true})
  191. syncNodeWithSettings(t, svc, 2, "n2-in", settings, xray.ClientTraffic{Email: email, Up: 260, Down: 260, Enable: true})
  192. assertUpDown(t, readTraffic(t, db, email), 110, 110, "baseline sum")
  193. // Node 1 rebuilt (reinstall / another master's reconcile): its inbound
  194. // vanishes from the snapshot. The shared accumulator must survive — losing
  195. // it would let the next node sync re-seed the row with that node's counter
  196. // alone, showing only the last panel's number instead of the sum.
  197. if _, err := svc.setRemoteTrafficLocked(1, &runtime.TrafficSnapshot{}, false); err != nil {
  198. t.Fatalf("sync node 1 with empty snapshot: %v", err)
  199. }
  200. assertUpDown(t, readTraffic(t, db, email), 110, 110, "after node 1 inbound removal")
  201. // Node 2 keeps accruing onto the surviving row. node2 delta=300-260=40 → 150.
  202. syncNodeWithSettings(t, svc, 2, "n2-in", settings, xray.ClientTraffic{Email: email, Up: 300, Down: 300, Enable: true})
  203. assertUpDown(t, readTraffic(t, db, email), 150, 150, "after node 2 grows")
  204. }
  205. func TestClientGoneFromOneNode_KeepsSharedEmailRow(t *testing.T) {
  206. db := initTrafficTestDB(t)
  207. createNodeInboundWithClient(t, db, 1, "n1-in", 41001, "shared")
  208. createNodeInboundWithClient(t, db, 2, "n2-in", 41002, "shared")
  209. svc := &InboundService{}
  210. const email = "shared"
  211. settings := fmt.Sprintf(`{"clients": [{"email": %q, "enable": true}]}`, email)
  212. // First syncs set baselines (node1=100, node2=200). Row seeded at 0.
  213. syncNodeWithSettings(t, svc, 1, "n1-in", settings, xray.ClientTraffic{Email: email, Up: 100, Down: 100, Enable: true})
  214. syncNodeWithSettings(t, svc, 2, "n2-in", settings, xray.ClientTraffic{Email: email, Up: 200, Down: 200, Enable: true})
  215. // Client detached from node 1's inbound only: its stats vanish from that
  216. // inbound's snapshot while node 2 still hosts the email.
  217. syncNodeWithSettings(t, svc, 1, "n1-in", `{"clients": []}`)
  218. assertUpDown(t, readTraffic(t, db, email), 0, 0, "after client left node 1 — row unchanged at 0")
  219. // Node 2 delta: 240-200=40 → accrues to 40.
  220. syncNodeWithSettings(t, svc, 2, "n2-in", settings, xray.ClientTraffic{Email: email, Up: 240, Down: 240, Enable: true})
  221. assertUpDown(t, readTraffic(t, db, email), 40, 40, "node 2 keeps accruing")
  222. }
  223. // TestStatsUnderSiblingInbound_KeepsNodeBaseline reproduces the recurring
  224. // sweep bug behind #5202: the client is attached to two inbounds of the SAME
  225. // node, the node reports its stats under n1-a only, but the master-side row
  226. // is owned by n1-b's mirror. The per-email sweep for n1-b must not drop the
  227. // (node, email) baseline that n1-a's delta computation needs — doing so every
  228. // cycle froze the client's counter permanently.
  229. func TestStatsUnderSiblingInbound_KeepsNodeBaseline(t *testing.T) {
  230. db := initTrafficTestDB(t)
  231. createNodeInboundWithClient(t, db, 1, "n1-a", 41001, "fresh")
  232. createNodeInbound(t, db, 1, "n1-b", 41002)
  233. svc := &InboundService{}
  234. const email = "fresh"
  235. var ibB model.Inbound
  236. if err := db.Where("tag = ?", "n1-b").First(&ibB).Error; err != nil {
  237. t.Fatalf("load n1-b: %v", err)
  238. }
  239. // Master-side row created when the client was added on the panel, owned by
  240. // n1-b's mirror (e.g. the client form targeted that inbound).
  241. if err := db.Create(&xray.ClientTraffic{InboundId: ibB.Id, Email: email, Enable: true}).Error; err != nil {
  242. t.Fatalf("seed master row: %v", err)
  243. }
  244. settings := fmt.Sprintf(`{"clients": [{"email": %q, "enable": true}]}`, email)
  245. sync := func(up, down int64) {
  246. t.Helper()
  247. snap := &runtime.TrafficSnapshot{Inbounds: []*model.Inbound{
  248. {Tag: "n1-a", Settings: settings, ClientStats: []xray.ClientTraffic{{Email: email, Up: up, Down: down, Enable: true}}},
  249. {Tag: "n1-b", Settings: `{"clients": []}`},
  250. }}
  251. if _, err := svc.setRemoteTrafficLocked(1, snap, false); err != nil {
  252. t.Fatalf("sync: %v", err)
  253. }
  254. }
  255. sync(630, 630)
  256. var baselines int64
  257. if err := db.Model(&model.NodeClientTraffic{}).Where("node_id = ? AND email = ?", 1, email).Count(&baselines).Error; err != nil {
  258. t.Fatalf("count baselines: %v", err)
  259. }
  260. if baselines != 1 {
  261. t.Fatalf("baseline must survive the sibling-inbound sweep, found %d rows", baselines)
  262. }
  263. sync(700, 700)
  264. assertUpDown(t, readTraffic(t, db, email), 70, 70, "delta accrues once baseline survives")
  265. }
  266. // TestMultiAttach_SameNode_DivergentSiblings reproduces #5274: a client is
  267. // attached to several inbounds of the SAME node. Xray reports client traffic
  268. // globally per email, so the node's enriched inbound list copies one shared
  269. // counter onto every inbound the client is on. When those copies diverge — a
  270. // legacy per-inbound row surviving the v3.2.x→v3.3.x upgrade, or any drift —
  271. // the per-inbound delta loop used to treat the lower sibling as a node-counter
  272. // reset and re-add its full value, inflating the client far past real usage.
  273. // The merge must collapse the email to its node-wide total and count it once.
  274. func TestMultiAttach_SameNode_DivergentSiblings(t *testing.T) {
  275. db := initTrafficTestDB(t)
  276. createNodeInboundWithClient(t, db, 1, "n1-a", 41001, "multi")
  277. createNodeInboundWithClient(t, db, 1, "n1-b", 41002, "multi")
  278. createNodeInboundWithClient(t, db, 1, "n1-c", 41003, "multi")
  279. svc := &InboundService{}
  280. const email = "multi"
  281. settings := fmt.Sprintf(`{"clients": [{"email": %q, "enable": true}]}`, email)
  282. // The three inbounds report the same email with diverging values; the
  283. // node's true per-email total is the largest (the shared global counter).
  284. sync := func(a, b, c int64) {
  285. t.Helper()
  286. snap := &runtime.TrafficSnapshot{Inbounds: []*model.Inbound{
  287. {Tag: "n1-a", Settings: settings, ClientStats: []xray.ClientTraffic{{Email: email, Up: a, Down: a, Enable: true}}},
  288. {Tag: "n1-b", Settings: settings, ClientStats: []xray.ClientTraffic{{Email: email, Up: b, Down: b, Enable: true}}},
  289. {Tag: "n1-c", Settings: settings, ClientStats: []xray.ClientTraffic{{Email: email, Up: c, Down: c, Enable: true}}},
  290. }}
  291. if _, err := svc.setRemoteTrafficLocked(1, snap, false); err != nil {
  292. t.Fatalf("sync: %v", err)
  293. }
  294. }
  295. // First-ever sync: row seeded at 0, canon (max of siblings = 100) becomes the
  296. // baseline. The node total is NOT imported as historical traffic — this prevents
  297. // ghost data from a previously-deleted account being counted as real usage.
  298. sync(100, 50, 80)
  299. assertUpDown(t, readTraffic(t, db, email), 0, 0, "first sync seeds at 0 — not the sum (230) nor the max (100)")
  300. // Second sync: canon grew from 100→150, delta=50 accrues. Siblings 60 and 90
  301. // do not re-add their full values — the canon clamp prevents inflation.
  302. sync(150, 60, 90)
  303. assertUpDown(t, readTraffic(t, db, email), 50, 50, "second sync: only the 50-unit increment counts, not every sibling")
  304. // Equal siblings (the healthy current-schema case): canon grew from 150→200,
  305. // delta=50 accrues once.
  306. sync(200, 200, 200)
  307. assertUpDown(t, readTraffic(t, db, email), 100, 100, "equal siblings accrue the single increment")
  308. }
  309. func TestDelClientStat_CleansNodeBaselines(t *testing.T) {
  310. db := initTrafficTestDB(t)
  311. svc := &InboundService{}
  312. const email = "gone"
  313. if err := db.Create(&xray.ClientTraffic{InboundId: 1, Email: email, Enable: true}).Error; err != nil {
  314. t.Fatalf("seed client_traffics: %v", err)
  315. }
  316. if err := db.Create(&model.NodeClientTraffic{NodeId: 1, Email: email, Up: 10, Down: 10}).Error; err != nil {
  317. t.Fatalf("seed node baseline 1: %v", err)
  318. }
  319. if err := db.Create(&model.NodeClientTraffic{NodeId: 2, Email: email, Up: 20, Down: 20}).Error; err != nil {
  320. t.Fatalf("seed node baseline 2: %v", err)
  321. }
  322. if err := svc.DelClientStat(db, email); err != nil {
  323. t.Fatalf("DelClientStat: %v", err)
  324. }
  325. var cnt int64
  326. if err := db.Model(&model.NodeClientTraffic{}).Where("email = ?", email).Count(&cnt).Error; err != nil {
  327. t.Fatalf("count baselines: %v", err)
  328. }
  329. if cnt != 0 {
  330. t.Errorf("expected node baselines cleaned, found %d", cnt)
  331. }
  332. }
  333. func TestNodeDelete_CleansNodeBaselines(t *testing.T) {
  334. db := initTrafficTestDB(t)
  335. nodeSvc := NodeService{}
  336. if err := db.Create(&model.NodeClientTraffic{NodeId: 7, Email: "a", Up: 1, Down: 1}).Error; err != nil {
  337. t.Fatalf("seed node 7 a: %v", err)
  338. }
  339. if err := db.Create(&model.NodeClientTraffic{NodeId: 7, Email: "b", Up: 2, Down: 2}).Error; err != nil {
  340. t.Fatalf("seed node 7 b: %v", err)
  341. }
  342. if err := db.Create(&model.NodeClientTraffic{NodeId: 8, Email: "c", Up: 3, Down: 3}).Error; err != nil {
  343. t.Fatalf("seed node 8 c: %v", err)
  344. }
  345. if err := nodeSvc.Delete(7); err != nil {
  346. t.Fatalf("NodeService.Delete(7): %v", err)
  347. }
  348. var sevenCnt, eightCnt int64
  349. db.Model(&model.NodeClientTraffic{}).Where("node_id = ?", 7).Count(&sevenCnt)
  350. db.Model(&model.NodeClientTraffic{}).Where("node_id = ?", 8).Count(&eightCnt)
  351. if sevenCnt != 0 {
  352. t.Errorf("node 7 baselines not cleaned: %d remain", sevenCnt)
  353. }
  354. if eightCnt != 1 {
  355. t.Errorf("node 8 baseline should survive, found %d", eightCnt)
  356. }
  357. }