@@ -22,6 +22,7 @@ import (
2222 "encoding/json"
2323 "net"
2424 "testing"
25+ "time"
2526
2627 "google.golang.org/grpc"
2728 "google.golang.org/grpc/credentials"
@@ -108,10 +109,17 @@ const testDialerCredsBuilderName = "test_dialer_creds"
108109// testDialerCredsBuilder implements the `Credentials` interface defined in
109110// package `xds/bootstrap` and encapsulates an insecure credential with a
110111// 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+ }
112117
113118func (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
115123}
116124
117125func (t * testDialerCredsBuilder ) Name () string {
@@ -123,10 +131,12 @@ func (t *testDialerCredsBuilder) Name() string {
123131// that specifies how to dial the xDS server.
124132type testDialerCredsBundle struct {
125133 credentials.Bundle
134+ dialCalled chan struct {}
126135}
127136
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 )
130140}
131141
132142func (s ) TestNewWithDialerFromCredentialsBundle (t * testing.T ) {
@@ -140,17 +150,16 @@ func (s) TestNewWithDialerFromCredentialsBundle(t *testing.T) {
140150 internal .GRPCNewClient = customGRPCNewClient
141151 defer func () { internal .GRPCNewClient = oldGRPCNewClient }()
142152
143- bootstrap .RegisterCredentials (& testDialerCredsBuilder {})
153+ dialCalled := make (chan struct {})
154+ bootstrap .RegisterCredentials (& testDialerCredsBuilder {dialCalled : dialCalled })
144155 serverCfg , err := internalbootstrap .ServerConfigForTesting (internalbootstrap.ServerConfigTestingOptions {
145156 URI : "trafficdirector.googleapis.com:443" ,
146157 ChannelCreds : []internalbootstrap.ChannelCreds {{Type : testDialerCredsBuilderName }},
147158 })
148159 if err != nil {
149160 t .Fatalf ("Failed to create server config for testing: %v" , err )
150161 }
151- if serverCfg .DialerOption () == nil {
152- t .Fatalf ("Dialer for xDS transport in server config for testing is nil, want non-nil" )
153- }
162+
154163 // Create a new transport.
155164 opts := transport.Options {
156165 ServerCfg : serverCfg ,
@@ -171,6 +180,11 @@ func (s) TestNewWithDialerFromCredentialsBundle(t *testing.T) {
171180 if err != nil {
172181 t .Fatalf ("transport.New(%v) failed: %v" , opts , err )
173182 }
183+ select {
184+ case <- dialCalled :
185+ case <- time .After (defaultTestTimeout ):
186+ t .Fatal ("Timeout when waiting for Dialer() to be invoked" )
187+ }
174188 // Verify there are three dial options passed to the custom grpc.NewClient.
175189 // The first is opts.ServerCfg.CredsDialOption(), the second is
176190 // grpc.WithKeepaliveParams(), and the third is opts.ServerCfg.DialerOption()
0 commit comments