Skip to content

Commit

Permalink
fix: Add grpc tester receiver to TestDial
Browse files Browse the repository at this point in the history
  • Loading branch information
aranjans committed Jun 12, 2024
1 parent d8a2ca2 commit 51a3b23
Showing 1 changed file with 24 additions and 10 deletions.
34 changes: 24 additions & 10 deletions credentials/alts/internal/handshaker/service/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,33 @@
package service

import (
"google.golang.org/grpc"
"google.golang.org/grpc/internal/grpctest"
"testing"
)

type s struct {
grpctest.Tester
}

func Test(t *testing.T) {
grpctest.RunSubTests(t, s{})
}

const (
testAddress1 = "some_address_1"
testAddress2 = "some_address_2"
)

// TestDial verifies the behavior of the Dial function in managing gRPC connections
// and checks the below scenarios:
// - First call to Dial with an address creates a new connection and stores it.
// - Second call to Dial with the same address returns the existing connection
// and expects the returned connection matching with ƒirst connection.
// - Third call to Dial with a different address creates a separate new connection
// and expects a different connection from connnection1 and connection2.
func TestDial(t *testing.T) {
// TestDial verifies the behaviour of alts handshake when there are multiple Dials.
func (s) TestDial(t *testing.T) {
temp := hsDialer
hsDialer = func(target string, opts ...grpc.DialOption) (*grpc.ClientConn, error) {
return &grpc.ClientConn{}, nil
}
defer func() {
hsDialer = temp
}()
// First call to Dial, it should create a connection to the server running
// at the given address.
conn1, err := Dial(testAddress1)
Expand All @@ -48,7 +59,9 @@ func TestDial(t *testing.T) {
t.Fatalf("hsConnMap[%v]=%v, want %v", testAddress1, got, want)
}

// Second call to Dial should return conn1 above.
// Second call to Dial with the same address returns the existing connection
// and expects the returned connection matching with ƒirst connection, and
// should return conn1 above.
conn2, err := Dial(testAddress1)
if err != nil {
t.Fatalf("second call to Dial(%v) failed: %v", testAddress1, err)
Expand All @@ -60,7 +73,8 @@ func TestDial(t *testing.T) {
t.Fatalf("hsConnMap[%v]=%v, want %v", testAddress1, got, want)
}

// Third call to Dial using a different address should create a new
// Third call to Dial with a different address creates a separate new connection
// and expects a different connection from conn1 and conn2, and should create a new
// connection.
conn3, err := Dial(testAddress2)
if err != nil {
Expand Down

0 comments on commit 51a3b23

Please sign in to comment.