rename prommetric to prometheous, add unit tests

master
kevin 4 years ago
parent 015716d1b5
commit a958400595

@ -1,6 +1,7 @@
package collection package collection
import ( import (
"errors"
"strconv" "strconv"
"sync" "sync"
"sync/atomic" "sync/atomic"
@ -10,6 +11,8 @@ import (
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
var errDummy = errors.New("dummy")
func TestCacheSet(t *testing.T) { func TestCacheSet(t *testing.T) {
cache, err := NewCache(time.Second*2, WithName("any")) cache, err := NewCache(time.Second*2, WithName("any"))
assert.Nil(t, err) assert.Nil(t, err)
@ -63,6 +66,54 @@ func TestCacheTake(t *testing.T) {
assert.Equal(t, int32(1), atomic.LoadInt32(&count)) 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) { func TestCacheWithLruEvicts(t *testing.T) {
cache, err := NewCache(time.Minute, WithLimit(3)) cache, err := NewCache(time.Minute, WithLimit(3))
assert.Nil(t, err) assert.Nil(t, err)

@ -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
}

@ -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.TimeoutHandler(time.Duration(s.conf.Timeout)*time.Millisecond),
handler.RecoverHandler, handler.RecoverHandler,
handler.MetricHandler(metrics), handler.MetricHandler(metrics),
handler.PromMetricHandler(route.Path), handler.PrometheousHandler(route.Path),
handler.MaxBytesHandler(s.conf.MaxBytes), handler.MaxBytesHandler(s.conf.MaxBytes),
handler.GunzipHandler, handler.GunzipHandler,
) )

@ -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 func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
startTime := timex.Now() startTime := timex.Now()

@ -9,7 +9,7 @@ import (
) )
func TestPromMetricHandler(t *testing.T) { func TestPromMetricHandler(t *testing.T) {
promMetricHandler := PromMetricHandler("/user/login") promMetricHandler := PrometheousHandler("/user/login")
handler := promMetricHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { handler := promMetricHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
})) }))

@ -74,7 +74,7 @@ func buildDialOptions(opts ...ClientOption) []grpc.DialOption {
clientinterceptors.TracingInterceptor, clientinterceptors.TracingInterceptor,
clientinterceptors.DurationInterceptor, clientinterceptors.DurationInterceptor,
clientinterceptors.BreakerInterceptor, clientinterceptors.BreakerInterceptor,
clientinterceptors.PromMetricInterceptor, clientinterceptors.PrometheousInterceptor,
clientinterceptors.TimeoutInterceptor(clientOptions.Timeout), clientinterceptors.TimeoutInterceptor(clientOptions.Timeout),
), ),
} }

@ -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 { cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
startTime := timex.Now() startTime := timex.Now()
err := invoker(ctx, method, req, reply, cc, opts...) err := invoker(ctx, method, req, reply, cc, opts...)

@ -26,7 +26,7 @@ func TestPromMetricInterceptor(t *testing.T) {
for _, test := range tests { for _, test := range tests {
t.Run(test.name, func(t *testing.T) { t.Run(test.name, func(t *testing.T) {
cc := new(grpc.ClientConn) 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, func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn,
opts ...grpc.CallOption) error { opts ...grpc.CallOption) error {
return test.err return test.err

@ -55,7 +55,7 @@ func (s *rpcServer) Start(register RegisterFn) error {
serverinterceptors.UnaryTracingInterceptor(s.name), serverinterceptors.UnaryTracingInterceptor(s.name),
serverinterceptors.UnaryCrashInterceptor(), serverinterceptors.UnaryCrashInterceptor(),
serverinterceptors.UnaryStatInterceptor(s.metrics), serverinterceptors.UnaryStatInterceptor(s.metrics),
serverinterceptors.UnaryPromMetricInterceptor(), serverinterceptors.UnaryPrometheousInterceptor(),
} }
unaryInterceptors = append(unaryInterceptors, s.unaryInterceptors...) unaryInterceptors = append(unaryInterceptors, s.unaryInterceptors...)
streamInterceptors := []grpc.StreamServerInterceptor{ streamInterceptors := []grpc.StreamServerInterceptor{

@ -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) ( return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (
interface{}, error) { interface{}, error) {
startTime := timex.Now() startTime := timex.Now()

@ -9,7 +9,7 @@ import (
) )
func TestUnaryPromMetricInterceptor(t *testing.T) { func TestUnaryPromMetricInterceptor(t *testing.T) {
interceptor := UnaryPromMetricInterceptor() interceptor := UnaryPrometheousInterceptor()
_, err := interceptor(context.Background(), nil, &grpc.UnaryServerInfo{ _, err := interceptor(context.Background(), nil, &grpc.UnaryServerInfo{
FullMethod: "/", FullMethod: "/",
}, func(ctx context.Context, req interface{}) (interface{}, error) { }, func(ctx context.Context, req interface{}) (interface{}, error) {
Loading…
Cancel
Save