1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-07-03 20:45:23 +00:00
v2fly/app/stats/command/command_test.go

93 lines
1.8 KiB
Go
Raw Normal View History

2018-07-10 21:40:58 +00:00
package command_test
import (
2018-09-30 21:08:14 +00:00
"context"
2018-07-10 21:40:58 +00:00
"testing"
2019-02-09 14:46:48 +00:00
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
2018-07-10 21:40:58 +00:00
"v2ray.com/core/app/stats"
. "v2ray.com/core/app/stats/command"
2019-02-02 21:19:30 +00:00
"v2ray.com/core/common"
2018-07-10 21:40:58 +00:00
)
func TestGetStats(t *testing.T) {
m, err := stats.NewManager(context.Background(), &stats.Config{})
2019-02-02 21:19:30 +00:00
common.Must(err)
2018-07-10 21:40:58 +00:00
sc, err := m.RegisterCounter("test_counter")
2019-02-02 21:19:30 +00:00
common.Must(err)
2018-07-10 21:40:58 +00:00
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 {
2019-02-09 14:46:48 +00:00
if err == nil {
t.Error("nil error: ", tc.name)
}
2018-07-10 21:40:58 +00:00
} else {
2019-02-02 21:19:30 +00:00
common.Must(err)
2020-06-08 08:16:50 +00:00
if r := cmp.Diff(resp.Stat, &Stat{Name: tc.name, Value: tc.value}, cmpopts.IgnoreUnexported(Stat{})); r != "" {
2019-02-09 14:46:48 +00:00
t.Error(r)
}
2018-07-10 21:40:58 +00:00
}
}
}
func TestQueryStats(t *testing.T) {
m, err := stats.NewManager(context.Background(), &stats.Config{})
2019-02-02 21:19:30 +00:00
common.Must(err)
2018-07-10 21:40:58 +00:00
sc1, err := m.RegisterCounter("test_counter")
2019-02-02 21:19:30 +00:00
common.Must(err)
2018-07-10 21:40:58 +00:00
sc1.Set(1)
sc2, err := m.RegisterCounter("test_counter_2")
2019-02-02 21:19:30 +00:00
common.Must(err)
2018-07-10 21:40:58 +00:00
sc2.Set(2)
sc3, err := m.RegisterCounter("test_counter_3")
2019-02-02 21:19:30 +00:00
common.Must(err)
2018-07-10 21:40:58 +00:00
sc3.Set(3)
s := NewStatsServer(m)
resp, err := s.QueryStats(context.Background(), &QueryStatsRequest{
Pattern: "counter_",
})
2019-02-02 21:19:30 +00:00
common.Must(err)
2019-02-09 14:46:48 +00:00
if r := cmp.Diff(resp.Stat, []*Stat{
{Name: "test_counter_2", Value: 2},
{Name: "test_counter_3", Value: 3},
2020-06-08 08:16:50 +00:00
}, cmpopts.SortSlices(func(s1, s2 *Stat) bool { return s1.Name < s2.Name }),
cmpopts.IgnoreUnexported(Stat{})); r != "" {
2019-02-09 14:46:48 +00:00
t.Error(r)
2018-07-10 21:40:58 +00:00
}
}