Skip to content

Commit db8216e

Browse files
tpokkihongalex
andauthored
feat(pubsub/pstest): add support to register other servers into grpc.Server (#9722)
Co-authored-by: Alex Hong <9397363+hongalex@users.noreply.github.com>
1 parent 292e812 commit db8216e

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

pubsub/pstest/fake.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import (
3737
"cloud.google.com/go/internal/testutil"
3838
pb "cloud.google.com/go/pubsub/apiv1/pubsubpb"
3939
"go.einride.tech/aip/filtering"
40+
"google.golang.org/grpc"
4041
"google.golang.org/grpc/codes"
4142
"google.golang.org/grpc/status"
4243
durpb "google.golang.org/protobuf/types/known/durationpb"
@@ -111,6 +112,13 @@ func NewServer(opts ...ServerReactorOption) *Server {
111112

112113
// NewServerWithPort creates a new fake server running in the current process at the specified port.
113114
func NewServerWithPort(port int, opts ...ServerReactorOption) *Server {
115+
return NewServerWithCallback(port, func(*grpc.Server) { /* empty */ }, opts...)
116+
}
117+
118+
// NewServerWithCallback creates new fake server running in the current process at the specified port.
119+
// Before starting the server, the provided callback is called to allow caller to register additional fakes
120+
// into grpc server.
121+
func NewServerWithCallback(port int, callback func(*grpc.Server), opts ...ServerReactorOption) *Server {
114122
srv, err := testutil.NewServerWithPort(port)
115123
if err != nil {
116124
panic(fmt.Sprintf("pstest.NewServerWithPort: %v", err))
@@ -136,6 +144,9 @@ func NewServerWithPort(port int, opts ...ServerReactorOption) *Server {
136144
pb.RegisterPublisherServer(srv.Gsrv, &s.GServer)
137145
pb.RegisterSubscriberServer(srv.Gsrv, &s.GServer)
138146
pb.RegisterSchemaServiceServer(srv.Gsrv, &s.GServer)
147+
148+
callback(srv.Gsrv)
149+
139150
srv.Start()
140151
return s
141152
}

pubsub/pstest/fake_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,12 @@ import (
2727
"testing"
2828
"time"
2929

30+
iampb "cloud.google.com/go/iam/apiv1/iampb"
3031
"cloud.google.com/go/internal/testutil"
3132
pb "cloud.google.com/go/pubsub/apiv1/pubsubpb"
3233
"google.golang.org/grpc"
3334
"google.golang.org/grpc/codes"
35+
"google.golang.org/grpc/credentials/insecure"
3436
"google.golang.org/grpc/status"
3537
"google.golang.org/protobuf/types/known/durationpb"
3638
field_mask "google.golang.org/protobuf/types/known/fieldmaskpb"
@@ -62,6 +64,47 @@ func TestNewServerWithPort(t *testing.T) {
6264
defer conn.Close()
6365
}
6466

67+
func TestNewServerWithCallback(t *testing.T) {
68+
// Allocate an available port to use with NewServerWithPort and then close it so it's available.
69+
// Note: There is no guarantee that the port does not become used between closing
70+
// the listener and creating the new server with NewServerWithPort, but the chances are
71+
// very small.
72+
l, err := net.Listen("tcp", ":0")
73+
if err != nil {
74+
t.Fatal(err)
75+
}
76+
port := l.Addr().(*net.TCPAddr).Port
77+
l.Close()
78+
79+
additionalFake := struct {
80+
iampb.UnimplementedIAMPolicyServer
81+
}{}
82+
83+
verifyCallback := false
84+
callback := func(grpc *grpc.Server) {
85+
// register something
86+
iampb.RegisterIAMPolicyServer(grpc, &additionalFake)
87+
verifyCallback = true
88+
}
89+
90+
// Pass a non 0 port to demonstrate we can pass a hardcoded port for the server to listen on
91+
srv := NewServerWithCallback(port, callback)
92+
if err != nil {
93+
t.Fatal(err)
94+
}
95+
defer srv.Close()
96+
97+
conn, err := grpc.NewClient(srv.Addr, grpc.WithTransportCredentials(insecure.NewCredentials()))
98+
if err != nil {
99+
t.Fatal(err)
100+
}
101+
defer conn.Close()
102+
103+
if !verifyCallback {
104+
t.Fatal("callback was not invoked")
105+
}
106+
}
107+
65108
func TestTopics(t *testing.T) {
66109
pclient, sclient, server, cleanup := newFake(context.TODO(), t)
67110
defer cleanup()

0 commit comments

Comments
 (0)