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