tests(coverage): Increase middleware tests coverage (#2165)
* tests(coverage): Increase tests coverage * Lint fixpull/2175/head
parent
dec323113f
commit
c9fbb27b5b
@ -0,0 +1,79 @@ |
||||
package circuitbreaker |
||||
|
||||
import ( |
||||
"context" |
||||
"errors" |
||||
"testing" |
||||
|
||||
kratos_errors "github.com/go-kratos/kratos/v2/errors" |
||||
"github.com/go-kratos/kratos/v2/internal/group" |
||||
"github.com/go-kratos/kratos/v2/transport" |
||||
) |
||||
|
||||
type transportMock struct { |
||||
kind transport.Kind |
||||
endpoint string |
||||
operation string |
||||
} |
||||
|
||||
type circuitBreakerMock struct { |
||||
err error |
||||
} |
||||
|
||||
func (tr *transportMock) Kind() transport.Kind { |
||||
return tr.kind |
||||
} |
||||
|
||||
func (tr *transportMock) Endpoint() string { |
||||
return tr.endpoint |
||||
} |
||||
|
||||
func (tr *transportMock) Operation() string { |
||||
return tr.operation |
||||
} |
||||
|
||||
func (tr *transportMock) RequestHeader() transport.Header { |
||||
return nil |
||||
} |
||||
|
||||
func (tr *transportMock) ReplyHeader() transport.Header { |
||||
return nil |
||||
} |
||||
|
||||
func (c *circuitBreakerMock) Allow() error { return c.err } |
||||
func (c *circuitBreakerMock) MarkSuccess() {} |
||||
func (c *circuitBreakerMock) MarkFailed() {} |
||||
|
||||
func Test_WithGroup(t *testing.T) { |
||||
o := options{ |
||||
group: group.NewGroup(func() interface{} { |
||||
return "" |
||||
}), |
||||
} |
||||
|
||||
WithGroup(nil)(&o) |
||||
if o.group != nil { |
||||
t.Error("The group property must be updated to nil.") |
||||
} |
||||
} |
||||
|
||||
func Test_Server(t *testing.T) { |
||||
nextValid := func(ctx context.Context, req interface{}) (interface{}, error) { |
||||
return "Hello valid", nil |
||||
} |
||||
nextInvalid := func(ctx context.Context, req interface{}) (interface{}, error) { |
||||
return nil, kratos_errors.InternalServer("", "") |
||||
} |
||||
|
||||
ctx := transport.NewClientContext(context.Background(), &transportMock{}) |
||||
|
||||
_, _ = Client(func(o *options) { |
||||
o.group = group.NewGroup(func() interface{} { |
||||
return &circuitBreakerMock{err: errors.New("circuitbreaker error")} |
||||
}) |
||||
})(nextValid)(ctx, nil) |
||||
|
||||
_, _ = Client(func(_ *options) {})(nextValid)(ctx, nil) |
||||
|
||||
_, _ = Client(func(_ *options) {})(nextInvalid)(ctx, nil) |
||||
} |
@ -0,0 +1,64 @@ |
||||
package ratelimit |
||||
|
||||
import ( |
||||
"context" |
||||
"errors" |
||||
"testing" |
||||
|
||||
"github.com/go-kratos/aegis/ratelimit" |
||||
) |
||||
|
||||
type ( |
||||
ratelimitMock struct { |
||||
reached bool |
||||
} |
||||
ratelimitReachedMock struct { |
||||
reached bool |
||||
} |
||||
) |
||||
|
||||
func (r *ratelimitMock) Allow() (ratelimit.DoneFunc, error) { |
||||
return func(_ ratelimit.DoneInfo) { |
||||
r.reached = true |
||||
}, nil |
||||
} |
||||
|
||||
func (r *ratelimitReachedMock) Allow() (ratelimit.DoneFunc, error) { |
||||
return func(_ ratelimit.DoneInfo) { |
||||
r.reached = true |
||||
}, errors.New("errored") |
||||
} |
||||
|
||||
func Test_WithLimiter(t *testing.T) { |
||||
o := options{ |
||||
limiter: &ratelimitMock{}, |
||||
} |
||||
|
||||
WithLimiter(nil)(&o) |
||||
if o.limiter != nil { |
||||
t.Error("The limiter property must be updated.") |
||||
} |
||||
} |
||||
|
||||
func Test_Server(t *testing.T) { |
||||
nextValid := func(ctx context.Context, req interface{}) (interface{}, error) { |
||||
return "Hello valid", nil |
||||
} |
||||
|
||||
rlm := &ratelimitMock{} |
||||
rlrm := &ratelimitReachedMock{} |
||||
|
||||
_, _ = Server(func(o *options) { |
||||
o.limiter = rlm |
||||
})(nextValid)(context.Background(), nil) |
||||
if !rlm.reached { |
||||
t.Error("The ratelimit must run the done function.") |
||||
} |
||||
|
||||
_, _ = Server(func(o *options) { |
||||
o.limiter = rlrm |
||||
})(nextValid)(context.Background(), nil) |
||||
if rlrm.reached { |
||||
t.Error("The ratelimit must not run the done function and should be denied.") |
||||
} |
||||
} |
@ -0,0 +1,81 @@ |
||||
package tracing |
||||
|
||||
import ( |
||||
"context" |
||||
"net" |
||||
"testing" |
||||
|
||||
"go.opentelemetry.io/otel/trace" |
||||
"google.golang.org/grpc/peer" |
||||
"google.golang.org/grpc/stats" |
||||
) |
||||
|
||||
type ctxKey string |
||||
|
||||
const testKey ctxKey = "MY_TEST_KEY" |
||||
|
||||
func Test_Client_HandleConn(t *testing.T) { |
||||
(&ClientHandler{}).HandleConn(context.Background(), nil) |
||||
} |
||||
|
||||
func Test_Client_TagConn(t *testing.T) { |
||||
client := &ClientHandler{} |
||||
ctx := context.WithValue(context.Background(), testKey, 123) |
||||
|
||||
if client.TagConn(ctx, nil).Value(testKey) != 123 { |
||||
t.Errorf(`The context value must be 123 for the "MY_KEY_TEST" key, %v given.`, client.TagConn(ctx, nil).Value(testKey)) |
||||
} |
||||
} |
||||
|
||||
func Test_Client_TagRPC(t *testing.T) { |
||||
client := &ClientHandler{} |
||||
ctx := context.WithValue(context.Background(), testKey, 123) |
||||
|
||||
if client.TagRPC(ctx, nil).Value(testKey) != 123 { |
||||
t.Errorf(`The context value must be 123 for the "MY_KEY_TEST" key, %v given.`, client.TagConn(ctx, nil).Value(testKey)) |
||||
} |
||||
} |
||||
|
||||
type ( |
||||
mockSpan struct { |
||||
trace.Span |
||||
mockSpanCtx *trace.SpanContext |
||||
} |
||||
) |
||||
|
||||
func (m *mockSpan) SpanContext() trace.SpanContext { |
||||
return *m.mockSpanCtx |
||||
} |
||||
|
||||
func Test_Client_HandleRPC(t *testing.T) { |
||||
client := &ClientHandler{} |
||||
ctx := context.Background() |
||||
rs := stats.OutHeader{} |
||||
|
||||
// Handle stats.RPCStats is not type of stats.OutHeader case
|
||||
client.HandleRPC(context.TODO(), nil) |
||||
|
||||
// Handle context doesn't have the peerkey filled with a Peer instance
|
||||
client.HandleRPC(ctx, &rs) |
||||
|
||||
// Handle context with the peerkey filled with a Peer instance
|
||||
ip, _ := net.ResolveIPAddr("ip", "1.1.1.1") |
||||
ctx = peer.NewContext(ctx, &peer.Peer{ |
||||
Addr: ip, |
||||
}) |
||||
client.HandleRPC(ctx, &rs) |
||||
|
||||
// Handle context with Span
|
||||
_, span := trace.NewNoopTracerProvider().Tracer("Tracer").Start(ctx, "Spanname") |
||||
spanCtx := trace.SpanContext{} |
||||
spanID := [8]byte{12, 12, 12, 12, 12, 12, 12, 12} |
||||
traceID := [16]byte{12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12} |
||||
spanCtx = spanCtx.WithTraceID(traceID) |
||||
spanCtx = spanCtx.WithSpanID(spanID) |
||||
mSpan := mockSpan{ |
||||
Span: span, |
||||
mockSpanCtx: &spanCtx, |
||||
} |
||||
ctx = trace.ContextWithSpan(ctx, &mSpan) |
||||
client.HandleRPC(ctx, &rs) |
||||
} |
@ -0,0 +1,54 @@ |
||||
package tracing |
||||
|
||||
import ( |
||||
"context" |
||||
"errors" |
||||
"testing" |
||||
|
||||
"github.com/go-kratos/kratos/v2/internal/testdata/binding" |
||||
"go.opentelemetry.io/otel/trace" |
||||
) |
||||
|
||||
func Test_NewTracer(t *testing.T) { |
||||
tracer := NewTracer(trace.SpanKindClient, func(o *options) { |
||||
o.tracerProvider = trace.NewNoopTracerProvider() |
||||
}) |
||||
|
||||
if tracer.kind != trace.SpanKindClient { |
||||
t.Errorf("The tracer kind must be equal to trace.SpanKindClient, %v given.", tracer.kind) |
||||
} |
||||
|
||||
defer func() { |
||||
if recover() == nil { |
||||
t.Error("The NewTracer with an invalid SpanKindMustCrash must panic") |
||||
} |
||||
}() |
||||
_ = NewTracer(666, func(o *options) { |
||||
o.tracerProvider = trace.NewNoopTracerProvider() |
||||
}) |
||||
} |
||||
|
||||
func Test_Tracer_End(t *testing.T) { |
||||
tracer := NewTracer(trace.SpanKindClient, func(o *options) { |
||||
o.tracerProvider = trace.NewNoopTracerProvider() |
||||
}) |
||||
ctx, span := trace.NewNoopTracerProvider().Tracer("noop").Start(context.Background(), "noopSpan") |
||||
|
||||
// Handle with error case
|
||||
tracer.End(ctx, span, nil, errors.New("dummy error")) |
||||
|
||||
// Handle without error case
|
||||
tracer.End(ctx, span, nil, nil) |
||||
|
||||
m := &binding.HelloRequest{} |
||||
|
||||
// Handle the trace KindServer
|
||||
tracer = NewTracer(trace.SpanKindServer, func(o *options) { |
||||
o.tracerProvider = trace.NewNoopTracerProvider() |
||||
}) |
||||
tracer.End(ctx, span, m, nil) |
||||
tracer = NewTracer(trace.SpanKindClient, func(o *options) { |
||||
o.tracerProvider = trace.NewNoopTracerProvider() |
||||
}) |
||||
tracer.End(ctx, span, m, nil) |
||||
} |
Loading…
Reference in new issue