1
0

xray_traffic_job_broadcast_test.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package job
  2. import (
  3. "slices"
  4. "testing"
  5. "github.com/mhsanaei/3x-ui/v3/internal/xray"
  6. )
  7. func TestSplitMovedClientTraffics(t *testing.T) {
  8. rows := []*xray.ClientTraffic{
  9. {Email: "idle@x", Up: 0, Down: 0},
  10. {Email: "up@x", Up: 1024, Down: 0},
  11. {Email: "down@x", Up: 0, Down: 2048},
  12. nil,
  13. {Email: "both@x", Up: 512, Down: 512},
  14. {Email: "alsoidle@x", Up: 0, Down: 0},
  15. }
  16. moved, emails, active := splitMovedClientTraffics(rows)
  17. t.Run("only the rows that moved bytes are broadcast", func(t *testing.T) {
  18. got := make([]string, 0, len(moved))
  19. for _, ct := range moved {
  20. got = append(got, ct.Email)
  21. }
  22. want := []string{"up@x", "down@x", "both@x"}
  23. if !slices.Equal(got, want) {
  24. t.Fatalf("moved = %v, want %v", got, want)
  25. }
  26. })
  27. t.Run("the active list and set agree with the broadcast rows", func(t *testing.T) {
  28. want := []string{"up@x", "down@x", "both@x"}
  29. if !slices.Equal(emails, want) {
  30. t.Fatalf("activeEmails = %v, want %v", emails, want)
  31. }
  32. if len(active) != len(want) {
  33. t.Fatalf("deltaActive has %d entries, want %d", len(active), len(want))
  34. }
  35. for _, e := range want {
  36. if !active[e] {
  37. t.Fatalf("deltaActive missing %q", e)
  38. }
  39. }
  40. if active["idle@x"] {
  41. t.Fatal("an idle client must not count as active")
  42. }
  43. })
  44. t.Run("an all-idle poll broadcasts nothing", func(t *testing.T) {
  45. moved, emails, active := splitMovedClientTraffics([]*xray.ClientTraffic{
  46. {Email: "a@x"}, {Email: "b@x"},
  47. })
  48. if len(moved) != 0 || len(emails) != 0 || len(active) != 0 {
  49. t.Fatalf("expected an empty split, got %d/%d/%d", len(moved), len(emails), len(active))
  50. }
  51. })
  52. }