Skip to content

Commit

Permalink
Use bufconn to stub a ContextDialer
Browse files Browse the repository at this point in the history
  • Loading branch information
MrAlias committed Jan 27, 2023
1 parent 19d812a commit d18cfe2
Showing 1 changed file with 29 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"context"
"errors"
"io"
"net"
"strings"
"testing"
"time"
Expand All @@ -39,6 +40,7 @@ import (
"google.golang.org/grpc/interop/grpc_testing"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"
"google.golang.org/grpc/test/bufconn"
)

func getSpanFromRecorder(sr *tracetest.SpanRecorder, name string) (trace.ReadOnlySpan, bool) {
Expand All @@ -65,11 +67,21 @@ func (mcuici *mockUICInvoker) invoker(ctx context.Context, method string, req, r
return nil
}

func ctxDialer() func(context.Context, string) (net.Conn, error) {
l := bufconn.Listen(0)
return func(ctx context.Context, _ string) (net.Conn, error) {
return l.DialContext(ctx)
}
}

func TestUnaryClientInterceptor(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(cancel)

clientConn, err := grpc.DialContext(ctx, "fake:8906", grpc.WithTransportCredentials(insecure.NewCredentials()))
clientConn, err := grpc.DialContext(ctx, "fake:8906",
grpc.WithContextDialer(ctxDialer()),
grpc.WithTransportCredentials(insecure.NewCredentials()),
)
if err != nil {
t.Fatalf("failed to create client connection: %v", err)
}
Expand Down Expand Up @@ -290,7 +302,10 @@ func createInterceptedStreamClient(t *testing.T, method string, opts clientStrea
t.Cleanup(cancel)

mockStream := newMockClientStream(opts)
clientConn, err := grpc.DialContext(ctx, "fake:8906", grpc.WithTransportCredentials(insecure.NewCredentials()))
clientConn, err := grpc.DialContext(ctx, "fake:8906",
grpc.WithContextDialer(ctxDialer()),
grpc.WithTransportCredentials(insecure.NewCredentials()),
)
if err != nil {
t.Fatalf("failed to create client connection: %v", err)
}
Expand Down Expand Up @@ -321,7 +336,7 @@ func createInterceptedStreamClient(t *testing.T, method string, opts clientStrea
}

func TestStreamClientInterceptorOnBIDIStream(t *testing.T) {
t.Cleanup(func() { goleak.VerifyNone(t) })
defer goleak.VerifyNone(t)

method := "/github.com.serviceName/bar"
name := "github.com.serviceName/bar"
Expand Down Expand Up @@ -387,7 +402,7 @@ func TestStreamClientInterceptorOnBIDIStream(t *testing.T) {
}

func TestStreamClientInterceptorOnUnidirectionalClientServerStream(t *testing.T) {
t.Cleanup(func() { goleak.VerifyNone(t) })
defer goleak.VerifyNone(t)

method := "/github.com.serviceName/bar"
name := "github.com.serviceName/bar"
Expand Down Expand Up @@ -454,12 +469,15 @@ func TestStreamClientInterceptorOnUnidirectionalClientServerStream(t *testing.T)
// TestStreamClientInterceptorCancelContext tests a cancel context situation.
// There should be no goleaks.
func TestStreamClientInterceptorCancelContext(t *testing.T) {
t.Cleanup(func() { goleak.VerifyNone(t) })
defer goleak.VerifyNone(t)

ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(cancel)

clientConn, err := grpc.DialContext(ctx, "fake:8906", grpc.WithTransportCredentials(insecure.NewCredentials()))
clientConn, err := grpc.DialContext(ctx, "fake:8906",
grpc.WithContextDialer(ctxDialer()),
grpc.WithTransportCredentials(insecure.NewCredentials()),
)
if err != nil {
t.Fatalf("failed to create client connection: %v", err)
}
Expand Down Expand Up @@ -510,12 +528,15 @@ func TestStreamClientInterceptorCancelContext(t *testing.T) {

// TestStreamClientInterceptorWithError tests a situation that streamer returns an error.
func TestStreamClientInterceptorWithError(t *testing.T) {
t.Cleanup(func() { goleak.VerifyNone(t) })
defer goleak.VerifyNone(t)

ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(cancel)

clientConn, err := grpc.DialContext(ctx, "fake:8906", grpc.WithTransportCredentials(insecure.NewCredentials()))
clientConn, err := grpc.DialContext(ctx, "fake:8906",
grpc.WithContextDialer(ctxDialer()),
grpc.WithTransportCredentials(insecure.NewCredentials()),
)
if err != nil {
t.Fatalf("failed to create client connection: %v", err)
}
Expand Down

0 comments on commit d18cfe2

Please sign in to comment.