From a958400595a282548d97c441234d41f20aef9b3c Mon Sep 17 00:00:00 2001 From: kevin Date: Sun, 27 Sep 2020 16:14:16 +0800 Subject: [PATCH] rename prommetric to prometheous, add unit tests --- core/collection/cache_test.go | 51 ++++++++++++++++ core/conf/config_test.go | 59 +++++++++++++++++++ rest/engine.go | 2 +- ...metrichandler.go => prometheoushandler.go} | 2 +- ...ler_test.go => prometheoushandler_test.go} | 2 +- zrpc/internal/client.go | 2 +- ...terceptor.go => prometheousinterceptor.go} | 2 +- ...test.go => prometheousinterceptor_test.go} | 2 +- zrpc/internal/rpcserver.go | 2 +- ...terceptor.go => prometheousinterceptor.go} | 2 +- ...test.go => prometheousinterceptor_test.go} | 2 +- 11 files changed, 119 insertions(+), 9 deletions(-) create mode 100644 core/conf/config_test.go rename rest/handler/{prommetrichandler.go => prometheoushandler.go} (94%) rename rest/handler/{prommetrichandler_test.go => prometheoushandler_test.go} (89%) rename zrpc/internal/clientinterceptors/{prommetricinterceptor.go => prometheousinterceptor.go} (92%) rename zrpc/internal/clientinterceptors/{prommetricinterceptor_test.go => prometheousinterceptor_test.go} (89%) rename zrpc/internal/serverinterceptors/{prommetricinterceptor.go => prometheousinterceptor.go} (94%) rename zrpc/internal/serverinterceptors/{prommetricinterceptor_test.go => prometheousinterceptor_test.go} (89%) diff --git a/core/collection/cache_test.go b/core/collection/cache_test.go index 24366aa6..bf30ee71 100644 --- a/core/collection/cache_test.go +++ b/core/collection/cache_test.go @@ -1,6 +1,7 @@ package collection import ( + "errors" "strconv" "sync" "sync/atomic" @@ -10,6 +11,8 @@ import ( "github.com/stretchr/testify/assert" ) +var errDummy = errors.New("dummy") + func TestCacheSet(t *testing.T) { cache, err := NewCache(time.Second*2, WithName("any")) assert.Nil(t, err) @@ -63,6 +66,54 @@ func TestCacheTake(t *testing.T) { assert.Equal(t, int32(1), atomic.LoadInt32(&count)) } +func TestCacheTakeExists(t *testing.T) { + cache, err := NewCache(time.Second * 2) + assert.Nil(t, err) + + var count int32 + var wg sync.WaitGroup + for i := 0; i < 100; i++ { + wg.Add(1) + go func() { + cache.Set("first", "first element") + cache.Take("first", func() (interface{}, error) { + atomic.AddInt32(&count, 1) + time.Sleep(time.Millisecond * 100) + return "first element", nil + }) + wg.Done() + }() + } + wg.Wait() + + assert.Equal(t, 1, cache.size()) + assert.Equal(t, int32(0), atomic.LoadInt32(&count)) +} + +func TestCacheTakeError(t *testing.T) { + cache, err := NewCache(time.Second * 2) + assert.Nil(t, err) + + var count int32 + var wg sync.WaitGroup + for i := 0; i < 100; i++ { + wg.Add(1) + go func() { + _, err := cache.Take("first", func() (interface{}, error) { + atomic.AddInt32(&count, 1) + time.Sleep(time.Millisecond * 100) + return "", errDummy + }) + assert.Equal(t, errDummy, err) + wg.Done() + }() + } + wg.Wait() + + assert.Equal(t, 0, cache.size()) + assert.Equal(t, int32(1), atomic.LoadInt32(&count)) +} + func TestCacheWithLruEvicts(t *testing.T) { cache, err := NewCache(time.Minute, WithLimit(3)) assert.Nil(t, err) diff --git a/core/conf/config_test.go b/core/conf/config_test.go new file mode 100644 index 00000000..34d1e2a9 --- /dev/null +++ b/core/conf/config_test.go @@ -0,0 +1,59 @@ +package conf + +import ( + "io/ioutil" + "os" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/tal-tech/go-zero/core/hash" +) + +func TestConfigJson(t *testing.T) { + tests := []string{ + ".json", + ".yaml", + ".yml", + } + text := `{ + "a": "foo", + "b": 1 +}` + for _, test := range tests { + test := test + t.Run(test, func(t *testing.T) { + t.Parallel() + + tmpfile, err := createTempFile(test, text) + assert.Nil(t, err) + defer os.Remove(tmpfile) + + var val struct { + A string `json:"a"` + B int `json:"b"` + } + err = LoadConfig(tmpfile, &val) + assert.Nil(t, err) + assert.Equal(t, "foo", val.A) + assert.Equal(t, 1, val.B) + }) + } +} + +func createTempFile(ext, text string) (string, error) { + tmpfile, err := ioutil.TempFile(os.TempDir(), hash.Md5Hex([]byte(text))+"*"+ext) + if err != nil { + return "", err + } + + if err := ioutil.WriteFile(tmpfile.Name(), []byte(text), os.ModeTemporary); err != nil { + return "", err + } + + filename := tmpfile.Name() + if err = tmpfile.Close(); err != nil { + return "", err + } + + return filename, nil +} diff --git a/rest/engine.go b/rest/engine.go index 7f3f6969..1ae37ec6 100644 --- a/rest/engine.go +++ b/rest/engine.go @@ -110,7 +110,7 @@ func (s *engine) bindRoute(fr featuredRoutes, router httpx.Router, metrics *stat handler.TimeoutHandler(time.Duration(s.conf.Timeout)*time.Millisecond), handler.RecoverHandler, handler.MetricHandler(metrics), - handler.PromMetricHandler(route.Path), + handler.PrometheousHandler(route.Path), handler.MaxBytesHandler(s.conf.MaxBytes), handler.GunzipHandler, ) diff --git a/rest/handler/prommetrichandler.go b/rest/handler/prometheoushandler.go similarity index 94% rename from rest/handler/prommetrichandler.go rename to rest/handler/prometheoushandler.go index 5a4d9f8f..17ef1a80 100644 --- a/rest/handler/prommetrichandler.go +++ b/rest/handler/prometheoushandler.go @@ -31,7 +31,7 @@ var ( }) ) -func PromMetricHandler(path string) func(http.Handler) http.Handler { +func PrometheousHandler(path string) func(http.Handler) http.Handler { return func(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { startTime := timex.Now() diff --git a/rest/handler/prommetrichandler_test.go b/rest/handler/prometheoushandler_test.go similarity index 89% rename from rest/handler/prommetrichandler_test.go rename to rest/handler/prometheoushandler_test.go index 2db66220..39bb11c0 100644 --- a/rest/handler/prommetrichandler_test.go +++ b/rest/handler/prometheoushandler_test.go @@ -9,7 +9,7 @@ import ( ) func TestPromMetricHandler(t *testing.T) { - promMetricHandler := PromMetricHandler("/user/login") + promMetricHandler := PrometheousHandler("/user/login") handler := promMetricHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) })) diff --git a/zrpc/internal/client.go b/zrpc/internal/client.go index 873badb0..f0b48467 100644 --- a/zrpc/internal/client.go +++ b/zrpc/internal/client.go @@ -74,7 +74,7 @@ func buildDialOptions(opts ...ClientOption) []grpc.DialOption { clientinterceptors.TracingInterceptor, clientinterceptors.DurationInterceptor, clientinterceptors.BreakerInterceptor, - clientinterceptors.PromMetricInterceptor, + clientinterceptors.PrometheousInterceptor, clientinterceptors.TimeoutInterceptor(clientOptions.Timeout), ), } diff --git a/zrpc/internal/clientinterceptors/prommetricinterceptor.go b/zrpc/internal/clientinterceptors/prometheousinterceptor.go similarity index 92% rename from zrpc/internal/clientinterceptors/prommetricinterceptor.go rename to zrpc/internal/clientinterceptors/prometheousinterceptor.go index efa333b3..cab6946f 100644 --- a/zrpc/internal/clientinterceptors/prommetricinterceptor.go +++ b/zrpc/internal/clientinterceptors/prometheousinterceptor.go @@ -32,7 +32,7 @@ var ( }) ) -func PromMetricInterceptor(ctx context.Context, method string, req, reply interface{}, +func PrometheousInterceptor(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error { startTime := timex.Now() err := invoker(ctx, method, req, reply, cc, opts...) diff --git a/zrpc/internal/clientinterceptors/prommetricinterceptor_test.go b/zrpc/internal/clientinterceptors/prometheousinterceptor_test.go similarity index 89% rename from zrpc/internal/clientinterceptors/prommetricinterceptor_test.go rename to zrpc/internal/clientinterceptors/prometheousinterceptor_test.go index c299e89f..2ebc7b31 100644 --- a/zrpc/internal/clientinterceptors/prommetricinterceptor_test.go +++ b/zrpc/internal/clientinterceptors/prometheousinterceptor_test.go @@ -26,7 +26,7 @@ func TestPromMetricInterceptor(t *testing.T) { for _, test := range tests { t.Run(test.name, func(t *testing.T) { cc := new(grpc.ClientConn) - err := PromMetricInterceptor(context.Background(), "/foo", nil, nil, cc, + err := PrometheousInterceptor(context.Background(), "/foo", nil, nil, cc, func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, opts ...grpc.CallOption) error { return test.err diff --git a/zrpc/internal/rpcserver.go b/zrpc/internal/rpcserver.go index c9919c74..efb3cb71 100644 --- a/zrpc/internal/rpcserver.go +++ b/zrpc/internal/rpcserver.go @@ -55,7 +55,7 @@ func (s *rpcServer) Start(register RegisterFn) error { serverinterceptors.UnaryTracingInterceptor(s.name), serverinterceptors.UnaryCrashInterceptor(), serverinterceptors.UnaryStatInterceptor(s.metrics), - serverinterceptors.UnaryPromMetricInterceptor(), + serverinterceptors.UnaryPrometheousInterceptor(), } unaryInterceptors = append(unaryInterceptors, s.unaryInterceptors...) streamInterceptors := []grpc.StreamServerInterceptor{ diff --git a/zrpc/internal/serverinterceptors/prommetricinterceptor.go b/zrpc/internal/serverinterceptors/prometheousinterceptor.go similarity index 94% rename from zrpc/internal/serverinterceptors/prommetricinterceptor.go rename to zrpc/internal/serverinterceptors/prometheousinterceptor.go index ed9fec6e..d9ad90a3 100644 --- a/zrpc/internal/serverinterceptors/prommetricinterceptor.go +++ b/zrpc/internal/serverinterceptors/prometheousinterceptor.go @@ -32,7 +32,7 @@ var ( }) ) -func UnaryPromMetricInterceptor() grpc.UnaryServerInterceptor { +func UnaryPrometheousInterceptor() grpc.UnaryServerInterceptor { return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) ( interface{}, error) { startTime := timex.Now() diff --git a/zrpc/internal/serverinterceptors/prommetricinterceptor_test.go b/zrpc/internal/serverinterceptors/prometheousinterceptor_test.go similarity index 89% rename from zrpc/internal/serverinterceptors/prommetricinterceptor_test.go rename to zrpc/internal/serverinterceptors/prometheousinterceptor_test.go index da95d728..14eacb60 100644 --- a/zrpc/internal/serverinterceptors/prommetricinterceptor_test.go +++ b/zrpc/internal/serverinterceptors/prometheousinterceptor_test.go @@ -9,7 +9,7 @@ import ( ) func TestUnaryPromMetricInterceptor(t *testing.T) { - interceptor := UnaryPromMetricInterceptor() + interceptor := UnaryPrometheousInterceptor() _, err := interceptor(context.Background(), nil, &grpc.UnaryServerInfo{ FullMethod: "/", }, func(ctx context.Context, req interface{}) (interface{}, error) {