stats_test.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. package stats_test
  2. import (
  3. "context"
  4. "testing"
  5. "time"
  6. . "github.com/xtls/xray-core/app/stats"
  7. "github.com/xtls/xray-core/common"
  8. "github.com/xtls/xray-core/features/stats"
  9. )
  10. func TestInterface(t *testing.T) {
  11. _ = (stats.Manager)(new(Manager))
  12. }
  13. func TestStatsChannelRunnable(t *testing.T) {
  14. raw, err := common.CreateObject(context.Background(), &Config{})
  15. common.Must(err)
  16. m := raw.(stats.Manager)
  17. ch1, err := m.RegisterChannel("test.channel.1")
  18. c1 := ch1.(*Channel)
  19. common.Must(err)
  20. if c1.Running() {
  21. t.Fatalf("unexpected running channel: test.channel.%d", 1)
  22. }
  23. common.Must(m.Start())
  24. if !c1.Running() {
  25. t.Fatalf("unexpected non-running channel: test.channel.%d", 1)
  26. }
  27. ch2, err := m.RegisterChannel("test.channel.2")
  28. c2 := ch2.(*Channel)
  29. common.Must(err)
  30. if !c2.Running() {
  31. t.Fatalf("unexpected non-running channel: test.channel.%d", 2)
  32. }
  33. s1, err := c1.Subscribe()
  34. common.Must(err)
  35. common.Must(c1.Close())
  36. if c1.Running() {
  37. t.Fatalf("unexpected running channel: test.channel.%d", 1)
  38. }
  39. select { // Check all subscribers in closed channel are closed
  40. case _, ok := <-s1:
  41. if ok {
  42. t.Fatalf("unexpected non-closed subscriber in channel: test.channel.%d", 1)
  43. }
  44. case <-time.After(500 * time.Millisecond):
  45. t.Fatalf("unexpected non-closed subscriber in channel: test.channel.%d", 1)
  46. }
  47. if len(c1.Subscribers()) != 0 { // Check subscribers in closed channel are emptied
  48. t.Fatalf("unexpected non-empty subscribers in channel: test.channel.%d", 1)
  49. }
  50. common.Must(m.Close())
  51. if c2.Running() {
  52. t.Fatalf("unexpected running channel: test.channel.%d", 2)
  53. }
  54. ch3, err := m.RegisterChannel("test.channel.3")
  55. c3 := ch3.(*Channel)
  56. common.Must(err)
  57. if c3.Running() {
  58. t.Fatalf("unexpected running channel: test.channel.%d", 3)
  59. }
  60. common.Must(c3.Start())
  61. common.Must(m.UnregisterChannel("test.channel.3"))
  62. if c3.Running() { // Test that unregistering will close the channel.
  63. t.Fatalf("unexpected running channel: test.channel.%d", 3)
  64. }
  65. }