Skip to content

Commit 56a14ba

Browse files
cleanup: replace dial with newclient (#7920)
1 parent b3bdacb commit 56a14ba

File tree

5 files changed

+48
-42
lines changed

5 files changed

+48
-42
lines changed

balancer/grpclb/grpclb_test.go

+21-18
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,7 @@ func (s) TestGRPCLB_Basic(t *testing.T) {
460460
}
461461
cc, err := grpc.NewClient(r.Scheme()+":///"+beServerName, dopts...)
462462
if err != nil {
463-
t.Fatalf("Failed to dial to the backend %v", err)
463+
t.Fatalf("Failed to create a client for the backend %v", err)
464464
}
465465
defer cc.Close()
466466

@@ -517,7 +517,7 @@ func (s) TestGRPCLB_Weighted(t *testing.T) {
517517
}
518518
cc, err := grpc.NewClient(r.Scheme()+":///"+beServerName, dopts...)
519519
if err != nil {
520-
t.Fatalf("Failed to dial to the backend %v", err)
520+
t.Fatalf("Failed to create a client for the backend %v", err)
521521
}
522522
defer cc.Close()
523523

@@ -597,7 +597,7 @@ func (s) TestGRPCLB_DropRequest(t *testing.T) {
597597
}
598598
cc, err := grpc.NewClient(r.Scheme()+":///"+beServerName, dopts...)
599599
if err != nil {
600-
t.Fatalf("Failed to dial to the backend %v", err)
600+
t.Fatalf("Failed to create a client for the backend %v", err)
601601
}
602602
defer cc.Close()
603603
testC := testgrpc.NewTestServiceClient(cc)
@@ -769,7 +769,7 @@ func (s) TestGRPCLB_BalancerDisconnects(t *testing.T) {
769769
}
770770
cc, err := grpc.NewClient(r.Scheme()+":///"+beServerName, dopts...)
771771
if err != nil {
772-
t.Fatalf("Failed to dial to the backend %v", err)
772+
t.Fatalf("Failed to create a client for the backend %v", err)
773773
}
774774
defer cc.Close()
775775
testC := testgrpc.NewTestServiceClient(cc)
@@ -940,7 +940,7 @@ func (s) TestGRPCLB_ExplicitFallback(t *testing.T) {
940940
}
941941
cc, err := grpc.NewClient(r.Scheme()+":///"+beServerName, dopts...)
942942
if err != nil {
943-
t.Fatalf("Failed to dial to the backend %v", err)
943+
t.Fatalf("Failed to create a client for the backend %v", err)
944944
}
945945
defer cc.Close()
946946
testC := testgrpc.NewTestServiceClient(cc)
@@ -1008,11 +1008,12 @@ func (s) TestGRPCLB_FallBackWithNoServerAddress(t *testing.T) {
10081008
grpc.WithTransportCredentials(&serverNameCheckCreds{}),
10091009
grpc.WithContextDialer(fakeNameDialer),
10101010
}
1011-
cc, err := grpc.Dial(r.Scheme()+":///"+beServerName, dopts...)
1011+
cc, err := grpc.NewClient(r.Scheme()+":///"+beServerName, dopts...)
10121012
if err != nil {
1013-
t.Fatalf("Failed to dial to the backend %v", err)
1013+
t.Fatalf("Failed to create a client for the backend %v", err)
10141014
}
10151015
defer cc.Close()
1016+
cc.Connect()
10161017
testC := testgrpc.NewTestServiceClient(cc)
10171018

10181019
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
@@ -1102,10 +1103,11 @@ func (s) TestGRPCLB_PickFirst(t *testing.T) {
11021103
grpc.WithTransportCredentials(&serverNameCheckCreds{}),
11031104
grpc.WithContextDialer(fakeNameDialer),
11041105
}
1105-
cc, err := grpc.Dial(r.Scheme()+":///"+beServerName, dopts...)
1106+
cc, err := grpc.NewClient(r.Scheme()+":///"+beServerName, dopts...)
11061107
if err != nil {
1107-
t.Fatalf("Failed to dial to the backend %v", err)
1108+
t.Fatalf("Failed to create a client for the backend: %v", err)
11081109
}
1110+
cc.Connect()
11091111
defer cc.Close()
11101112

11111113
// Push a service config with grpclb as the load balancing policy and
@@ -1198,7 +1200,7 @@ func (s) TestGRPCLB_BackendConnectionErrorPropagation(t *testing.T) {
11981200
grpc.WithTransportCredentials(&serverNameCheckCreds{}),
11991201
grpc.WithContextDialer(fakeNameDialer))
12001202
if err != nil {
1201-
t.Fatalf("Failed to create new client to the backend %v", err)
1203+
t.Fatalf("Failed to create a client for the backend: %v", err)
12021204
}
12031205
defer cc.Close()
12041206
testC := testgrpc.NewTestServiceClient(cc)
@@ -1241,10 +1243,11 @@ func testGRPCLBEmptyServerList(t *testing.T, svcfg string) {
12411243
grpc.WithTransportCredentials(&serverNameCheckCreds{}),
12421244
grpc.WithContextDialer(fakeNameDialer),
12431245
}
1244-
cc, err := grpc.DialContext(ctx, r.Scheme()+":///"+beServerName, dopts...)
1246+
cc, err := grpc.NewClient(r.Scheme()+":///"+beServerName, dopts...)
12451247
if err != nil {
1246-
t.Fatalf("Failed to dial to the backend %v", err)
1248+
t.Fatalf("Failed to create a client for the backend %v", err)
12471249
}
1250+
cc.Connect()
12481251
defer cc.Close()
12491252
testC := testgrpc.NewTestServiceClient(cc)
12501253

@@ -1311,15 +1314,16 @@ func (s) TestGRPCLBWithTargetNameFieldInConfig(t *testing.T) {
13111314
// Push the backend address to the remote balancer.
13121315
tss.ls.sls <- sl
13131316

1314-
cc, err := grpc.Dial(r.Scheme()+":///"+beServerName,
1317+
cc, err := grpc.NewClient(r.Scheme()+":///"+beServerName,
13151318
grpc.WithResolvers(r),
13161319
grpc.WithTransportCredentials(&serverNameCheckCreds{}),
13171320
grpc.WithContextDialer(fakeNameDialer),
13181321
grpc.WithUserAgent(testUserAgent))
13191322
if err != nil {
1320-
t.Fatalf("Failed to dial to the backend %v", err)
1323+
t.Fatalf("Failed to create a client for the backend %v", err)
13211324
}
13221325
defer cc.Close()
1326+
cc.Connect()
13231327
testC := testgrpc.NewTestServiceClient(cc)
13241328

13251329
// Push a resolver update with grpclb configuration which does not contain the
@@ -1418,15 +1422,14 @@ func runAndCheckStats(t *testing.T, drop bool, statsChan chan *lbpb.ClientStats,
14181422
tss.ls.statsDura = 100 * time.Millisecond
14191423
creds := serverNameCheckCreds{}
14201424

1421-
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
1422-
defer cancel()
1423-
cc, err := grpc.DialContext(ctx, r.Scheme()+":///"+beServerName, grpc.WithResolvers(r),
1425+
cc, err := grpc.NewClient(r.Scheme()+":///"+beServerName, grpc.WithResolvers(r),
14241426
grpc.WithTransportCredentials(&creds),
14251427
grpc.WithPerRPCCredentials(failPreRPCCred{}),
14261428
grpc.WithContextDialer(fakeNameDialer))
14271429
if err != nil {
1428-
t.Fatalf("Failed to dial to the backend %v", err)
1430+
t.Fatalf("Failed to create a client for the backend %v", err)
14291431
}
1432+
cc.Connect()
14301433
defer cc.Close()
14311434

14321435
rstate := resolver.State{ServiceConfig: r.CC.ParseServiceConfig(grpclbConfig)}

balancer/rls/picker_test.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -267,9 +267,9 @@ func (s) Test_RLSDefaultTargetPicksMetric(t *testing.T) {
267267
r := startManualResolverWithConfig(t, rlsConfig)
268268

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

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

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

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

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

resolver_balancer_ext_test.go

+6-4
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,12 @@ func (s) TestResolverBalancerInteraction(t *testing.T) {
6666
rb.ResolveNowCallback = func(resolver.ResolveNowOptions) { close(rnCh) }
6767
resolver.Register(rb)
6868

69-
cc, err := grpc.Dial(name+":///", grpc.WithTransportCredentials(insecure.NewCredentials()))
69+
cc, err := grpc.NewClient(name+":///", grpc.WithTransportCredentials(insecure.NewCredentials()))
7070
if err != nil {
71-
t.Fatalf("grpc.Dial error: %v", err)
71+
t.Fatalf("grpc.NewClient error: %v", err)
7272
}
7373
defer cc.Close()
74+
cc.Connect()
7475
select {
7576
case <-rnCh:
7677
case <-time.After(defaultTestTimeout):
@@ -109,11 +110,12 @@ func (s) TestResolverBuildFailure(t *testing.T) {
109110
resolver.Register(&resolverBuilderWithErr{errCh: resErrCh, scheme: name})
110111

111112
resErrCh <- nil
112-
cc, err := grpc.Dial(name+":///", grpc.WithTransportCredentials(insecure.NewCredentials()))
113+
cc, err := grpc.NewClient(name+":///", grpc.WithTransportCredentials(insecure.NewCredentials()))
113114
if err != nil {
114-
t.Fatalf("grpc.Dial error: %v", err)
115+
t.Fatalf("grpc.NewClient error: %v", err)
115116
}
116117
defer cc.Close()
118+
cc.Connect()
117119
enterIdle(cc)
118120
const errStr = "test error from resolver builder"
119121
t.Log("pushing res err")

test/creds_test.go

+14-13
Original file line numberDiff line numberDiff line change
@@ -236,9 +236,9 @@ func (s) TestFailFastRPCErrorOnBadCertificates(t *testing.T) {
236236
opts := []grpc.DialOption{grpc.WithTransportCredentials(clientAlwaysFailCred{})}
237237
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
238238
defer cancel()
239-
cc, err := grpc.DialContext(ctx, te.srvAddr, opts...)
239+
cc, err := grpc.NewClient(te.srvAddr, opts...)
240240
if err != nil {
241-
t.Fatalf("Dial(_) = %v, want %v", err, nil)
241+
t.Fatalf("NewClient(_) = %v, want %v", err, nil)
242242
}
243243
defer cc.Close()
244244

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

264264
opts := []grpc.DialOption{grpc.WithTransportCredentials(clientAlwaysFailCred{})}
265-
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
266-
defer cancel()
267-
cc, err := grpc.DialContext(ctx, te.srvAddr, opts...)
265+
cc, err := grpc.NewClient(te.srvAddr, opts...)
268266
if err != nil {
269-
t.Fatalf("Dial(_) = %v, want %v", err, nil)
267+
t.Fatalf("NewClient(_) = %v, want %v", err, nil)
270268
}
271269
defer cc.Close()
272270

273271
tc := testgrpc.NewTestServiceClient(cc)
274-
ctx, cancel = context.WithTimeout(context.Background(), defaultTestShortTimeout)
272+
ctx, cancel := context.WithTimeout(context.Background(), defaultTestShortTimeout)
275273
defer cancel()
276274
if _, err = tc.EmptyCall(ctx, &testpb.Empty{}, grpc.WaitForReady(true)); !strings.Contains(err.Error(), clientAlwaysFailCredErrorMsg) {
277275
t.Fatalf("TestService/EmptyCall(_, _) = _, %v, want err.Error() contains %q", err, clientAlwaysFailCredErrorMsg)
@@ -437,11 +435,12 @@ func (s) TestCredsHandshakeAuthority(t *testing.T) {
437435

438436
r := manual.NewBuilderWithScheme("whatever")
439437

440-
cc, err := grpc.Dial(r.Scheme()+":///"+testAuthority, grpc.WithTransportCredentials(cred), grpc.WithResolvers(r))
438+
cc, err := grpc.NewClient(r.Scheme()+":///"+testAuthority, grpc.WithTransportCredentials(cred), grpc.WithResolvers(r))
441439
if err != nil {
442-
t.Fatalf("grpc.Dial(%q) = %v", lis.Addr().String(), err)
440+
t.Fatalf("grpc.NewClient(%q) = %v", lis.Addr().String(), err)
443441
}
444442
defer cc.Close()
443+
cc.Connect()
445444
r.UpdateState(resolver.State{Addresses: []resolver.Address{{Addr: lis.Addr().String()}}})
446445

447446
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
@@ -469,11 +468,12 @@ func (s) TestCredsHandshakeServerNameAuthority(t *testing.T) {
469468

470469
r := manual.NewBuilderWithScheme("whatever")
471470

472-
cc, err := grpc.Dial(r.Scheme()+":///"+testAuthority, grpc.WithTransportCredentials(cred), grpc.WithResolvers(r))
471+
cc, err := grpc.NewClient(r.Scheme()+":///"+testAuthority, grpc.WithTransportCredentials(cred), grpc.WithResolvers(r))
473472
if err != nil {
474-
t.Fatalf("grpc.Dial(%q) = %v", lis.Addr().String(), err)
473+
t.Fatalf("grpc.NewClient(%q) = %v", lis.Addr().String(), err)
475474
}
476475
defer cc.Close()
476+
cc.Connect()
477477
r.UpdateState(resolver.State{Addresses: []resolver.Address{{Addr: lis.Addr().String(), ServerName: testServerName}}})
478478

479479
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
@@ -524,11 +524,12 @@ func (s) TestServerCredsDispatch(t *testing.T) {
524524
go s.Serve(lis)
525525
defer s.Stop()
526526

527-
cc, err := grpc.Dial(lis.Addr().String(), grpc.WithTransportCredentials(cred))
527+
cc, err := grpc.NewClient(lis.Addr().String(), grpc.WithTransportCredentials(cred))
528528
if err != nil {
529-
t.Fatalf("grpc.Dial(%q) = %v", lis.Addr().String(), err)
529+
t.Fatalf("grpc.NewClient(%q) = %v", lis.Addr().String(), err)
530530
}
531531
defer cc.Close()
532+
cc.Connect()
532533

533534
rawConn := cred.getRawConn()
534535
// Give grpc a chance to see the error and potentially close the connection.

test/end2end_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -609,7 +609,7 @@ func (te *test) listenAndServe(ts testgrpc.TestServiceServer, listen func(networ
609609
if te.serverInitialConnWindowSize > 0 {
610610
sopts = append(sopts, grpc.InitialConnWindowSize(te.serverInitialConnWindowSize))
611611
}
612-
la := "localhost:0"
612+
la := ":0"
613613
switch te.e.network {
614614
case "unix":
615615
la = "/tmp/testsock" + fmt.Sprintf("%d", time.Now().UnixNano())

0 commit comments

Comments
 (0)