google.golang.org/grpc@v1.72.2/xds/internal/xdsclient/transport/grpctransport/grpctransport_ext_test.go (about) 1 /* 2 * 3 * Copyright 2024 gRPC authors. 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package grpctransport_test 19 20 import ( 21 "testing" 22 23 "google.golang.org/grpc" 24 "google.golang.org/grpc/internal/grpctest" 25 internalbootstrap "google.golang.org/grpc/internal/xds/bootstrap" 26 "google.golang.org/grpc/xds/internal/xdsclient/internal" 27 "google.golang.org/grpc/xds/internal/xdsclient/transport" 28 "google.golang.org/grpc/xds/internal/xdsclient/transport/grpctransport" 29 ) 30 31 type s struct { 32 grpctest.Tester 33 } 34 35 func Test(t *testing.T) { 36 grpctest.RunSubTests(t, s{}) 37 } 38 39 // Tests that the grpctransport.Builder creates a new grpc.ClientConn every time 40 // Build() is called. 41 func (s) TestBuild_CustomDialer(t *testing.T) { 42 // Override the dialer with a custom one. 43 customDialerCalled := false 44 origDialer := internal.GRPCNewClient 45 internal.GRPCNewClient = func(target string, opts ...grpc.DialOption) (*grpc.ClientConn, error) { 46 customDialerCalled = true 47 return grpc.NewClient(target, opts...) 48 } 49 defer func() { internal.GRPCNewClient = origDialer }() 50 51 serverCfg, err := internalbootstrap.ServerConfigForTesting(internalbootstrap.ServerConfigTestingOptions{URI: "server-address"}) 52 if err != nil { 53 t.Fatalf("Failed to create server config for testing: %v", err) 54 } 55 56 // Create a new transport and ensure that the custom dialer was called. 57 opts := transport.BuildOptions{ServerConfig: serverCfg} 58 builder := &grpctransport.Builder{} 59 tr, err := builder.Build(opts) 60 if err != nil { 61 t.Fatalf("Builder.Build(%+v) failed: %v", opts, err) 62 } 63 defer tr.Close() 64 65 if !customDialerCalled { 66 t.Fatalf("Builder.Build(%+v): custom dialer called = false, want true", opts) 67 } 68 customDialerCalled = false 69 70 // Create another transport and ensure that the custom dialer was called. 71 tr, err = builder.Build(opts) 72 if err != nil { 73 t.Fatalf("Builder.Build(%+v) failed: %v", opts, err) 74 } 75 defer tr.Close() 76 77 if !customDialerCalled { 78 t.Fatalf("Builder.Build(%+v): custom dialer called = false, want true", opts) 79 } 80 } 81 82 // Tests that the grpctransport.Builder fails to build a transport when the 83 // provided BuildOptions do not contain a ServerConfig. 84 func (s) TestBuild_EmptyServerConfig(t *testing.T) { 85 builder := &grpctransport.Builder{} 86 opts := transport.BuildOptions{} 87 if tr, err := builder.Build(opts); err == nil { 88 tr.Close() 89 t.Fatalf("Builder.Build(%+v) succeeded when expected to fail", opts) 90 } 91 }