Skip to content

Commit 761bd1b

Browse files
larryanzhongalex
andauthored
feat: support listening on custom host (testutil/server) (#11586)
Co-authored-by: Alex Hong <9397363+hongalex@users.noreply.github.com>
1 parent 0e56303 commit 761bd1b

File tree

2 files changed

+51
-8
lines changed

2 files changed

+51
-8
lines changed

internal/testutil/server.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,16 +52,24 @@ type Server struct {
5252
Gsrv *grpc.Server
5353
}
5454

55-
// NewServer creates a new Server. The Server will be listening for gRPC connections
56-
// at the address named by the Addr field, without TLS.
55+
// NewServer creates a new Server on localhost. The Server will be listening for
56+
// gRPC connections at the address named by the Addr field, without TLS.
5757
func NewServer(opts ...grpc.ServerOption) (*Server, error) {
5858
return NewServerWithPort(0, opts...)
5959
}
6060

61-
// NewServerWithPort creates a new Server at a specific port. The Server will be listening
62-
// for gRPC connections at the address named by the Addr field, without TLS.
61+
// NewServerWithPort creates a new Server on localhost at a specific port. The
62+
// Server will be listening for gRPC connections at the address named by the
63+
// Addr field, without TLS.
6364
func NewServerWithPort(port int, opts ...grpc.ServerOption) (*Server, error) {
64-
l, err := net.Listen("tcp", fmt.Sprintf("localhost:%d", port))
65+
return NewServerWithAddress(fmt.Sprintf("localhost:%d", port))
66+
}
67+
68+
// NewServerWithAddress creates a new Server with a specific address (host and
69+
// port). The Server will be listening for gRPC connections at the address named
70+
// by the Addr field, without TLS.
71+
func NewServerWithAddress(addr string, opts ...grpc.ServerOption) (*Server, error) {
72+
l, err := net.Listen("tcp", addr)
6573
if err != nil {
6674
return nil, err
6775
}

internal/testutil/server_test.go

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,12 @@
1515
package testutil
1616

1717
import (
18+
"fmt"
1819
"testing"
1920

2021
grpc "google.golang.org/grpc"
2122
"google.golang.org/grpc/codes"
23+
"google.golang.org/grpc/credentials/insecure"
2224
"google.golang.org/grpc/status"
2325
)
2426

@@ -27,13 +29,46 @@ func TestNewServer(t *testing.T) {
2729
if err != nil {
2830
t.Fatal(err)
2931
}
30-
defer srv.Close()
3132
srv.Start()
32-
conn, err := grpc.Dial(srv.Addr, grpc.WithInsecure())
33+
34+
conn, err := grpc.NewClient(srv.Addr, grpc.WithTransportCredentials(insecure.NewCredentials()))
3335
if err != nil {
3436
t.Fatal(err)
3537
}
36-
defer conn.Close()
38+
39+
t.Cleanup(func() {
40+
conn.Close()
41+
srv.Close()
42+
})
43+
}
44+
45+
func TestNewServerWithAddress(t *testing.T) {
46+
addresses := []string{
47+
":8181",
48+
"0.0.0.0:8181",
49+
"127.0.0.1:8181",
50+
"localhost:8181",
51+
}
52+
53+
for _, a := range addresses {
54+
t.Run(fmt.Sprintf("GIVEN host %s THEN succeed to init new server", a), func(t *testing.T) {
55+
srv, err := NewServerWithAddress(a)
56+
if err != nil {
57+
t.Fatal(err)
58+
}
59+
srv.Start()
60+
61+
conn, err := grpc.NewClient(srv.Addr, grpc.WithTransportCredentials(insecure.NewCredentials()))
62+
if err != nil {
63+
t.Fatal(err)
64+
}
65+
66+
t.Cleanup(func() {
67+
conn.Close()
68+
srv.Close()
69+
})
70+
})
71+
}
3772
}
3873

3974
func TestPageBounds(t *testing.T) {

0 commit comments

Comments
 (0)