node_traffic_sync_online_prune_test.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. package job
  2. import (
  3. "path/filepath"
  4. "testing"
  5. "github.com/op/go-logging"
  6. "github.com/mhsanaei/3x-ui/v3/internal/database"
  7. "github.com/mhsanaei/3x-ui/v3/internal/database/dbtest"
  8. "github.com/mhsanaei/3x-ui/v3/internal/database/model"
  9. xuilogger "github.com/mhsanaei/3x-ui/v3/internal/logger"
  10. "github.com/mhsanaei/3x-ui/v3/internal/web/runtime"
  11. "github.com/mhsanaei/3x-ui/v3/internal/web/service"
  12. "github.com/mhsanaei/3x-ui/v3/internal/xray"
  13. )
  14. // The sync tick is the only place that sees which nodes it no longer fetches, so it
  15. // must drop their online sets itself: a disabled node here, a deleted one below.
  16. func TestNodeTrafficSyncDropsOnlineClientsOfUnsyncedNodes(t *testing.T) {
  17. xuilogger.InitLogger(logging.ERROR)
  18. dbtest.InitDB(t, filepath.Join(t.TempDir(), "x-ui.db"))
  19. runtime.SetManager(runtime.NewManager(runtime.LocalDeps{APIPort: func() int { return 0 }, SetNeedRestart: func() {}}))
  20. t.Cleanup(func() { runtime.SetManager(nil) })
  21. process := xray.NewTestProcess(nil, "")
  22. t.Cleanup(service.SetXrayProcessForTest(process))
  23. disabled := &model.Node{Name: "disabled", Address: "127.0.0.1", Port: 1, ApiToken: "tok", Enable: true, Status: "online"}
  24. if err := database.GetDB().Create(disabled).Error; err != nil {
  25. t.Fatalf("create node: %v", err)
  26. }
  27. if err := database.GetDB().Model(disabled).Update("enable", false).Error; err != nil {
  28. t.Fatalf("disable node: %v", err)
  29. }
  30. process.SetNodeOnlineTree(disabled.Id, map[string][]string{"g-disabled": {"a@x"}})
  31. process.SetNodeOnlineTree(disabled.Id+100, map[string][]string{"g-deleted": {"b@x"}})
  32. NewNodeTrafficSyncJob().Run()
  33. if got := process.GetMergedNodeTrees(); len(got) != 0 {
  34. t.Fatalf("online sets after a sync tick = %v, want none for a disabled or deleted node", got)
  35. }
  36. }