github.com/xmplusdev/xray-core@v1.8.10/app/stats/command/command_test.go (about) 1 package command_test 2 3 import ( 4 "context" 5 "testing" 6 7 "github.com/google/go-cmp/cmp" 8 "github.com/google/go-cmp/cmp/cmpopts" 9 "github.com/xmplusdev/xray-core/app/stats" 10 . "github.com/xmplusdev/xray-core/app/stats/command" 11 "github.com/xmplusdev/xray-core/common" 12 ) 13 14 func TestGetStats(t *testing.T) { 15 m, err := stats.NewManager(context.Background(), &stats.Config{}) 16 common.Must(err) 17 18 sc, err := m.RegisterCounter("test_counter") 19 common.Must(err) 20 21 sc.Set(1) 22 23 s := NewStatsServer(m) 24 25 testCases := []struct { 26 name string 27 reset bool 28 value int64 29 err bool 30 }{ 31 { 32 name: "counterNotExist", 33 err: true, 34 }, 35 { 36 name: "test_counter", 37 reset: true, 38 value: 1, 39 }, 40 { 41 name: "test_counter", 42 value: 0, 43 }, 44 } 45 for _, tc := range testCases { 46 resp, err := s.GetStats(context.Background(), &GetStatsRequest{ 47 Name: tc.name, 48 Reset_: tc.reset, 49 }) 50 if tc.err { 51 if err == nil { 52 t.Error("nil error: ", tc.name) 53 } 54 } else { 55 common.Must(err) 56 if r := cmp.Diff(resp.Stat, &Stat{Name: tc.name, Value: tc.value}, cmpopts.IgnoreUnexported(Stat{})); r != "" { 57 t.Error(r) 58 } 59 } 60 } 61 } 62 63 func TestQueryStats(t *testing.T) { 64 m, err := stats.NewManager(context.Background(), &stats.Config{}) 65 common.Must(err) 66 67 sc1, err := m.RegisterCounter("test_counter") 68 common.Must(err) 69 sc1.Set(1) 70 71 sc2, err := m.RegisterCounter("test_counter_2") 72 common.Must(err) 73 sc2.Set(2) 74 75 sc3, err := m.RegisterCounter("test_counter_3") 76 common.Must(err) 77 sc3.Set(3) 78 79 s := NewStatsServer(m) 80 resp, err := s.QueryStats(context.Background(), &QueryStatsRequest{ 81 Pattern: "counter_", 82 }) 83 common.Must(err) 84 if r := cmp.Diff(resp.Stat, []*Stat{ 85 {Name: "test_counter_2", Value: 2}, 86 {Name: "test_counter_3", Value: 3}, 87 }, cmpopts.SortSlices(func(s1, s2 *Stat) bool { return s1.Name < s2.Name }), 88 cmpopts.IgnoreUnexported(Stat{})); r != "" { 89 t.Error(r) 90 } 91 }