From 0b6a13fe844dbb6cf53731ecef2239d1f53806a1 Mon Sep 17 00:00:00 2001 From: kevin Date: Thu, 20 Aug 2020 22:53:18 +0800 Subject: [PATCH] add more tests --- .../crashinterceptor_test.go | 31 +++++++++++++++++++ .../prommetricinterceptor.go | 4 +-- .../statinterceptor_test.go | 22 +++++++++++++ .../timeoutinterceptor_test.go | 21 +++++++++++++ 4 files changed, 76 insertions(+), 2 deletions(-) create mode 100644 rpcx/internal/serverinterceptors/crashinterceptor_test.go create mode 100644 rpcx/internal/serverinterceptors/statinterceptor_test.go create mode 100644 rpcx/internal/serverinterceptors/timeoutinterceptor_test.go diff --git a/rpcx/internal/serverinterceptors/crashinterceptor_test.go b/rpcx/internal/serverinterceptors/crashinterceptor_test.go new file mode 100644 index 00000000..fc7882f3 --- /dev/null +++ b/rpcx/internal/serverinterceptors/crashinterceptor_test.go @@ -0,0 +1,31 @@ +package serverinterceptors + +import ( + "context" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/tal-tech/go-zero/core/logx" + "google.golang.org/grpc" +) + +func init() { + logx.Disable() +} + +func TestStreamCrashInterceptor(t *testing.T) { + err := StreamCrashInterceptor(nil, nil, nil, func( + srv interface{}, stream grpc.ServerStream) error { + panic("mock panic") + }) + assert.NotNil(t, err) +} + +func TestUnaryCrashInterceptor(t *testing.T) { + interceptor := UnaryCrashInterceptor() + _, err := interceptor(context.Background(), nil, nil, func( + ctx context.Context, req interface{}) (interface{}, error) { + panic("mock panic") + }) + assert.NotNil(t, err) +} diff --git a/rpcx/internal/serverinterceptors/prommetricinterceptor.go b/rpcx/internal/serverinterceptors/prommetricinterceptor.go index 7fe38f01..ed9fec6e 100644 --- a/rpcx/internal/serverinterceptors/prommetricinterceptor.go +++ b/rpcx/internal/serverinterceptors/prommetricinterceptor.go @@ -33,12 +33,12 @@ var ( ) func UnaryPromMetricInterceptor() grpc.UnaryServerInterceptor { - return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) { + return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) ( + interface{}, error) { startTime := timex.Now() resp, err := handler(ctx, req) metricServerReqDur.Observe(int64(timex.Since(startTime)/time.Millisecond), info.FullMethod) metricServerReqCodeTotal.Inc(info.FullMethod, strconv.Itoa(int(status.Code(err)))) return resp, err } - } diff --git a/rpcx/internal/serverinterceptors/statinterceptor_test.go b/rpcx/internal/serverinterceptors/statinterceptor_test.go new file mode 100644 index 00000000..b02764b2 --- /dev/null +++ b/rpcx/internal/serverinterceptors/statinterceptor_test.go @@ -0,0 +1,22 @@ +package serverinterceptors + +import ( + "context" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/tal-tech/go-zero/core/stat" + "google.golang.org/grpc" +) + +func TestUnaryStatInterceptor(t *testing.T) { + metrics := stat.NewMetrics("mock") + interceptor := UnaryStatInterceptor(metrics) + _, err := interceptor(context.Background(), nil, &grpc.UnaryServerInfo{ + FullMethod: "/", + }, func( + ctx context.Context, req interface{}) (interface{}, error) { + return nil, nil + }) + assert.Nil(t, err) +} diff --git a/rpcx/internal/serverinterceptors/timeoutinterceptor_test.go b/rpcx/internal/serverinterceptors/timeoutinterceptor_test.go new file mode 100644 index 00000000..84626185 --- /dev/null +++ b/rpcx/internal/serverinterceptors/timeoutinterceptor_test.go @@ -0,0 +1,21 @@ +package serverinterceptors + +import ( + "context" + "testing" + "time" + + "github.com/stretchr/testify/assert" + "google.golang.org/grpc" +) + +func TestUnaryTimeoutInterceptor(t *testing.T) { + interceptor := UnaryTimeoutInterceptor(time.Millisecond * 10) + _, err := interceptor(context.Background(), nil, &grpc.UnaryServerInfo{ + FullMethod: "/", + }, func( + ctx context.Context, req interface{}) (interface{}, error) { + return nil, nil + }) + assert.Nil(t, err) +}