Skip to content

Commit 63865a2

Browse files
larryanzhongalex
andauthored
feat(pubsub/pstest): support listening on custom address (#11606)
Co-authored-by: Alex Hong <9397363+hongalex@users.noreply.github.com>
1 parent 30f8af9 commit 63865a2

File tree

2 files changed

+77
-40
lines changed

2 files changed

+77
-40
lines changed

pubsub/pstest/fake.go

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -112,18 +112,33 @@ func NewServer(opts ...ServerReactorOption) *Server {
112112
return NewServerWithPort(0, opts...)
113113
}
114114

115-
// NewServerWithPort creates a new fake server running in the current process at the specified port.
115+
// NewServerWithPort creates a new fake server running in the current process at
116+
// the specified port.
116117
func NewServerWithPort(port int, opts ...ServerReactorOption) *Server {
117-
return NewServerWithCallback(port, func(*grpc.Server) { /* empty */ }, opts...)
118+
return NewServerWithAddress(fmt.Sprintf("localhost:%d", port), opts...)
118119
}
119120

120-
// NewServerWithCallback creates new fake server running in the current process at the specified port.
121-
// Before starting the server, the provided callback is called to allow caller to register additional fakes
122-
// into grpc server.
121+
// NewServerWithAddress creates a new fake server running in the current process
122+
// at the specified address (host and port).
123+
func NewServerWithAddress(address string, opts ...ServerReactorOption) *Server {
124+
return initNewServer(address, func(*grpc.Server) { /* empty */ }, opts...)
125+
}
126+
127+
// NewServerWithCallback creates new fake server running in the current process
128+
// at the specified port. Before starting the server, the provided callback is
129+
// called to allow caller to register additional fakes into grpc server.
123130
func NewServerWithCallback(port int, callback func(*grpc.Server), opts ...ServerReactorOption) *Server {
124-
srv, err := testutil.NewServerWithPort(port)
131+
return initNewServer(fmt.Sprintf("localhost:%d", port), callback, opts...)
132+
}
133+
134+
// NewServerByAddressWithCallback creates new fake server running in the current
135+
// process at with the provided address (host and port).
136+
// Before starting the server, the provided callback is called to allow caller
137+
// to register additional fakes into grpc server.
138+
func initNewServer(address string, callback func(*grpc.Server), opts ...ServerReactorOption) *Server {
139+
srv, err := testutil.NewServerWithAddress(address)
125140
if err != nil {
126-
panic(fmt.Sprintf("pstest.NewServerWithPort: %v", err))
141+
panic(fmt.Sprintf("pstest.initNewServer: %v", err))
127142
}
128143
reactorOptions := ReactorOptions{}
129144
for _, opt := range opts {

pubsub/pstest/fake_test.go

Lines changed: 55 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -41,42 +41,49 @@ import (
4141
)
4242

4343
func TestNewServerWithPort(t *testing.T) {
44-
// Allocate an available port to use with NewServerWithPort and then close it so it's available.
45-
// Note: There is no guarantee that the port does not become used between closing
46-
// the listener and creating the new server with NewServerWithPort, but the chances are
47-
// very small.
48-
l, err := net.Listen("tcp", ":0")
49-
if err != nil {
50-
t.Fatal(err)
51-
}
52-
port := l.Addr().(*net.TCPAddr).Port
53-
l.Close()
54-
55-
// Pass a non 0 port to demonstrate we can pass a hardcoded port for the server to listen on
44+
port := getFreePort(t)
5645
srv := NewServerWithPort(port)
46+
47+
conn, err := grpc.NewClient(srv.Addr, grpc.WithTransportCredentials(insecure.NewCredentials()))
5748
if err != nil {
5849
t.Fatal(err)
5950
}
60-
defer srv.Close()
61-
conn, err := grpc.Dial(srv.Addr, grpc.WithInsecure())
62-
if err != nil {
63-
t.Fatal(err)
64-
}
65-
defer conn.Close()
51+
52+
t.Cleanup(func() {
53+
conn.Close()
54+
srv.Close()
55+
})
6656
}
6757

68-
func TestNewServerWithCallback(t *testing.T) {
69-
// Allocate an available port to use with NewServerWithPort and then close it so it's available.
70-
// Note: There is no guarantee that the port does not become used between closing
71-
// the listener and creating the new server with NewServerWithPort, but the chances are
72-
// very small.
73-
l, err := net.Listen("tcp", ":0")
74-
if err != nil {
75-
t.Fatal(err)
58+
func TestNewServerWithAddress(t *testing.T) {
59+
hosts := []string{
60+
"",
61+
"0.0.0.0",
62+
"127.0.0.1",
63+
"localhost",
64+
}
65+
for _, h := range hosts {
66+
port := getFreePort(t)
67+
address := fmt.Sprintf("%s:%d", h, port)
68+
t.Run(fmt.Sprintf("Init new server succeed with address %s", address), func(t *testing.T) {
69+
srv := NewServerWithAddress(address)
70+
71+
conn, err := grpc.NewClient(srv.Addr, grpc.WithTransportCredentials(insecure.NewCredentials()))
72+
if err != nil {
73+
t.Fatal(err)
74+
}
75+
76+
t.Cleanup(func() {
77+
conn.Close()
78+
srv.Close()
79+
})
80+
})
7681
}
77-
port := l.Addr().(*net.TCPAddr).Port
78-
l.Close()
7982

83+
}
84+
85+
func TestNewServerWithCallback(t *testing.T) {
86+
port := getFreePort(t)
8087
additionalFake := struct {
8188
iampb.UnimplementedIAMPolicyServer
8289
}{}
@@ -90,20 +97,35 @@ func TestNewServerWithCallback(t *testing.T) {
9097

9198
// Pass a non 0 port to demonstrate we can pass a hardcoded port for the server to listen on
9299
srv := NewServerWithCallback(port, callback)
93-
if err != nil {
94-
t.Fatal(err)
95-
}
96-
defer srv.Close()
97100

98101
conn, err := grpc.NewClient(srv.Addr, grpc.WithTransportCredentials(insecure.NewCredentials()))
99102
if err != nil {
100103
t.Fatal(err)
101104
}
102-
defer conn.Close()
103105

104106
if !verifyCallback {
105107
t.Fatal("callback was not invoked")
106108
}
109+
110+
t.Cleanup(func() {
111+
conn.Close()
112+
srv.Close()
113+
})
114+
}
115+
116+
// getFreePort allocates an available port then close it so it's available.
117+
// Note: There is no guarantee that the port does not become used between closing
118+
// the listener and creating the new server with the invocation function, but
119+
// the chances are very small.
120+
func getFreePort(t *testing.T) int {
121+
l, err := net.Listen("tcp", ":0")
122+
if err != nil {
123+
t.Fatal(err)
124+
}
125+
port := l.Addr().(*net.TCPAddr).Port
126+
l.Close()
127+
128+
return port
107129
}
108130

109131
func TestTopics(t *testing.T) {

0 commit comments

Comments
 (0)