Debian Codegraph @

internal/handshaker/service/service_test.go

raw symbols SCIP
1 /*
2 *
3 * Copyright 2021 Google LLC
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 * https://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 "context"
23 "crypto/tls"
24 "testing"
25
26 "google.golang.org/grpc/credentials"
27
28 grpc "google.golang.org/grpc"
29 )
30
31 const (
32 testAddress1 = "test_address_1"
33 testAddress2 = "test_address_2"
34 testAddress3 = "test_address_3"
35 )
36
37 func TestDial(t *testing.T) {
38 defer func() func() {
39 temp := hsDialer
40 hsDialer = func(ctx context.Context, target string, opts ...grpc.DialOption) (*grpc.ClientConn, error) {
41 return &grpc.ClientConn{}, nil
42 }
43 return func() {
44 hsDialer = temp
45 }
46 }()
47
48 ctx := context.Background()
49
50 // First call to Dial, it should create a connection to the server running
51 // at the given address.
52 conn1, err := Dial(ctx, testAddress1, nil)
53 if err != nil {
54 t.Fatalf("first call to Dial(%v) failed: %v", testAddress1, err)
55 }
56 if conn1 == nil {
57 t.Fatalf("first call to Dial(%v)=(nil, _), want not nil", testAddress1)
58 }
59 if got, want := hsConnMap[testAddress1], conn1; got != want {
60 t.Fatalf("hsConnMap[%v] = %v, want %v", testAddress1, got, want)
61 }
62
63 // Second call to Dial should return conn1 above.
64 conn2, err := Dial(ctx, testAddress1, nil)
65 if err != nil {
66 t.Fatalf("second call to Dial(%v) failed: %v", testAddress1, err)
67 }
68 if got, want := conn2, conn1; got != want {
69 t.Fatalf("second call to Dial(%v)=(%v, _), want (%v, _)", testAddress1, got, want)
70 }
71 if got, want := hsConnMap[testAddress1], conn1; got != want {
72 t.Fatalf("hsConnMap[%v] = %v, want %v", testAddress1, got, want)
73 }
74
75 // Third call to Dial using a different address should create a new
76 // connection.
77 conn3, err := Dial(ctx, testAddress2, nil)
78 if err != nil {
79 t.Fatalf("third call to Dial(%v) failed: %v", testAddress2, err)
80 }
81 if conn3 == nil {
82 t.Fatalf("third call to Dial(%v)=(nil, _), want not nil", testAddress2)
83 }
84 if got, want := hsConnMap[testAddress2], conn3; got != want {
85 t.Fatalf("hsConnMap[%v] = %v, want %v", testAddress2, got, want)
86 }
87 if got, want := conn2 == conn3, false; got != want {
88 t.Fatalf("(conn2 == conn3) = %v, want %v", got, want)
89 }
90
91 // Connect to an address with transportCredentials.
92 conn4, err := Dial(ctx, testAddress3, credentials.NewTLS(&tls.Config{}))
93 if err != nil {
94 t.Fatalf("first call to Dial(%v) failed: %v", testAddress3, err)
95 }
96 if conn4 == nil {
97 t.Fatalf("first call to Dial(%v)=(nil, _), want not nil", testAddress3)
98 }
99 if got, want := hsConnMap[testAddress3], conn4; got != want {
100 t.Fatalf("hsConnMap[%v] = %v, want %v", testAddress3, got, want)
101 }
102 }