@@ -22,6 +22,7 @@ import (
22
22
"encoding/json"
23
23
"net"
24
24
"testing"
25
+ "time"
25
26
26
27
"google.golang.org/grpc"
27
28
"google.golang.org/grpc/credentials"
@@ -108,10 +109,17 @@ const testDialerCredsBuilderName = "test_dialer_creds"
108
109
// testDialerCredsBuilder implements the `Credentials` interface defined in
109
110
// package `xds/bootstrap` and encapsulates an insecure credential with a
110
111
// custom Dialer that specifies how to dial the xDS server.
111
- type testDialerCredsBuilder struct {}
112
+ type testDialerCredsBuilder struct {
113
+ // Closed with the custom Dialer is invoked.
114
+ // Needs to be passed in by the test.
115
+ dialCalled chan struct {}
116
+ }
112
117
113
118
func (t * testDialerCredsBuilder ) Build (json.RawMessage ) (credentials.Bundle , func (), error ) {
114
- return & testDialerCredsBundle {insecure .NewBundle ()}, func () {}, nil
119
+ return & testDialerCredsBundle {
120
+ Bundle : insecure .NewBundle (),
121
+ dialCalled : t .dialCalled ,
122
+ }, func () {}, nil
115
123
}
116
124
117
125
func (t * testDialerCredsBuilder ) Name () string {
@@ -123,10 +131,12 @@ func (t *testDialerCredsBuilder) Name() string {
123
131
// that specifies how to dial the xDS server.
124
132
type testDialerCredsBundle struct {
125
133
credentials.Bundle
134
+ dialCalled chan struct {}
126
135
}
127
136
128
- func (t * testDialerCredsBundle ) Dialer (context.Context , string ) (net.Conn , error ) {
129
- return nil , nil
137
+ func (t * testDialerCredsBundle ) Dialer (_ context.Context , address string ) (net.Conn , error ) {
138
+ close (t .dialCalled )
139
+ return net .Dial ("tcp" , address )
130
140
}
131
141
132
142
func (s ) TestNewWithDialerFromCredentialsBundle (t * testing.T ) {
@@ -140,17 +150,16 @@ func (s) TestNewWithDialerFromCredentialsBundle(t *testing.T) {
140
150
internal .GRPCNewClient = customGRPCNewClient
141
151
defer func () { internal .GRPCNewClient = oldGRPCNewClient }()
142
152
143
- bootstrap .RegisterCredentials (& testDialerCredsBuilder {})
153
+ dialCalled := make (chan struct {})
154
+ bootstrap .RegisterCredentials (& testDialerCredsBuilder {dialCalled : dialCalled })
144
155
serverCfg , err := internalbootstrap .ServerConfigForTesting (internalbootstrap.ServerConfigTestingOptions {
145
156
URI : "trafficdirector.googleapis.com:443" ,
146
157
ChannelCreds : []internalbootstrap.ChannelCreds {{Type : testDialerCredsBuilderName }},
147
158
})
148
159
if err != nil {
149
160
t .Fatalf ("Failed to create server config for testing: %v" , err )
150
161
}
151
- if serverCfg .DialerOption () == nil {
152
- t .Fatalf ("Dialer for xDS transport in server config for testing is nil, want non-nil" )
153
- }
162
+
154
163
// Create a new transport.
155
164
opts := transport.Options {
156
165
ServerCfg : serverCfg ,
@@ -171,6 +180,11 @@ func (s) TestNewWithDialerFromCredentialsBundle(t *testing.T) {
171
180
if err != nil {
172
181
t .Fatalf ("transport.New(%v) failed: %v" , opts , err )
173
182
}
183
+ select {
184
+ case <- dialCalled :
185
+ case <- time .After (defaultTestTimeout ):
186
+ t .Fatal ("Timeout when waiting for Dialer() to be invoked" )
187
+ }
174
188
// Verify there are three dial options passed to the custom grpc.NewClient.
175
189
// The first is opts.ServerCfg.CredsDialOption(), the second is
176
190
// grpc.WithKeepaliveParams(), and the third is opts.ServerCfg.DialerOption()
0 commit comments