google.golang.org/grpc@v1.62.1/credentials/alts/internal/handshaker/service/service_test.go (about) 1 /* 2 * 3 * Copyright 2018 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 19 package service 20 21 import ( 22 "testing" 23 24 grpc "google.golang.org/grpc" 25 ) 26 27 const ( 28 testAddress1 = "some_address_1" 29 testAddress2 = "some_address_2" 30 ) 31 32 func TestDial(t *testing.T) { 33 defer func() func() { 34 temp := hsDialer 35 hsDialer = func(target string, opts ...grpc.DialOption) (*grpc.ClientConn, error) { 36 return &grpc.ClientConn{}, nil 37 } 38 return func() { 39 hsDialer = temp 40 } 41 }() 42 43 // First call to Dial, it should create a connection to the server running 44 // at the given address. 45 conn1, err := Dial(testAddress1) 46 if err != nil { 47 t.Fatalf("first call to Dial(%v) failed: %v", testAddress1, err) 48 } 49 if conn1 == nil { 50 t.Fatalf("first call to Dial(%v)=(nil, _), want not nil", testAddress1) 51 } 52 if got, want := hsConnMap[testAddress1], conn1; got != want { 53 t.Fatalf("hsConnMap[%v]=%v, want %v", testAddress1, got, want) 54 } 55 56 // Second call to Dial should return conn1 above. 57 conn2, err := Dial(testAddress1) 58 if err != nil { 59 t.Fatalf("second call to Dial(%v) failed: %v", testAddress1, err) 60 } 61 if got, want := conn2, conn1; got != want { 62 t.Fatalf("second call to Dial(%v)=(%v, _), want (%v,. _)", testAddress1, got, want) 63 } 64 if got, want := hsConnMap[testAddress1], conn1; got != want { 65 t.Fatalf("hsConnMap[%v]=%v, want %v", testAddress1, got, want) 66 } 67 68 // Third call to Dial using a different address should create a new 69 // connection. 70 conn3, err := Dial(testAddress2) 71 if err != nil { 72 t.Fatalf("third call to Dial(%v) failed: %v", testAddress2, err) 73 } 74 if conn3 == nil { 75 t.Fatalf("third call to Dial(%v)=(nil, _), want not nil", testAddress2) 76 } 77 if got, want := hsConnMap[testAddress2], conn3; got != want { 78 t.Fatalf("hsConnMap[%v]=%v, want %v", testAddress2, got, want) 79 } 80 if got, want := conn2 == conn3, false; got != want { 81 t.Fatalf("(conn2==conn3)=%v, want %v", got, want) 82 } 83 }