From 33a9db85c8fccb8e7e9dbc9e13f4d9c4fdb4545d Mon Sep 17 00:00:00 2001 From: kevin Date: Tue, 29 Sep 2020 14:30:22 +0800 Subject: [PATCH] add unit test, fix interceptor bug --- core/mapping/fieldoptions_test.go | 31 ++++++++++++++++++++++++++++++ core/mapping/unmarshaler_test.go | 7 +++++++ core/proc/goroutines_test.go | 16 +++++++++++++++ core/proc/profile_test.go | 21 ++++++++++++++++++++ example/rpc/server/unary/server.go | 9 +++++++++ zrpc/server.go | 6 +++--- 6 files changed, 87 insertions(+), 3 deletions(-) create mode 100644 core/mapping/fieldoptions_test.go create mode 100644 core/proc/goroutines_test.go create mode 100644 core/proc/profile_test.go diff --git a/core/mapping/fieldoptions_test.go b/core/mapping/fieldoptions_test.go new file mode 100644 index 00000000..01f2f2da --- /dev/null +++ b/core/mapping/fieldoptions_test.go @@ -0,0 +1,31 @@ +package mapping + +import ( + "reflect" + "testing" + + "github.com/stretchr/testify/assert" +) + +type Bar struct { + Val string `json:"val"` +} + +func TestFieldOptionOptionalDep(t *testing.T) { + var bar Bar + rt := reflect.TypeOf(bar) + for i := 0; i < rt.NumField(); i++ { + field := rt.Field(i) + val, opt, err := parseKeyAndOptions(jsonTagKey, field) + assert.Equal(t, "val", val) + assert.Nil(t, opt) + assert.Nil(t, err) + } + + // check nil working + var o *fieldOptions + check := func(o *fieldOptions) { + assert.Equal(t, 0, len(o.optionalDep())) + } + check(o) +} diff --git a/core/mapping/unmarshaler_test.go b/core/mapping/unmarshaler_test.go index da23d599..b1cd1e53 100644 --- a/core/mapping/unmarshaler_test.go +++ b/core/mapping/unmarshaler_test.go @@ -2387,6 +2387,13 @@ func TestUnmarshalNestedMapSimpleTypeMatch(t *testing.T) { assert.Equal(t, "1", c.Anything["id"]) } +func TestUnmarshalValuer(t *testing.T) { + unmarshaler := NewUnmarshaler(jsonTagKey) + var foo string + err := unmarshaler.UnmarshalValuer(nil, foo) + assert.NotNil(t, err) +} + func BenchmarkUnmarshalString(b *testing.B) { type inner struct { Value string `key:"value"` diff --git a/core/proc/goroutines_test.go b/core/proc/goroutines_test.go new file mode 100644 index 00000000..4a4685d8 --- /dev/null +++ b/core/proc/goroutines_test.go @@ -0,0 +1,16 @@ +package proc + +import ( + "log" + "strings" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestDumpGoroutines(t *testing.T) { + var buf strings.Builder + log.SetOutput(&buf) + dumpGoroutines() + assert.True(t, strings.Contains(buf.String(), ".dump")) +} diff --git a/core/proc/profile_test.go b/core/proc/profile_test.go new file mode 100644 index 00000000..a5353a59 --- /dev/null +++ b/core/proc/profile_test.go @@ -0,0 +1,21 @@ +package proc + +import ( + "log" + "strings" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestProfile(t *testing.T) { + var buf strings.Builder + log.SetOutput(&buf) + profiler := StartProfile() + // start again should not work + assert.NotNil(t, StartProfile()) + profiler.Stop() + // stop twice + profiler.Stop() + assert.True(t, strings.Contains(buf.String(), ".pprof")) +} diff --git a/example/rpc/server/unary/server.go b/example/rpc/server/unary/server.go index 75e9846f..115cf1c2 100644 --- a/example/rpc/server/unary/server.go +++ b/example/rpc/server/unary/server.go @@ -4,6 +4,7 @@ import ( "context" "flag" "fmt" + "log" "os" "sync" "time" @@ -50,5 +51,13 @@ func main() { server := zrpc.MustNewServer(c, func(grpcServer *grpc.Server) { unary.RegisterGreeterServer(grpcServer, NewGreetServer()) }) + interceptor := func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) { + st := time.Now() + resp, err = handler(ctx, req) + log.Printf("method: %s time: %v\n", info.FullMethod, time.Since(st)) + return resp, err + } + + server.AddUnaryInterceptors(interceptor) server.Start() } diff --git a/zrpc/server.go b/zrpc/server.go index 90c8150e..06aa08c9 100644 --- a/zrpc/server.go +++ b/zrpc/server.go @@ -67,15 +67,15 @@ func NewServer(c RpcServerConf, register internal.RegisterFn) (*RpcServer, error } func (rs *RpcServer) AddOptions(options ...grpc.ServerOption) { - rs.AddOptions(options...) + rs.server.AddOptions(options...) } func (rs *RpcServer) AddStreamInterceptors(interceptors ...grpc.StreamServerInterceptor) { - rs.AddStreamInterceptors(interceptors...) + rs.server.AddStreamInterceptors(interceptors...) } func (rs *RpcServer) AddUnaryInterceptors(interceptors ...grpc.UnaryServerInterceptor) { - rs.AddUnaryInterceptors(interceptors...) + rs.server.AddUnaryInterceptors(interceptors...) } func (rs *RpcServer) Start() {