@@ -41,42 +41,49 @@ import (
41
41
)
42
42
43
43
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 )
56
45
srv := NewServerWithPort (port )
46
+
47
+ conn , err := grpc .NewClient (srv .Addr , grpc .WithTransportCredentials (insecure .NewCredentials ()))
57
48
if err != nil {
58
49
t .Fatal (err )
59
50
}
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
+ })
66
56
}
67
57
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
+ })
76
81
}
77
- port := l .Addr ().(* net.TCPAddr ).Port
78
- l .Close ()
79
82
83
+ }
84
+
85
+ func TestNewServerWithCallback (t * testing.T ) {
86
+ port := getFreePort (t )
80
87
additionalFake := struct {
81
88
iampb.UnimplementedIAMPolicyServer
82
89
}{}
@@ -90,20 +97,35 @@ func TestNewServerWithCallback(t *testing.T) {
90
97
91
98
// Pass a non 0 port to demonstrate we can pass a hardcoded port for the server to listen on
92
99
srv := NewServerWithCallback (port , callback )
93
- if err != nil {
94
- t .Fatal (err )
95
- }
96
- defer srv .Close ()
97
100
98
101
conn , err := grpc .NewClient (srv .Addr , grpc .WithTransportCredentials (insecure .NewCredentials ()))
99
102
if err != nil {
100
103
t .Fatal (err )
101
104
}
102
- defer conn .Close ()
103
105
104
106
if ! verifyCallback {
105
107
t .Fatal ("callback was not invoked" )
106
108
}
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
107
129
}
108
130
109
131
func TestTopics (t * testing.T ) {
0 commit comments