Skip to content

Commit e9b3d26

Browse files
committed
Changed from grpc.Dial to grpc.NewClient
1 parent 46c18b7 commit e9b3d26

2 files changed

Lines changed: 16 additions & 6 deletions

File tree

grpc_client.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,6 @@ func dialGRPCConn(tls *tls.Config, dialer func(context.Context, string) (net.Con
2424
// We use a custom dialer so that we can connect over unix domain sockets.
2525
opts = append(opts, grpc.WithContextDialer(dialer))
2626

27-
// Fail right away
28-
opts = append(opts, grpc.FailOnNonTempDialError(true))
29-
3027
// If we have no TLS configuration set, we need to explicitly tell grpc
3128
// that we're connecting with an insecure connection.
3229
if tls == nil {
@@ -45,7 +42,7 @@ func dialGRPCConn(tls *tls.Config, dialer func(context.Context, string) (net.Con
4542

4643
// Connect. Note the first parameter is unused because we use a custom
4744
// dialer that has the state to see the address.
48-
conn, err := grpc.Dial("unused", opts...)
45+
conn, err := grpc.NewClient("passthrough:///unused", opts...)
4946
if err != nil {
5047
return nil, err
5148
}

testing.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@ import (
1010
"net"
1111
"net/rpc"
1212
"testing"
13+
"time"
1314

1415
hclog "github.com/hashicorp/go-hclog"
1516
"github.com/hashicorp/go-plugin/internal/grpcmux"
1617
"google.golang.org/grpc"
18+
"google.golang.org/grpc/connectivity"
1719
"google.golang.org/grpc/credentials/insecure"
1820
)
1921

@@ -120,15 +122,26 @@ func TestGRPCConn(t testing.TB, register func(*grpc.Server)) (*grpc.ClientConn,
120122
go func() { _ = server.Serve(l) }()
121123

122124
// Connect to the server
123-
conn, err := grpc.Dial(
125+
conn, err := grpc.NewClient(
124126
l.Addr().String(),
125-
grpc.WithBlock(),
126127
grpc.WithTransportCredentials(insecure.NewCredentials()),
127128
)
128129
if err != nil {
129130
t.Fatalf("err: %s", err)
130131
}
131132

133+
// Wait for the connection to be established before closing the listener
134+
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
135+
defer cancel()
136+
conn.Connect()
137+
138+
// Wait until connection reaches Ready state
139+
for state := conn.GetState(); state != connectivity.Ready; state = conn.GetState() {
140+
if !conn.WaitForStateChange(ctx, state) {
141+
t.Fatalf("failed to establish connection, final state: %v", conn.GetState())
142+
}
143+
}
144+
132145
// Connection successful, close the listener
133146
_ = l.Close()
134147

0 commit comments

Comments
 (0)