diff options
-rw-r--r-- | service/metrics/rpc_test.go | 524 |
1 files changed, 86 insertions, 438 deletions
diff --git a/service/metrics/rpc_test.go b/service/metrics/rpc_test.go index ee9b142f..c75e72fb 100644 --- a/service/metrics/rpc_test.go +++ b/service/metrics/rpc_test.go @@ -6,11 +6,12 @@ import ( "github.com/spiral/roadrunner/service" "github.com/spiral/roadrunner/service/rpc" "github.com/stretchr/testify/assert" + rpc2 "net/rpc" "testing" "time" ) -func Test_Set_RPC(t *testing.T) { +func setup(t *testing.T, metric string) (*rpc2.Client, service.Container) { logger, _ := test.NewNullLogger() logger.SetLevel(logrus.DebugLevel) @@ -23,11 +24,8 @@ func Test_Set_RPC(t *testing.T) { metricsCfg: `{ "address": "localhost:2112", "collect":{ - "user_gauge":{ - "type": "gauge" - } + ` + metric + ` } - }`})) s, _ := c.Get(ID) @@ -40,11 +38,22 @@ func Test_Set_RPC(t *testing.T) { go func() { c.Serve() }() time.Sleep(time.Millisecond * 100) - defer c.Stop() client, err := rs.Client() assert.NoError(t, err) + return client, c +} + +func Test_Set_RPC(t *testing.T) { + client, c := setup( + t, + `"user_gauge":{ + "type": "gauge" + }`, + ) + defer c.Stop() + var ok bool assert.NoError(t, client.Call("metrics.Set", Metric{ Name: "user_gauge", @@ -58,41 +67,15 @@ func Test_Set_RPC(t *testing.T) { } func Test_Set_RPC_Vector(t *testing.T) { - logger, _ := test.NewNullLogger() - logger.SetLevel(logrus.DebugLevel) - - c := service.NewContainer(logger) - c.Register(rpc.ID, &rpc.Service{}) - c.Register(ID, &Service{}) - - assert.NoError(t, c.Init(&testCfg{ - rpcCfg: `{"enable":true, "listen":"tcp://:5004"}`, - metricsCfg: `{ - "address": "localhost:2112", - "collect":{ - "user_gauge":{ + client, c := setup( + t, + `"user_gauge":{ "type": "gauge", "labels": ["type", "section"] - } - } - - }`})) - - s, _ := c.Get(ID) - assert.NotNil(t, s) - - s2, _ := c.Get(rpc.ID) - rs := s2.(*rpc.Service) - - assert.True(t, s.(*Service).Enabled()) - - go func() { c.Serve() }() - time.Sleep(time.Millisecond * 100) + }`, + ) defer c.Stop() - client, err := rs.Client() - assert.NoError(t, err) - var ok bool assert.NoError(t, client.Call("metrics.Set", Metric{ Name: "user_gauge", @@ -107,40 +90,15 @@ func Test_Set_RPC_Vector(t *testing.T) { } func Test_Set_RPC_CollectorError(t *testing.T) { - logger, _ := test.NewNullLogger() - logger.SetLevel(logrus.DebugLevel) - - c := service.NewContainer(logger) - c.Register(rpc.ID, &rpc.Service{}) - c.Register(ID, &Service{}) - - assert.NoError(t, c.Init(&testCfg{ - rpcCfg: `{"enable":true, "listen":"tcp://:5004"}`, - metricsCfg: `{ - "address": "localhost:2112", - "collect":{ - "user_gauge":{ - "type": "gauge", + client, c := setup( + t, + `"user_gauge":{ + "type": "gauge", "labels": ["type", "section"] - } - } - }`})) - - s, _ := c.Get(ID) - assert.NotNil(t, s) - - s2, _ := c.Get(rpc.ID) - rs := s2.(*rpc.Service) - - assert.True(t, s.(*Service).Enabled()) - - go func() { c.Serve() }() - time.Sleep(time.Millisecond * 100) + }`, + ) defer c.Stop() - client, err := rs.Client() - assert.NoError(t, err) - var ok bool assert.Error(t, client.Call("metrics.Set", Metric{ Name: "user_gauge_2", @@ -150,41 +108,15 @@ func Test_Set_RPC_CollectorError(t *testing.T) { } func Test_Set_RPC_MetricError(t *testing.T) { - logger, _ := test.NewNullLogger() - logger.SetLevel(logrus.DebugLevel) - - c := service.NewContainer(logger) - c.Register(rpc.ID, &rpc.Service{}) - c.Register(ID, &Service{}) - - assert.NoError(t, c.Init(&testCfg{ - rpcCfg: `{"enable":true, "listen":"tcp://:5004"}`, - metricsCfg: `{ - "address": "localhost:2112", - "collect":{ - "user_gauge":{ + client, c := setup( + t, + `"user_gauge":{ "type": "gauge", "labels": ["type", "section"] - } - } - - }`})) - - s, _ := c.Get(ID) - assert.NotNil(t, s) - - s2, _ := c.Get(rpc.ID) - rs := s2.(*rpc.Service) - - assert.True(t, s.(*Service).Enabled()) - - go func() { c.Serve() }() - time.Sleep(time.Millisecond * 100) + }`, + ) defer c.Stop() - client, err := rs.Client() - assert.NoError(t, err) - var ok bool assert.Error(t, client.Call("metrics.Set", Metric{ Name: "user_gauge", @@ -194,41 +126,15 @@ func Test_Set_RPC_MetricError(t *testing.T) { } func Test_Set_RPC_MetricError_2(t *testing.T) { - logger, _ := test.NewNullLogger() - logger.SetLevel(logrus.DebugLevel) - - c := service.NewContainer(logger) - c.Register(rpc.ID, &rpc.Service{}) - c.Register(ID, &Service{}) - - assert.NoError(t, c.Init(&testCfg{ - rpcCfg: `{"enable":true, "listen":"tcp://:5004"}`, - metricsCfg: `{ - "address": "localhost:2112", - "collect":{ - "user_gauge":{ + client, c := setup( + t, + `"user_gauge":{ "type": "gauge", "labels": ["type", "section"] - } - } - - }`})) - - s, _ := c.Get(ID) - assert.NotNil(t, s) - - s2, _ := c.Get(rpc.ID) - rs := s2.(*rpc.Service) - - assert.True(t, s.(*Service).Enabled()) - - go func() { c.Serve() }() - time.Sleep(time.Millisecond * 100) + }`, + ) defer c.Stop() - client, err := rs.Client() - assert.NoError(t, err) - var ok bool assert.Error(t, client.Call("metrics.Set", Metric{ Name: "user_gauge", @@ -239,40 +145,14 @@ func Test_Set_RPC_MetricError_2(t *testing.T) { // -- observe func Test_Observe_RPC(t *testing.T) { - logger, _ := test.NewNullLogger() - logger.SetLevel(logrus.DebugLevel) - - c := service.NewContainer(logger) - c.Register(rpc.ID, &rpc.Service{}) - c.Register(ID, &Service{}) - - assert.NoError(t, c.Init(&testCfg{ - rpcCfg: `{"enable":true, "listen":"tcp://:5004"}`, - metricsCfg: `{ - "address": "localhost:2112", - "collect":{ - "user_histogram":{ + client, c := setup( + t, + `"user_histogram":{ "type": "histogram" - } - } - - }`})) - - s, _ := c.Get(ID) - assert.NotNil(t, s) - - s2, _ := c.Get(rpc.ID) - rs := s2.(*rpc.Service) - - assert.True(t, s.(*Service).Enabled()) - - go func() { c.Serve() }() - time.Sleep(time.Millisecond * 100) + }`, + ) defer c.Stop() - client, err := rs.Client() - assert.NoError(t, err) - var ok bool assert.NoError(t, client.Call("metrics.Observe", Metric{ Name: "user_histogram", @@ -286,41 +166,15 @@ func Test_Observe_RPC(t *testing.T) { } func Test_Observe_RPC_Vector(t *testing.T) { - logger, _ := test.NewNullLogger() - logger.SetLevel(logrus.DebugLevel) - - c := service.NewContainer(logger) - c.Register(rpc.ID, &rpc.Service{}) - c.Register(ID, &Service{}) - - assert.NoError(t, c.Init(&testCfg{ - rpcCfg: `{"enable":true, "listen":"tcp://:5004"}`, - metricsCfg: `{ - "address": "localhost:2112", - "collect":{ - "user_histogram":{ + client, c := setup( + t, + `"user_histogram":{ "type": "histogram", "labels": ["type", "section"] - } - } - - }`})) - - s, _ := c.Get(ID) - assert.NotNil(t, s) - - s2, _ := c.Get(rpc.ID) - rs := s2.(*rpc.Service) - - assert.True(t, s.(*Service).Enabled()) - - go func() { c.Serve() }() - time.Sleep(time.Millisecond * 100) + }`, + ) defer c.Stop() - client, err := rs.Client() - assert.NoError(t, err) - var ok bool assert.NoError(t, client.Call("metrics.Observe", Metric{ Name: "user_histogram", @@ -335,40 +189,15 @@ func Test_Observe_RPC_Vector(t *testing.T) { } func Test_Observe_RPC_CollectorError(t *testing.T) { - logger, _ := test.NewNullLogger() - logger.SetLevel(logrus.DebugLevel) - - c := service.NewContainer(logger) - c.Register(rpc.ID, &rpc.Service{}) - c.Register(ID, &Service{}) - - assert.NoError(t, c.Init(&testCfg{ - rpcCfg: `{"enable":true, "listen":"tcp://:5004"}`, - metricsCfg: `{ - "address": "localhost:2112", - "collect":{ - "user_histogram":{ + client, c := setup( + t, + `"user_histogram":{ "type": "histogram", "labels": ["type", "section"] - } - } - }`})) - - s, _ := c.Get(ID) - assert.NotNil(t, s) - - s2, _ := c.Get(rpc.ID) - rs := s2.(*rpc.Service) - - assert.True(t, s.(*Service).Enabled()) - - go func() { c.Serve() }() - time.Sleep(time.Millisecond * 100) + }`, + ) defer c.Stop() - client, err := rs.Client() - assert.NoError(t, err) - var ok bool assert.Error(t, client.Call("metrics.Observe", Metric{ Name: "user_histogram", @@ -378,41 +207,15 @@ func Test_Observe_RPC_CollectorError(t *testing.T) { } func Test_Observe_RPC_MetricError(t *testing.T) { - logger, _ := test.NewNullLogger() - logger.SetLevel(logrus.DebugLevel) - - c := service.NewContainer(logger) - c.Register(rpc.ID, &rpc.Service{}) - c.Register(ID, &Service{}) - - assert.NoError(t, c.Init(&testCfg{ - rpcCfg: `{"enable":true, "listen":"tcp://:5004"}`, - metricsCfg: `{ - "address": "localhost:2112", - "collect":{ - "user_histogram":{ + client, c := setup( + t, + `"user_histogram":{ "type": "histogram", "labels": ["type", "section"] - } - } - - }`})) - - s, _ := c.Get(ID) - assert.NotNil(t, s) - - s2, _ := c.Get(rpc.ID) - rs := s2.(*rpc.Service) - - assert.True(t, s.(*Service).Enabled()) - - go func() { c.Serve() }() - time.Sleep(time.Millisecond * 100) + }`, + ) defer c.Stop() - client, err := rs.Client() - assert.NoError(t, err) - var ok bool assert.Error(t, client.Call("metrics.Observe", Metric{ Name: "user_histogram", @@ -422,41 +225,15 @@ func Test_Observe_RPC_MetricError(t *testing.T) { } func Test_Observe_RPC_MetricError_2(t *testing.T) { - logger, _ := test.NewNullLogger() - logger.SetLevel(logrus.DebugLevel) - - c := service.NewContainer(logger) - c.Register(rpc.ID, &rpc.Service{}) - c.Register(ID, &Service{}) - - assert.NoError(t, c.Init(&testCfg{ - rpcCfg: `{"enable":true, "listen":"tcp://:5004"}`, - metricsCfg: `{ - "address": "localhost:2112", - "collect":{ - "user_histogram":{ + client, c := setup( + t, + `"user_histogram":{ "type": "histogram", "labels": ["type", "section"] - } - } - - }`})) - - s, _ := c.Get(ID) - assert.NotNil(t, s) - - s2, _ := c.Get(rpc.ID) - rs := s2.(*rpc.Service) - - assert.True(t, s.(*Service).Enabled()) - - go func() { c.Serve() }() - time.Sleep(time.Millisecond * 100) + }`, + ) defer c.Stop() - client, err := rs.Client() - assert.NoError(t, err) - var ok bool assert.Error(t, client.Call("metrics.Observe", Metric{ Name: "user_histogram", @@ -467,40 +244,14 @@ func Test_Observe_RPC_MetricError_2(t *testing.T) { // -- observe summary func Test_Observe2_RPC(t *testing.T) { - logger, _ := test.NewNullLogger() - logger.SetLevel(logrus.DebugLevel) - - c := service.NewContainer(logger) - c.Register(rpc.ID, &rpc.Service{}) - c.Register(ID, &Service{}) - - assert.NoError(t, c.Init(&testCfg{ - rpcCfg: `{"enable":true, "listen":"tcp://:5004"}`, - metricsCfg: `{ - "address": "localhost:2112", - "collect":{ - "user_histogram":{ + client, c := setup( + t, + `"user_histogram":{ "type": "summary" - } - } - - }`})) - - s, _ := c.Get(ID) - assert.NotNil(t, s) - - s2, _ := c.Get(rpc.ID) - rs := s2.(*rpc.Service) - - assert.True(t, s.(*Service).Enabled()) - - go func() { c.Serve() }() - time.Sleep(time.Millisecond * 100) + }`, + ) defer c.Stop() - client, err := rs.Client() - assert.NoError(t, err) - var ok bool assert.NoError(t, client.Call("metrics.Observe", Metric{ Name: "user_histogram", @@ -514,41 +265,15 @@ func Test_Observe2_RPC(t *testing.T) { } func Test_Observe2_RPC_Vector(t *testing.T) { - logger, _ := test.NewNullLogger() - logger.SetLevel(logrus.DebugLevel) - - c := service.NewContainer(logger) - c.Register(rpc.ID, &rpc.Service{}) - c.Register(ID, &Service{}) - - assert.NoError(t, c.Init(&testCfg{ - rpcCfg: `{"enable":true, "listen":"tcp://:5004"}`, - metricsCfg: `{ - "address": "localhost:2112", - "collect":{ - "user_histogram":{ + client, c := setup( + t, + `"user_histogram":{ "type": "summary", "labels": ["type", "section"] - } - } - - }`})) - - s, _ := c.Get(ID) - assert.NotNil(t, s) - - s2, _ := c.Get(rpc.ID) - rs := s2.(*rpc.Service) - - assert.True(t, s.(*Service).Enabled()) - - go func() { c.Serve() }() - time.Sleep(time.Millisecond * 100) + }`, + ) defer c.Stop() - client, err := rs.Client() - assert.NoError(t, err) - var ok bool assert.NoError(t, client.Call("metrics.Observe", Metric{ Name: "user_histogram", @@ -563,40 +288,15 @@ func Test_Observe2_RPC_Vector(t *testing.T) { } func Test_Observe2_RPC_CollectorError(t *testing.T) { - logger, _ := test.NewNullLogger() - logger.SetLevel(logrus.DebugLevel) - - c := service.NewContainer(logger) - c.Register(rpc.ID, &rpc.Service{}) - c.Register(ID, &Service{}) - - assert.NoError(t, c.Init(&testCfg{ - rpcCfg: `{"enable":true, "listen":"tcp://:5004"}`, - metricsCfg: `{ - "address": "localhost:2112", - "collect":{ - "user_histogram":{ + client, c := setup( + t, + `"user_histogram":{ "type": "summary", "labels": ["type", "section"] - } - } - }`})) - - s, _ := c.Get(ID) - assert.NotNil(t, s) - - s2, _ := c.Get(rpc.ID) - rs := s2.(*rpc.Service) - - assert.True(t, s.(*Service).Enabled()) - - go func() { c.Serve() }() - time.Sleep(time.Millisecond * 100) + }`, + ) defer c.Stop() - client, err := rs.Client() - assert.NoError(t, err) - var ok bool assert.Error(t, client.Call("metrics.Observe", Metric{ Name: "user_histogram", @@ -606,41 +306,15 @@ func Test_Observe2_RPC_CollectorError(t *testing.T) { } func Test_Observe2_RPC_MetricError(t *testing.T) { - logger, _ := test.NewNullLogger() - logger.SetLevel(logrus.DebugLevel) - - c := service.NewContainer(logger) - c.Register(rpc.ID, &rpc.Service{}) - c.Register(ID, &Service{}) - - assert.NoError(t, c.Init(&testCfg{ - rpcCfg: `{"enable":true, "listen":"tcp://:5004"}`, - metricsCfg: `{ - "address": "localhost:2112", - "collect":{ - "user_histogram":{ + client, c := setup( + t, + `"user_histogram":{ "type": "summary", "labels": ["type", "section"] - } - } - - }`})) - - s, _ := c.Get(ID) - assert.NotNil(t, s) - - s2, _ := c.Get(rpc.ID) - rs := s2.(*rpc.Service) - - assert.True(t, s.(*Service).Enabled()) - - go func() { c.Serve() }() - time.Sleep(time.Millisecond * 100) + }`, + ) defer c.Stop() - client, err := rs.Client() - assert.NoError(t, err) - var ok bool assert.Error(t, client.Call("metrics.Observe", Metric{ Name: "user_histogram", @@ -650,41 +324,15 @@ func Test_Observe2_RPC_MetricError(t *testing.T) { } func Test_Observe2_RPC_MetricError_2(t *testing.T) { - logger, _ := test.NewNullLogger() - logger.SetLevel(logrus.DebugLevel) - - c := service.NewContainer(logger) - c.Register(rpc.ID, &rpc.Service{}) - c.Register(ID, &Service{}) - - assert.NoError(t, c.Init(&testCfg{ - rpcCfg: `{"enable":true, "listen":"tcp://:5004"}`, - metricsCfg: `{ - "address": "localhost:2112", - "collect":{ - "user_histogram":{ + client, c := setup( + t, + `"user_histogram":{ "type": "summary", "labels": ["type", "section"] - } - } - - }`})) - - s, _ := c.Get(ID) - assert.NotNil(t, s) - - s2, _ := c.Get(rpc.ID) - rs := s2.(*rpc.Service) - - assert.True(t, s.(*Service).Enabled()) - - go func() { c.Serve() }() - time.Sleep(time.Millisecond * 100) + }`, + ) defer c.Stop() - client, err := rs.Client() - assert.NoError(t, err) - var ok bool assert.Error(t, client.Call("metrics.Observe", Metric{ Name: "user_histogram", |