瀏覽代碼

rename prommetric to prometheous, add unit tests

kevin 4 年之前
父節點
當前提交
a958400595

+ 51 - 0
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)

+ 59 - 0
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
+}

+ 1 - 1
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,
 	)

+ 1 - 1
rest/handler/prommetrichandler.go → 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()

+ 1 - 1
rest/handler/prommetrichandler_test.go → 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)
 	}))

+ 1 - 1
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),
 		),
 	}

+ 1 - 1
zrpc/internal/clientinterceptors/prommetricinterceptor.go → 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...)

+ 1 - 1
zrpc/internal/clientinterceptors/prommetricinterceptor_test.go → 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

+ 1 - 1
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{

+ 1 - 1
zrpc/internal/serverinterceptors/prommetricinterceptor.go → 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()

+ 1 - 1
zrpc/internal/serverinterceptors/prommetricinterceptor_test.go → 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) {