@@ -41,42 +41,49 @@ import (
4141)
4242
4343func 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
109131func TestTopics (t * testing.T ) {
0 commit comments