1
0

node_transition_burst_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. package job
  2. import (
  3. "fmt"
  4. "net/http/httptest"
  5. "path/filepath"
  6. "strconv"
  7. "strings"
  8. "sync"
  9. "testing"
  10. "time"
  11. "github.com/op/go-logging"
  12. "github.com/mhsanaei/3x-ui/v3/internal/database"
  13. "github.com/mhsanaei/3x-ui/v3/internal/database/dbtest"
  14. "github.com/mhsanaei/3x-ui/v3/internal/database/model"
  15. "github.com/mhsanaei/3x-ui/v3/internal/eventbus"
  16. xuilogger "github.com/mhsanaei/3x-ui/v3/internal/logger"
  17. "github.com/mhsanaei/3x-ui/v3/internal/web/runtime"
  18. )
  19. // goingDownNodes seeds n online nodes whose address refuses connections, so the
  20. // next heartbeat flips every one of them to offline in the same tick.
  21. func goingDownNodes(t *testing.T, n int) {
  22. t.Helper()
  23. xuilogger.InitLogger(logging.ERROR)
  24. dbtest.InitDB(t, filepath.Join(t.TempDir(), "x-ui.db"))
  25. runtime.SetManager(runtime.NewManager(runtime.LocalDeps{APIPort: func() int { return 0 }, SetNeedRestart: func() {}}))
  26. t.Cleanup(func() { runtime.SetManager(nil) })
  27. srv := httptest.NewServer(nil)
  28. host, port, _ := strings.Cut(strings.TrimPrefix(srv.URL, "http://"), ":")
  29. portNum, _ := strconv.Atoi(port)
  30. srv.Close()
  31. for i := range n {
  32. node := &model.Node{
  33. Name: fmt.Sprintf("node-%02d", i), Scheme: "http", Address: host, Port: portNum, BasePath: "/",
  34. ApiToken: "tok", Enable: true, Status: "online", AllowPrivateAddress: true, TlsVerifyMode: "verify",
  35. }
  36. if err := database.GetDB().Create(node).Error; err != nil {
  37. t.Fatalf("create node: %v", err)
  38. }
  39. }
  40. }
  41. func collectNodeEvents(t *testing.T) func() []eventbus.Event {
  42. t.Helper()
  43. bus := eventbus.New(eventbus.DefaultBufferSize)
  44. var mu sync.Mutex
  45. var got []eventbus.Event
  46. bus.Subscribe("test", func(e eventbus.Event) {
  47. mu.Lock()
  48. got = append(got, e)
  49. mu.Unlock()
  50. })
  51. prev := EventBus
  52. EventBus = bus
  53. t.Cleanup(func() {
  54. EventBus = prev
  55. bus.Stop()
  56. })
  57. return func() []eventbus.Event {
  58. time.Sleep(300 * time.Millisecond)
  59. mu.Lock()
  60. defer mu.Unlock()
  61. return append([]eventbus.Event(nil), got...)
  62. }
  63. }
  64. // A master-side blip flipped every node in one tick and published one event per
  65. // node, overflowing the notifier queues and every chat's rate limit.
  66. func TestHeartbeatSummarizesNodeDownBurst(t *testing.T) {
  67. goingDownNodes(t, 12)
  68. events := collectNodeEvents(t)
  69. NewNodeHeartbeatJob().Run()
  70. got := events()
  71. if len(got) != 1 {
  72. t.Fatalf("heartbeat published %d events for 12 nodes going down, want 1 summary", len(got))
  73. }
  74. want := "node-00, node-01, node-02, node-03, node-04, node-05, node-06, node-07, node-08, node-09 (+2)"
  75. if got[0].Type != eventbus.EventNodeDown || got[0].Source != want {
  76. t.Fatalf("summary event = %s %q, want %s %q", got[0].Type, got[0].Source, eventbus.EventNodeDown, want)
  77. }
  78. }
  79. func TestHeartbeatKeepsSingleNodeDownEvent(t *testing.T) {
  80. goingDownNodes(t, 1)
  81. events := collectNodeEvents(t)
  82. NewNodeHeartbeatJob().Run()
  83. got := events()
  84. if len(got) != 1 || got[0].Source != "node-00" {
  85. t.Fatalf("events = %+v, want the node's own node.down", got)
  86. }
  87. if _, ok := got[0].Data.(*eventbus.NodeHealthData); !ok {
  88. t.Fatalf("single node.down lost its health data: %#v", got[0].Data)
  89. }
  90. }