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