From f1b7c8b33bc135c6cb8f21cdec586b25d81ea214 Mon Sep 17 00:00:00 2001 From: Danny Cao Date: Mon, 19 Jul 2021 19:19:23 -0400 Subject: [PATCH] feat(pubsub/pstest): add ability to create a pstest server listening on (#4459) a specified port Signed-off-by: Danny Cao --- pubsub/pstest/examples_test.go | 20 ++++++++++++++++++++ pubsub/pstest/fake.go | 9 +++++++-- pubsub/pstest/fake_test.go | 26 ++++++++++++++++++++++++++ 3 files changed, 53 insertions(+), 2 deletions(-) diff --git a/pubsub/pstest/examples_test.go b/pubsub/pstest/examples_test.go index 56a30c1242b..b98ab3801af 100644 --- a/pubsub/pstest/examples_test.go +++ b/pubsub/pstest/examples_test.go @@ -42,3 +42,23 @@ func ExampleNewServer() { defer client.Close() _ = client // TODO: Use the client. } + +func ExampleNewServerWithPort() { + ctx := context.Background() + // Start a fake server running locally at 9001. + srv := pstest.NewServerWithPort(9001) + defer srv.Close() + // Connect to the server without using TLS. + conn, err := grpc.Dial(srv.Addr, grpc.WithInsecure()) + if err != nil { + // TODO: Handle error. + } + defer conn.Close() + // Use the connection when creating a pubsub client. + client, err := pubsub.NewClient(ctx, "project", option.WithGRPCConn(conn)) + if err != nil { + // TODO: Handle error. + } + defer client.Close() + _ = client // TODO: Use the client. +} diff --git a/pubsub/pstest/fake.go b/pubsub/pstest/fake.go index 23b5bc57101..b3c89074e6c 100644 --- a/pubsub/pstest/fake.go +++ b/pubsub/pstest/fake.go @@ -114,9 +114,14 @@ type GServer struct { // NewServer creates a new fake server running in the current process. func NewServer(opts ...ServerReactorOption) *Server { - srv, err := testutil.NewServer() + return NewServerWithPort(0, opts...) +} + +// NewServerWithPort creates a new fake server running in the current process at the specified port. +func NewServerWithPort(port int, opts ...ServerReactorOption) *Server { + srv, err := testutil.NewServerWithPort(port) if err != nil { - panic(fmt.Sprintf("pstest.NewServer: %v", err)) + panic(fmt.Sprintf("pstest.NewServerWithPort: %v", err)) } reactorOptions := ReactorOptions{} for _, opt := range opts { diff --git a/pubsub/pstest/fake_test.go b/pubsub/pstest/fake_test.go index 279bcc0a972..83a6f481e06 100644 --- a/pubsub/pstest/fake_test.go +++ b/pubsub/pstest/fake_test.go @@ -18,6 +18,7 @@ import ( "context" "fmt" "io" + "net" "reflect" "strings" "sync" @@ -34,6 +35,31 @@ import ( "google.golang.org/protobuf/types/known/timestamppb" ) +func TestNewServerWithPort(t *testing.T) { + // Allocate an available port to use with NewServerWithPort and then close it so it's available. + // Note: There is no guarantee that the port does not become used between closing + // the listener and creating the new server with NewServerWithPort, but the chances are + // very small. + l, err := net.Listen("tcp", ":0") + if err != nil { + t.Fatal(err) + } + port := l.Addr().(*net.TCPAddr).Port + l.Close() + + // Pass a non 0 port to demonstrate we can pass a hardcoded port for the server to listen on + srv := NewServerWithPort(port) + if err != nil { + t.Fatal(err) + } + defer srv.Close() + conn, err := grpc.Dial(srv.Addr, grpc.WithInsecure()) + if err != nil { + t.Fatal(err) + } + defer conn.Close() +} + func TestTopics(t *testing.T) { pclient, _, server, cleanup := newFake(context.TODO(), t) defer cleanup()