From ccbabf6f581267f8d05b0c214548d21e55bcff2a Mon Sep 17 00:00:00 2001 From: kevin Date: Mon, 24 Aug 2020 18:18:58 +0800 Subject: [PATCH] add more tests --- .../timeoutinterceptor_test.go | 50 +++++++++++++++++ .../tracinginterceptor_test.go | 53 +++++++++++++++++++ 2 files changed, 103 insertions(+) create mode 100644 rpcx/internal/clientinterceptors/timeoutinterceptor_test.go create mode 100644 rpcx/internal/clientinterceptors/tracinginterceptor_test.go diff --git a/rpcx/internal/clientinterceptors/timeoutinterceptor_test.go b/rpcx/internal/clientinterceptors/timeoutinterceptor_test.go new file mode 100644 index 00000000..99a6b4c9 --- /dev/null +++ b/rpcx/internal/clientinterceptors/timeoutinterceptor_test.go @@ -0,0 +1,50 @@ +package clientinterceptors + +import ( + "context" + "strconv" + "sync" + "testing" + "time" + + "github.com/stretchr/testify/assert" + "google.golang.org/grpc" +) + +func TestTimeoutInterceptor(t *testing.T) { + timeouts := []time.Duration{0, time.Millisecond * 10} + for _, timeout := range timeouts { + t.Run(strconv.FormatInt(int64(timeout), 10), func(t *testing.T) { + interceptor := TimeoutInterceptor(timeout) + cc := new(grpc.ClientConn) + err := interceptor(context.Background(), "/foo", nil, nil, cc, + func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, + opts ...grpc.CallOption) error { + return nil + }, + ) + assert.Nil(t, err) + }) + } +} + +func TestTimeoutInterceptor_timeout(t *testing.T) { + const timeout = time.Millisecond * 10 + interceptor := TimeoutInterceptor(timeout) + ctx, cancel := context.WithTimeout(context.Background(), time.Minute) + defer cancel() + var wg sync.WaitGroup + wg.Add(1) + cc := new(grpc.ClientConn) + err := interceptor(ctx, "/foo", nil, nil, cc, + func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, + opts ...grpc.CallOption) error { + defer wg.Done() + tm, ok := ctx.Deadline() + assert.True(t, ok) + assert.True(t, tm.Before(time.Now().Add(timeout+time.Millisecond))) + return nil + }) + wg.Wait() + assert.Nil(t, err) +} diff --git a/rpcx/internal/clientinterceptors/tracinginterceptor_test.go b/rpcx/internal/clientinterceptors/tracinginterceptor_test.go new file mode 100644 index 00000000..2d92fc3a --- /dev/null +++ b/rpcx/internal/clientinterceptors/tracinginterceptor_test.go @@ -0,0 +1,53 @@ +package clientinterceptors + +import ( + "context" + "sync" + "sync/atomic" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/tal-tech/go-zero/core/trace" + "google.golang.org/grpc" + "google.golang.org/grpc/metadata" +) + +func TestTracingInterceptor(t *testing.T) { + var run int32 + var wg sync.WaitGroup + wg.Add(1) + cc := new(grpc.ClientConn) + err := TracingInterceptor(context.Background(), "/foo", nil, nil, cc, + func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, + opts ...grpc.CallOption) error { + defer wg.Done() + atomic.AddInt32(&run, 1) + return nil + }) + wg.Wait() + assert.Nil(t, err) + assert.Equal(t, int32(1), atomic.LoadInt32(&run)) +} + +func TestTracingInterceptor_GrpcFormat(t *testing.T) { + var run int32 + var wg sync.WaitGroup + wg.Add(1) + md := metadata.New(map[string]string{ + "foo": "bar", + }) + carrier, err := trace.Inject(trace.GrpcFormat, md) + assert.Nil(t, err) + ctx, _ := trace.StartServerSpan(context.Background(), carrier, "user", "/foo") + cc := new(grpc.ClientConn) + err = TracingInterceptor(ctx, "/foo", nil, nil, cc, + func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, + opts ...grpc.CallOption) error { + defer wg.Done() + atomic.AddInt32(&run, 1) + return nil + }) + wg.Wait() + assert.Nil(t, err) + assert.Equal(t, int32(1), atomic.LoadInt32(&run)) +}