Skip to content

Commit

Permalink
replace grpc.Dial with grpc.NewClient
Browse files Browse the repository at this point in the history
  • Loading branch information
janardhankrishna-sai committed Dec 11, 2024
1 parent 05e8553 commit cb435b2
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 34 deletions.
12 changes: 6 additions & 6 deletions balancer/rls/picker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,9 +267,9 @@ func (s) Test_RLSDefaultTargetPicksMetric(t *testing.T) {
r := startManualResolverWithConfig(t, rlsConfig)

tmr := stats.NewTestMetricsRecorder()
cc, err := grpc.Dial(r.Scheme()+":///", grpc.WithResolvers(r), grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithStatsHandler(tmr))
cc, err := grpc.NewClient(r.Scheme()+":///", grpc.WithResolvers(r), grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithStatsHandler(tmr))
if err != nil {
t.Fatalf("grpc.Dial() failed: %v", err)
t.Fatalf("grpc.NewClient() failed: %v", err)
}
defer cc.Close()

Expand Down Expand Up @@ -314,9 +314,9 @@ func (s) Test_RLSTargetPicksMetric(t *testing.T) {

tmr := stats.NewTestMetricsRecorder()
// Dial the backend.
cc, err := grpc.Dial(r.Scheme()+":///", grpc.WithResolvers(r), grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithStatsHandler(tmr))
cc, err := grpc.NewClient(r.Scheme()+":///", grpc.WithResolvers(r), grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithStatsHandler(tmr))
if err != nil {
t.Fatalf("grpc.Dial() failed: %v", err)
t.Fatalf("grpc.NewClient() failed: %v", err)
}
defer cc.Close()

Expand Down Expand Up @@ -352,9 +352,9 @@ func (s) Test_RLSFailedPicksMetric(t *testing.T) {

tmr := stats.NewTestMetricsRecorder()
// Dial the backend.
cc, err := grpc.Dial(r.Scheme()+":///", grpc.WithResolvers(r), grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithStatsHandler(tmr))
cc, err := grpc.NewClient(r.Scheme()+":///", grpc.WithResolvers(r), grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithStatsHandler(tmr))
if err != nil {
t.Fatalf("grpc.Dial() failed: %v", err)
t.Fatalf("grpc.NewClient() failed: %v", err)
}
defer cc.Close()

Expand Down
10 changes: 6 additions & 4 deletions resolver_balancer_ext_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,12 @@ func (s) TestResolverBalancerInteraction(t *testing.T) {
rb.ResolveNowCallback = func(resolver.ResolveNowOptions) { close(rnCh) }
resolver.Register(rb)

cc, err := grpc.Dial(name+":///", grpc.WithTransportCredentials(insecure.NewCredentials()))
cc, err := grpc.NewClient(name+":///", grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithDefaultServiceConfig(`{"loadBalancingConfig": [{"`+name+`":{}}]}`))
if err != nil {
t.Fatalf("grpc.Dial error: %v", err)
t.Fatalf("grpc.NewClient error: %v", err)
}
defer cc.Close()
cc.Connect()
select {
case <-rnCh:
case <-time.After(defaultTestTimeout):
Expand Down Expand Up @@ -109,11 +110,12 @@ func (s) TestResolverBuildFailure(t *testing.T) {
resolver.Register(&resolverBuilderWithErr{errCh: resErrCh, scheme: name})

resErrCh <- nil
cc, err := grpc.Dial(name+":///", grpc.WithTransportCredentials(insecure.NewCredentials()))
cc, err := grpc.NewClient(name+":///", grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
t.Fatalf("grpc.Dial error: %v", err)
t.Fatalf("grpc.NewClient error: %v", err)
}
defer cc.Close()
cc.Connect()
enterIdle(cc)
const errStr = "test error from resolver builder"
t.Log("pushing res err")
Expand Down
80 changes: 56 additions & 24 deletions test/creds_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,9 @@ import (

"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/connectivity"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/internal/testutils"
"google.golang.org/grpc/internal/stubserver"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/resolver"
"google.golang.org/grpc/resolver/manual"
Expand Down Expand Up @@ -236,9 +235,9 @@ func (s) TestFailFastRPCErrorOnBadCertificates(t *testing.T) {
opts := []grpc.DialOption{grpc.WithTransportCredentials(clientAlwaysFailCred{})}
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
defer cancel()
cc, err := grpc.DialContext(ctx, te.srvAddr, opts...)
cc, err := grpc.NewClient(te.srvAddr, opts...)
if err != nil {
t.Fatalf("Dial(_) = %v, want %v", err, nil)
t.Fatalf("NewClient(_) = %v, want %v", err, nil)
}
defer cc.Close()

Expand All @@ -262,16 +261,14 @@ func (s) TestWaitForReadyRPCErrorOnBadCertificates(t *testing.T) {
defer te.tearDown()

opts := []grpc.DialOption{grpc.WithTransportCredentials(clientAlwaysFailCred{})}
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
defer cancel()
cc, err := grpc.DialContext(ctx, te.srvAddr, opts...)
cc, err := grpc.NewClient(te.srvAddr, opts...)
if err != nil {
t.Fatalf("Dial(_) = %v, want %v", err, nil)
}
defer cc.Close()

tc := testgrpc.NewTestServiceClient(cc)
ctx, cancel = context.WithTimeout(context.Background(), defaultTestShortTimeout)
ctx, cancel := context.WithTimeout(context.Background(), defaultTestShortTimeout)
defer cancel()
if _, err = tc.EmptyCall(ctx, &testpb.Empty{}, grpc.WaitForReady(true)); !strings.Contains(err.Error(), clientAlwaysFailCredErrorMsg) {
t.Fatalf("TestService/EmptyCall(_, _) = _, %v, want err.Error() contains %q", err, clientAlwaysFailCredErrorMsg)
Expand Down Expand Up @@ -430,23 +427,40 @@ func (s) TestCredsHandshakeAuthority(t *testing.T) {
if err != nil {
t.Fatal(err)
}
defer lis.Close()

cred := &authorityCheckCreds{}
s := grpc.NewServer()
go s.Serve(lis)
defer s.Stop()
stub := &stubserver.StubServer{
Listener: lis,
EmptyCallF: func(context.Context, *testpb.Empty) (*testpb.Empty, error) {
return &testpb.Empty{}, nil
},
}
stub.S = grpc.NewServer()
stubserver.StartTestService(t, stub)

r := manual.NewBuilderWithScheme("whatever")

cc, err := grpc.Dial(r.Scheme()+":///"+testAuthority, grpc.WithTransportCredentials(cred), grpc.WithResolvers(r))
cc, err := grpc.NewClient(r.Scheme()+":///"+testAuthority, grpc.WithTransportCredentials(cred), grpc.WithResolvers(r))
if err != nil {
t.Fatalf("grpc.Dial(%q) = %v", lis.Addr().String(), err)
t.Fatalf("grpc.NewClient(%q) = %v", lis.Addr().String(), err)
}
defer cc.Close()
r.UpdateState(resolver.State{Addresses: []resolver.Address{{Addr: lis.Addr().String()}}})

// Start a goroutine to update the resolver state.
go func() {
<-time.After(100 * time.Millisecond)
r.UpdateState(resolver.State{Addresses: []resolver.Address{{Addr: lis.Addr().String()}}})
}()

ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
defer cancel()
testutils.AwaitState(ctx, t, cc, connectivity.Ready)

// Perform an RPC to trigger the connection process and use the resolver.
client := testpb.NewTestServiceClient(cc)
if _, err := client.EmptyCall(ctx, &testpb.Empty{}); err != nil {
t.Fatalf("Test RPC failed: %v", err)
}

if cred.got != testAuthority {
t.Fatalf("client creds got authority: %q, want: %q", cred.got, testAuthority)
Expand All @@ -463,22 +477,39 @@ func (s) TestCredsHandshakeServerNameAuthority(t *testing.T) {
t.Fatal(err)
}
cred := &authorityCheckCreds{}
s := grpc.NewServer()
go s.Serve(lis)
defer s.Stop()
defer lis.Close()

stub := &stubserver.StubServer{
Listener: lis,
EmptyCallF: func(context.Context, *testpb.Empty) (*testpb.Empty, error) {
return &testpb.Empty{}, nil
},
}
stub.S = grpc.NewServer()
stubserver.StartTestService(t, stub)

r := manual.NewBuilderWithScheme("whatever")

cc, err := grpc.Dial(r.Scheme()+":///"+testAuthority, grpc.WithTransportCredentials(cred), grpc.WithResolvers(r))
cc, err := grpc.NewClient(r.Scheme()+":///"+testAuthority, grpc.WithTransportCredentials(cred), grpc.WithResolvers(r))
if err != nil {
t.Fatalf("grpc.Dial(%q) = %v", lis.Addr().String(), err)
t.Fatalf("grpc.NewClient(%q) = %v", lis.Addr().String(), err)
}
defer cc.Close()
r.UpdateState(resolver.State{Addresses: []resolver.Address{{Addr: lis.Addr().String(), ServerName: testServerName}}})

// Start a goroutine to update the resolver state.
go func() {
<-time.After(100 * time.Millisecond)
r.UpdateState(resolver.State{Addresses: []resolver.Address{{Addr: lis.Addr().String(), ServerName: testServerName}}})
}()

ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
defer cancel()
testutils.AwaitState(ctx, t, cc, connectivity.Ready)

// Perform an RPC to trigger the connection process and use the resolver.
client := testpb.NewTestServiceClient(cc)
if _, err := client.EmptyCall(ctx, &testpb.Empty{}); err != nil {
t.Fatalf("Test RPC failed: %v", err)
}

if cred.got != testServerName {
t.Fatalf("client creds got authority: %q, want: %q", cred.got, testAuthority)
Expand Down Expand Up @@ -524,10 +555,11 @@ func (s) TestServerCredsDispatch(t *testing.T) {
go s.Serve(lis)
defer s.Stop()

cc, err := grpc.Dial(lis.Addr().String(), grpc.WithTransportCredentials(cred))
cc, err := grpc.NewClient(lis.Addr().String(), grpc.WithTransportCredentials(cred))
if err != nil {
t.Fatalf("grpc.Dial(%q) = %v", lis.Addr().String(), err)
t.Fatalf("grpc.NewClient(%q) = %v", lis.Addr().String(), err)
}
cc.Connect()
defer cc.Close()

rawConn := cred.getRawConn()
Expand Down

0 comments on commit cb435b2

Please sign in to comment.