Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(pubsub/pstest): add ability to create a pstest server listening on a specified port #4459

Merged
merged 1 commit into from Jul 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 20 additions & 0 deletions pubsub/pstest/examples_test.go
Expand Up @@ -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.
}
9 changes: 7 additions & 2 deletions pubsub/pstest/fake.go
Expand Up @@ -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))
hongalex marked this conversation as resolved.
Show resolved Hide resolved
panic(fmt.Sprintf("pstest.NewServerWithPort: %v", err))
}
reactorOptions := ReactorOptions{}
for _, opt := range opts {
Expand Down
26 changes: 26 additions & 0 deletions pubsub/pstest/fake_test.go
Expand Up @@ -18,6 +18,7 @@ import (
"context"
"fmt"
"io"
"net"
"reflect"
"strings"
"sync"
Expand All @@ -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()
Expand Down