google.golang.org/grpc@v1.62.1/xds/internal/xdsclient/transport/transport_test.go (about)

     1  /*
     2   *
     3   * Copyright 2022 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 transport
    19  
    20  import (
    21  	"testing"
    22  
    23  	v3corepb "github.com/envoyproxy/go-control-plane/envoy/config/core/v3"
    24  	"google.golang.org/grpc"
    25  	"google.golang.org/grpc/internal/grpctest"
    26  	xdstestutils "google.golang.org/grpc/xds/internal/testutils"
    27  )
    28  
    29  type s struct {
    30  	grpctest.Tester
    31  }
    32  
    33  func Test(t *testing.T) {
    34  	grpctest.RunSubTests(t, s{})
    35  }
    36  
    37  func (s) TestNewWithGRPCDial(t *testing.T) {
    38  	// Override the dialer with a custom one.
    39  	customDialerCalled := false
    40  	customDialer := func(target string, opts ...grpc.DialOption) (*grpc.ClientConn, error) {
    41  		customDialerCalled = true
    42  		return grpc.Dial(target, opts...)
    43  	}
    44  	oldDial := grpcDial
    45  	grpcDial = customDialer
    46  	defer func() { grpcDial = oldDial }()
    47  
    48  	// Create a new transport and ensure that the custom dialer was called.
    49  	opts := Options{
    50  		ServerCfg:      *xdstestutils.ServerConfigForAddress(t, "server-address"),
    51  		NodeProto:      &v3corepb.Node{},
    52  		OnRecvHandler:  func(ResourceUpdate) error { return nil },
    53  		OnErrorHandler: func(error) {},
    54  		OnSendHandler:  func(*ResourceSendInfo) {},
    55  	}
    56  	c, err := New(opts)
    57  	if err != nil {
    58  		t.Fatalf("New(%v) failed: %v", opts, err)
    59  	}
    60  	defer c.Close()
    61  
    62  	if !customDialerCalled {
    63  		t.Fatalf("New(%+v) custom dialer called = false, want true", opts)
    64  	}
    65  	customDialerCalled = false
    66  
    67  	// Reset the dialer, create a new transport and ensure that our custom
    68  	// dialer is no longer called.
    69  	grpcDial = grpc.Dial
    70  	c, err = New(opts)
    71  	defer func() {
    72  		if c != nil {
    73  			c.Close()
    74  		}
    75  	}()
    76  	if err != nil {
    77  		t.Fatalf("New(%v) failed: %v", opts, err)
    78  	}
    79  
    80  	if customDialerCalled {
    81  		t.Fatalf("New(%+v) custom dialer called = true, want false", opts)
    82  	}
    83  }