1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-22 18:17:52 -05:00
v2fly/app/stats/command/command_test.go

93 lines
1.8 KiB
Go
Raw Normal View History

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