From 12f17a3e2e43453d1ab8f6dee9c1a6dcaf7d2bc1 Mon Sep 17 00:00:00 2001 From: yuemoxi <99347745@qq.com> Date: Sat, 28 Aug 2021 00:00:27 +0800 Subject: [PATCH] test(middleware/) add test (#1379) * test(middleware/) add test --- middleware/metrics/metrics_test.go | 18 ++++++ middleware/recovery/recovery_test.go | 19 +++++- middleware/tracing/metadata.go | 1 - middleware/tracing/metadata_test.go | 89 ++++++++++++++++++++++++++++ 4 files changed, 125 insertions(+), 2 deletions(-) create mode 100644 middleware/metrics/metrics_test.go create mode 100644 middleware/tracing/metadata_test.go diff --git a/middleware/metrics/metrics_test.go b/middleware/metrics/metrics_test.go new file mode 100644 index 000000000..10f5bfe34 --- /dev/null +++ b/middleware/metrics/metrics_test.go @@ -0,0 +1,18 @@ +package metrics + +import ( + "context" + "github.com/stretchr/testify/assert" + "testing" +) + +func TestMetrics(t *testing.T) { + next := func(ctx context.Context, req interface{}) (interface{}, error) { + return req.(string) + "https://go-kratos.dev", nil + } + _, err := Server()(next)(context.Background(), "test:") + assert.Equal(t, err, nil) + + _, err = Client()(next)(context.Background(), "test:") + assert.Equal(t, err, nil) +} diff --git a/middleware/recovery/recovery_test.go b/middleware/recovery/recovery_test.go index 4dc0b7886..11a9cacf8 100644 --- a/middleware/recovery/recovery_test.go +++ b/middleware/recovery/recovery_test.go @@ -2,7 +2,11 @@ package recovery import ( "context" + "fmt" "testing" + + "github.com/go-kratos/kratos/v2/errors" + "github.com/go-kratos/kratos/v2/log" ) func TestOnce(t *testing.T) { @@ -15,6 +19,19 @@ func TestOnce(t *testing.T) { next := func(ctx context.Context, req interface{}) (interface{}, error) { panic("panic reason") } - _, e := Recovery()(next)(context.Background(), "panic") + _, e := Recovery(WithLogger(log.DefaultLogger))(next)(context.Background(), "panic") t.Logf("succ and reason is %v", e) } + +func TestNotPanic(t *testing.T) { + next := func(ctx context.Context, req interface{}) (interface{}, error) { + return req.(string) + "https://go-kratos.dev", nil + } + + _, e := Recovery(WithHandler(func(ctx context.Context, req, err interface{}) error { + return errors.InternalServer("RECOVERY", fmt.Sprintf("panic triggered: %v", err)) + }))(next)(context.Background(), "notPanic") + if e != nil { + t.Errorf("e isn't nil") + } +} diff --git a/middleware/tracing/metadata.go b/middleware/tracing/metadata.go index 7e8cbe951..687d89fec 100644 --- a/middleware/tracing/metadata.go +++ b/middleware/tracing/metadata.go @@ -2,7 +2,6 @@ package tracing import ( "context" - "github.com/go-kratos/kratos/v2" "github.com/go-kratos/kratos/v2/metadata" "go.opentelemetry.io/otel/propagation" diff --git a/middleware/tracing/metadata_test.go b/middleware/tracing/metadata_test.go new file mode 100644 index 000000000..22a14993d --- /dev/null +++ b/middleware/tracing/metadata_test.go @@ -0,0 +1,89 @@ +package tracing + +import ( + "context" + "testing" + + "github.com/go-kratos/kratos/v2" + "github.com/go-kratos/kratos/v2/metadata" + + "github.com/stretchr/testify/assert" + "go.opentelemetry.io/otel/propagation" +) + +func TestMetadata_Inject(t *testing.T) { + type args struct { + appName string + carrier propagation.TextMapCarrier + } + tests := []struct { + name string + args args + want string + }{ + { + name: "https://go-kratos.dev", + args: args{"https://go-kratos.dev", propagation.HeaderCarrier{}}, + want: "https://go-kratos.dev", + }, + { + name: "https://github.com/go-kratos/kratos", + args: args{"https://github.com/go-kratos/kratos", propagation.HeaderCarrier{"mode": []string{"test"}}}, + want: "https://github.com/go-kratos/kratos", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + a := kratos.New(kratos.Name(tt.args.appName)) + ctx := kratos.NewContext(context.Background(), a) + var m = new(Metadata) + m.Inject(ctx, tt.args.carrier) + if res := tt.args.carrier.Get(serviceHeader); tt.want != res { + t.Errorf("Get(serviceHeader) :%s want: %s", res, tt.want) + } + }) + } +} + +func TestMetadata_Extract(t *testing.T) { + type args struct { + parent context.Context + carrier propagation.TextMapCarrier + } + tests := []struct { + name string + args args + want string + }{ + { + name: "https://go-kratos.dev", + args: args{ + parent: context.Background(), + carrier: propagation.HeaderCarrier{"X-Md-Service-Name": []string{"https://go-kratos.dev"}}, + }, + want: "https://go-kratos.dev", + }, + { + name: "https://github.com/go-kratos/kratos", + args: args{ + parent: metadata.NewServerContext(context.Background(), metadata.Metadata{}), + carrier: propagation.HeaderCarrier{"X-Md-Service-Name": []string{"https://github.com/go-kratos/kratos"}}, + }, + want: "https://github.com/go-kratos/kratos", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + b := Metadata{} + ctx := b.Extract(tt.args.parent, tt.args.carrier) + md, ok := metadata.FromServerContext(ctx) + assert.Equal(t, ok, true) + assert.Equal(t, md.Get(serviceHeader), tt.want) + }) + } +} + +func TestFields(t *testing.T) { + b := Metadata{} + assert.Equal(t, b.Fields(), []string{"x-md-service-name"}) +}