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
19package service
20
21import (
22	"testing"
23
24	grpc "google.golang.org/grpc"
25)
26
27const (
28	// The address is irrelevant in this test.
29	testAddress = "some_address"
30)
31
32func 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	// Ensure that hsConn is nil at first.
44	hsConn = nil
45
46	// First call to Dial, it should create set hsConn.
47	conn1, err := Dial(testAddress)
48	if err != nil {
49		t.Fatalf("first call to Dial failed: %v", err)
50	}
51	if conn1 == nil {
52		t.Fatal("first call to Dial(_)=(nil, _), want not nil")
53	}
54	if got, want := hsConn, conn1; got != want {
55		t.Fatalf("hsConn=%v, want %v", got, want)
56	}
57
58	// Second call to Dial should return conn1 above.
59	conn2, err := Dial(testAddress)
60	if err != nil {
61		t.Fatalf("second call to Dial(_) failed: %v", err)
62	}
63	if got, want := conn2, conn1; got != want {
64		t.Fatalf("second call to Dial(_)=(%v, _), want (%v,. _)", got, want)
65	}
66	if got, want := hsConn, conn1; got != want {
67		t.Fatalf("hsConn=%v, want %v", got, want)
68	}
69}
70