From b82c02ed165a7c674791e4dcc7827515d908d3c0 Mon Sep 17 00:00:00 2001 From: kevin Date: Sat, 22 Aug 2020 23:08:33 +0800 Subject: [PATCH] add more tests --- .../authinterceptor_test.go | 55 +++++++++++++ .../crashinterceptor_test.go | 8 +- .../prommetricinterceptor_test.go | 19 +++++ .../sheddinginterceptor_test.go | 77 +++++++++++++++++++ .../statinterceptor_test.go | 14 +++- 5 files changed, 167 insertions(+), 6 deletions(-) create mode 100644 rpcx/internal/serverinterceptors/authinterceptor_test.go create mode 100644 rpcx/internal/serverinterceptors/prommetricinterceptor_test.go create mode 100644 rpcx/internal/serverinterceptors/sheddinginterceptor_test.go diff --git a/rpcx/internal/serverinterceptors/authinterceptor_test.go b/rpcx/internal/serverinterceptors/authinterceptor_test.go new file mode 100644 index 00000000..9932fc62 --- /dev/null +++ b/rpcx/internal/serverinterceptors/authinterceptor_test.go @@ -0,0 +1,55 @@ +package serverinterceptors + +import ( + "context" + "testing" + + "github.com/alicebob/miniredis" + "github.com/stretchr/testify/assert" + "github.com/tal-tech/go-zero/core/stores/redis" + "github.com/tal-tech/go-zero/rpcx/internal/auth" + "google.golang.org/grpc/metadata" +) + +func TestUnaryAuthorizeInterceptor(t *testing.T) { + tests := []struct { + name string + strict bool + }{ + { + name: "strict=true", + strict: true, + }, + { + name: "strict=false", + strict: false, + }, + } + + r := miniredis.NewMiniRedis() + assert.Nil(t, r.Start()) + defer r.Close() + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + store := redis.NewRedis(r.Addr(), redis.NodeType) + authenticator, err := auth.NewAuthenticator(store, "apps", test.strict) + assert.Nil(t, err) + interceptor := UnaryAuthorizeInterceptor(authenticator) + md := metadata.New(map[string]string{ + "app": "name", + "token": "key", + }) + ctx := metadata.NewIncomingContext(context.Background(), md) + _, err = interceptor(ctx, nil, nil, + func(ctx context.Context, req interface{}) (interface{}, error) { + return nil, nil + }) + if test.strict { + assert.NotNil(t, err) + } else { + assert.Nil(t, err) + } + }) + } +} diff --git a/rpcx/internal/serverinterceptors/crashinterceptor_test.go b/rpcx/internal/serverinterceptors/crashinterceptor_test.go index fc7882f3..3e66b764 100644 --- a/rpcx/internal/serverinterceptors/crashinterceptor_test.go +++ b/rpcx/internal/serverinterceptors/crashinterceptor_test.go @@ -23,9 +23,9 @@ func TestStreamCrashInterceptor(t *testing.T) { func TestUnaryCrashInterceptor(t *testing.T) { interceptor := UnaryCrashInterceptor() - _, err := interceptor(context.Background(), nil, nil, func( - ctx context.Context, req interface{}) (interface{}, error) { - panic("mock panic") - }) + _, 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_test.go b/rpcx/internal/serverinterceptors/prommetricinterceptor_test.go new file mode 100644 index 00000000..da95d728 --- /dev/null +++ b/rpcx/internal/serverinterceptors/prommetricinterceptor_test.go @@ -0,0 +1,19 @@ +package serverinterceptors + +import ( + "context" + "testing" + + "github.com/stretchr/testify/assert" + "google.golang.org/grpc" +) + +func TestUnaryPromMetricInterceptor(t *testing.T) { + interceptor := UnaryPromMetricInterceptor() + _, 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/sheddinginterceptor_test.go b/rpcx/internal/serverinterceptors/sheddinginterceptor_test.go new file mode 100644 index 00000000..7b64002e --- /dev/null +++ b/rpcx/internal/serverinterceptors/sheddinginterceptor_test.go @@ -0,0 +1,77 @@ +package serverinterceptors + +import ( + "context" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/tal-tech/go-zero/core/load" + "github.com/tal-tech/go-zero/core/stat" + "google.golang.org/grpc" +) + +func TestUnarySheddingInterceptor(t *testing.T) { + tests := []struct { + name string + allow bool + handleErr error + expect error + }{ + { + name: "allow", + allow: true, + handleErr: nil, + expect: nil, + }, + { + name: "allow", + allow: true, + handleErr: context.DeadlineExceeded, + expect: context.DeadlineExceeded, + }, + { + name: "reject", + allow: false, + handleErr: nil, + expect: load.ErrServiceOverloaded, + }, + } + + for _, test := range tests { + test := test + t.Run(test.name, func(t *testing.T) { + t.Parallel() + + shedder := mockedShedder{allow: test.allow} + metrics := stat.NewMetrics("mock") + interceptor := UnarySheddingInterceptor(shedder, metrics) + _, err := interceptor(context.Background(), nil, &grpc.UnaryServerInfo{ + FullMethod: "/", + }, func(ctx context.Context, req interface{}) (interface{}, error) { + return nil, test.handleErr + }) + assert.Equal(t, test.expect, err) + }) + } +} + +type mockedShedder struct { + allow bool +} + +func (m mockedShedder) Allow() (load.Promise, error) { + if m.allow { + return mockedPromise{}, nil + } else { + return nil, load.ErrServiceOverloaded + } +} + +type mockedPromise struct { +} + +func (m mockedPromise) Pass() { +} + +func (m mockedPromise) Fail() { +} diff --git a/rpcx/internal/serverinterceptors/statinterceptor_test.go b/rpcx/internal/serverinterceptors/statinterceptor_test.go index b02764b2..ec971a83 100644 --- a/rpcx/internal/serverinterceptors/statinterceptor_test.go +++ b/rpcx/internal/serverinterceptors/statinterceptor_test.go @@ -14,9 +14,19 @@ func TestUnaryStatInterceptor(t *testing.T) { interceptor := UnaryStatInterceptor(metrics) _, err := interceptor(context.Background(), nil, &grpc.UnaryServerInfo{ FullMethod: "/", - }, func( - ctx context.Context, req interface{}) (interface{}, error) { + }, func(ctx context.Context, req interface{}) (interface{}, error) { return nil, nil }) assert.Nil(t, err) } + +func TestUnaryStatInterceptor_crash(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) { + panic("error") + }) + assert.NotNil(t, err) +}