@@ -2,11 +2,15 @@ package serverinterceptors
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"net"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/tal-tech/go-zero/core/lang"
|
||||||
"github.com/tal-tech/go-zero/core/stat"
|
"github.com/tal-tech/go-zero/core/stat"
|
||||||
"google.golang.org/grpc"
|
"google.golang.org/grpc"
|
||||||
|
"google.golang.org/grpc/peer"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestUnaryStatInterceptor(t *testing.T) {
|
func TestUnaryStatInterceptor(t *testing.T) {
|
||||||
@@ -30,3 +34,51 @@ func TestUnaryStatInterceptor_crash(t *testing.T) {
|
|||||||
})
|
})
|
||||||
assert.NotNil(t, err)
|
assert.NotNil(t, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestLogDuration(t *testing.T) {
|
||||||
|
addrs, err := net.InterfaceAddrs()
|
||||||
|
assert.Nil(t, err)
|
||||||
|
assert.True(t, len(addrs) > 0)
|
||||||
|
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
ctx context.Context
|
||||||
|
req interface{}
|
||||||
|
duration time.Duration
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "normal",
|
||||||
|
ctx: context.Background(),
|
||||||
|
req: "foo",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "bad req",
|
||||||
|
ctx: context.Background(),
|
||||||
|
req: make(chan lang.PlaceholderType), // not marshalable
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "timeout",
|
||||||
|
ctx: context.Background(),
|
||||||
|
req: "foo",
|
||||||
|
duration: time.Second,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "timeout",
|
||||||
|
ctx: peer.NewContext(context.Background(), &peer.Peer{
|
||||||
|
Addr: addrs[0],
|
||||||
|
}),
|
||||||
|
req: "foo",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, test := range tests {
|
||||||
|
test := test
|
||||||
|
t.Run(test.name, func(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
assert.NotPanics(t, func() {
|
||||||
|
logDuration(test.ctx, "foo", test.req, test.duration)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -2,6 +2,8 @@ package serverinterceptors
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"errors"
|
||||||
|
"io"
|
||||||
"sync"
|
"sync"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
"testing"
|
"testing"
|
||||||
@@ -9,7 +11,9 @@ import (
|
|||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/tal-tech/go-zero/core/trace"
|
"github.com/tal-tech/go-zero/core/trace"
|
||||||
"google.golang.org/grpc"
|
"google.golang.org/grpc"
|
||||||
|
"google.golang.org/grpc/codes"
|
||||||
"google.golang.org/grpc/metadata"
|
"google.golang.org/grpc/metadata"
|
||||||
|
"google.golang.org/grpc/status"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestUnaryOpenTracingInterceptor_Disable(t *testing.T) {
|
func TestUnaryOpenTracingInterceptor_Disable(t *testing.T) {
|
||||||
@@ -52,6 +56,42 @@ func TestUnaryTracingInterceptor(t *testing.T) {
|
|||||||
assert.Equal(t, int32(1), atomic.LoadInt32(&run))
|
assert.Equal(t, int32(1), atomic.LoadInt32(&run))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestUnaryTracingInterceptor_WithError(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
err error
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "normal error",
|
||||||
|
err: errors.New("dummy"),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "grpc error",
|
||||||
|
err: status.Error(codes.DataLoss, "dummy"),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, test := range tests {
|
||||||
|
test := test
|
||||||
|
t.Run(test.name, func(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
var wg sync.WaitGroup
|
||||||
|
wg.Add(1)
|
||||||
|
var md metadata.MD
|
||||||
|
ctx := metadata.NewIncomingContext(context.Background(), md)
|
||||||
|
_, err := UnaryTracingInterceptor(ctx, nil, &grpc.UnaryServerInfo{
|
||||||
|
FullMethod: "/",
|
||||||
|
}, func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||||
|
defer wg.Done()
|
||||||
|
return nil, test.err
|
||||||
|
})
|
||||||
|
wg.Wait()
|
||||||
|
assert.Equal(t, test.err, err)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestStreamTracingInterceptor_GrpcFormat(t *testing.T) {
|
func TestStreamTracingInterceptor_GrpcFormat(t *testing.T) {
|
||||||
var run int32
|
var run int32
|
||||||
var wg sync.WaitGroup
|
var wg sync.WaitGroup
|
||||||
@@ -71,8 +111,141 @@ func TestStreamTracingInterceptor_GrpcFormat(t *testing.T) {
|
|||||||
assert.Equal(t, int32(1), atomic.LoadInt32(&run))
|
assert.Equal(t, int32(1), atomic.LoadInt32(&run))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestStreamTracingInterceptor_FinishWithGrpcError(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
err error
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "receive event",
|
||||||
|
err: status.Error(codes.DataLoss, "dummy"),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "error event",
|
||||||
|
err: status.Error(codes.DataLoss, "dummy"),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, test := range tests {
|
||||||
|
test := test
|
||||||
|
t.Run(test.name, func(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
var wg sync.WaitGroup
|
||||||
|
wg.Add(1)
|
||||||
|
var md metadata.MD
|
||||||
|
ctx := metadata.NewIncomingContext(context.Background(), md)
|
||||||
|
stream := mockedServerStream{ctx: ctx}
|
||||||
|
err := StreamTracingInterceptor(nil, &stream, &grpc.StreamServerInfo{
|
||||||
|
FullMethod: "/foo",
|
||||||
|
}, func(srv interface{}, stream grpc.ServerStream) error {
|
||||||
|
defer wg.Done()
|
||||||
|
return test.err
|
||||||
|
})
|
||||||
|
wg.Wait()
|
||||||
|
assert.Equal(t, test.err, err)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestStreamTracingInterceptor_WithError(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
err error
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "normal error",
|
||||||
|
err: errors.New("dummy"),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "grpc error",
|
||||||
|
err: status.Error(codes.DataLoss, "dummy"),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, test := range tests {
|
||||||
|
test := test
|
||||||
|
t.Run(test.name, func(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
var wg sync.WaitGroup
|
||||||
|
wg.Add(1)
|
||||||
|
var md metadata.MD
|
||||||
|
ctx := metadata.NewIncomingContext(context.Background(), md)
|
||||||
|
stream := mockedServerStream{ctx: ctx}
|
||||||
|
err := StreamTracingInterceptor(nil, &stream, &grpc.StreamServerInfo{
|
||||||
|
FullMethod: "/foo",
|
||||||
|
}, func(srv interface{}, stream grpc.ServerStream) error {
|
||||||
|
defer wg.Done()
|
||||||
|
return test.err
|
||||||
|
})
|
||||||
|
wg.Wait()
|
||||||
|
assert.Equal(t, test.err, err)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestClientStream_RecvMsg(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
err error
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "nil error",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "EOF",
|
||||||
|
err: io.EOF,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "dummy error",
|
||||||
|
err: errors.New("dummy"),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, test := range tests {
|
||||||
|
test := test
|
||||||
|
t.Run(test.name, func(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
stream := wrapServerStream(context.Background(), &mockedServerStream{
|
||||||
|
ctx: context.Background(),
|
||||||
|
err: test.err,
|
||||||
|
})
|
||||||
|
assert.Equal(t, test.err, stream.RecvMsg(nil))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestServerStream_SendMsg(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
err error
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "nil error",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "with error",
|
||||||
|
err: errors.New("dummy"),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, test := range tests {
|
||||||
|
test := test
|
||||||
|
t.Run(test.name, func(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
stream := wrapServerStream(context.Background(), &mockedServerStream{
|
||||||
|
ctx: context.Background(),
|
||||||
|
err: test.err,
|
||||||
|
})
|
||||||
|
assert.Equal(t, test.err, stream.SendMsg(nil))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
type mockedServerStream struct {
|
type mockedServerStream struct {
|
||||||
ctx context.Context
|
ctx context.Context
|
||||||
|
err error
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *mockedServerStream) SetHeader(md metadata.MD) error {
|
func (m *mockedServerStream) SetHeader(md metadata.MD) error {
|
||||||
@@ -96,9 +269,9 @@ func (m *mockedServerStream) Context() context.Context {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (m *mockedServerStream) SendMsg(v interface{}) error {
|
func (m *mockedServerStream) SendMsg(v interface{}) error {
|
||||||
panic("implement me")
|
return m.err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *mockedServerStream) RecvMsg(v interface{}) error {
|
func (m *mockedServerStream) RecvMsg(v interface{}) error {
|
||||||
panic("implement me")
|
return m.err
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user