From 2648448cdff6e20850046ae69a02997477e7079b Mon Sep 17 00:00:00 2001 From: Vissa Janardhan Krishna Sai Date: Wed, 11 Dec 2024 05:06:39 +0000 Subject: [PATCH 01/11] replace grpc.Dial with grpc.NewClient --- balancer/rls/picker_test.go | 12 +++--- resolver_balancer_ext_test.go | 10 +++-- test/creds_test.go | 80 ++++++++++++++++++++++++----------- 3 files changed, 68 insertions(+), 34 deletions(-) diff --git a/balancer/rls/picker_test.go b/balancer/rls/picker_test.go index cb08c433f9e8..a0bdbc827921 100644 --- a/balancer/rls/picker_test.go +++ b/balancer/rls/picker_test.go @@ -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() @@ -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() @@ -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() diff --git a/resolver_balancer_ext_test.go b/resolver_balancer_ext_test.go index 417a5781ef80..599d3cc46079 100644 --- a/resolver_balancer_ext_test.go +++ b/resolver_balancer_ext_test.go @@ -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): @@ -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") diff --git a/test/creds_test.go b/test/creds_test.go index ff3818e0ad54..255092adb339 100644 --- a/test/creds_test.go +++ b/test/creds_test.go @@ -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" @@ -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() @@ -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) @@ -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) @@ -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) @@ -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() From b3f2f5e69c1019ae577825f8620cc0213b984c4f Mon Sep 17 00:00:00 2001 From: Vissa Janardhan Krishna Sai Date: Wed, 11 Dec 2024 10:15:42 +0000 Subject: [PATCH 02/11] re-trigger --- test/balancer_switching_test.go | 1 - 1 file changed, 1 deletion(-) diff --git a/test/balancer_switching_test.go b/test/balancer_switching_test.go index 2e46fcdb4a76..8e337a2106ab 100644 --- a/test/balancer_switching_test.go +++ b/test/balancer_switching_test.go @@ -128,7 +128,6 @@ func (s) TestBalancerSwitch_Basic(t *testing.T) { addrs := stubBackendsToResolverAddrs(backends) r := manual.NewBuilderWithScheme("whatever") - r.InitialState(resolver.State{Addresses: addrs}) cc, err := grpc.NewClient(r.Scheme()+":///test.server", grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithResolvers(r)) From 76915bdd9d2becc6a2a49bd80e9bea31b572adfe Mon Sep 17 00:00:00 2001 From: Vissa Janardhan Krishna Sai Date: Wed, 11 Dec 2024 10:20:29 +0000 Subject: [PATCH 03/11] re-trigger --- test/balancer_switching_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/test/balancer_switching_test.go b/test/balancer_switching_test.go index 8e337a2106ab..2e46fcdb4a76 100644 --- a/test/balancer_switching_test.go +++ b/test/balancer_switching_test.go @@ -128,6 +128,7 @@ func (s) TestBalancerSwitch_Basic(t *testing.T) { addrs := stubBackendsToResolverAddrs(backends) r := manual.NewBuilderWithScheme("whatever") + r.InitialState(resolver.State{Addresses: addrs}) cc, err := grpc.NewClient(r.Scheme()+":///test.server", grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithResolvers(r)) From 1cba7c2d591794cbd02028021f6fac508961d570 Mon Sep 17 00:00:00 2001 From: Vissa Janardhan Krishna Sai Date: Wed, 11 Dec 2024 15:24:56 +0000 Subject: [PATCH 04/11] using r.InitialState instead of r.UpdateState --- test/creds_test.go | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/test/creds_test.go b/test/creds_test.go index 255092adb339..8074109f0b01 100644 --- a/test/creds_test.go +++ b/test/creds_test.go @@ -441,18 +441,16 @@ func (s) TestCredsHandshakeAuthority(t *testing.T) { r := manual.NewBuilderWithScheme("whatever") + r.InitialState(resolver.State{ + Addresses: []resolver.Address{{Addr: lis.Addr().String()}}, + }) + cc, err := grpc.NewClient(r.Scheme()+":///"+testAuthority, grpc.WithTransportCredentials(cred), grpc.WithResolvers(r)) if err != nil { t.Fatalf("grpc.NewClient(%q) = %v", lis.Addr().String(), err) } defer cc.Close() - // 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() From 6cebed002954f1256208a960d0eabf3ee1b7f9a5 Mon Sep 17 00:00:00 2001 From: Vissa Janardhan Krishna Sai Date: Wed, 11 Dec 2024 16:00:24 +0000 Subject: [PATCH 05/11] using rb.InitialState instead of cc.UpdateState --- resolver_balancer_ext_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/resolver_balancer_ext_test.go b/resolver_balancer_ext_test.go index 599d3cc46079..2f187e8a32f9 100644 --- a/resolver_balancer_ext_test.go +++ b/resolver_balancer_ext_test.go @@ -57,7 +57,7 @@ func (s) TestResolverBalancerInteraction(t *testing.T) { rb := manual.NewBuilderWithScheme(name) rb.BuildCallback = func(_ resolver.Target, cc resolver.ClientConn, _ resolver.BuildOptions) { sc := cc.ParseServiceConfig(`{"loadBalancingConfig": [{"` + name + `":{}}]}`) - cc.UpdateState(resolver.State{ + rb.InitialState(resolver.State{ Addresses: []resolver.Address{{Addr: "test"}}, ServiceConfig: sc, }) @@ -66,7 +66,7 @@ func (s) TestResolverBalancerInteraction(t *testing.T) { rb.ResolveNowCallback = func(resolver.ResolveNowOptions) { close(rnCh) } resolver.Register(rb) - cc, err := grpc.NewClient(name+":///", grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithDefaultServiceConfig(`{"loadBalancingConfig": [{"`+name+`":{}}]}`)) + cc, err := grpc.NewClient(name+":///", grpc.WithTransportCredentials(insecure.NewCredentials())) if err != nil { t.Fatalf("grpc.NewClient error: %v", err) } From 1c11e2308fe8f852cbdeced74f08c2d4ce654764 Mon Sep 17 00:00:00 2001 From: Vissa Janardhan Krishna Sai Date: Fri, 13 Dec 2024 05:11:41 +0000 Subject: [PATCH 06/11] reverting stubserver changes --- test/creds_test.go | 21 +++++++-------------- 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/test/creds_test.go b/test/creds_test.go index 8074109f0b01..0019d0f55454 100644 --- a/test/creds_test.go +++ b/test/creds_test.go @@ -29,9 +29,11 @@ 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/stubserver" + "google.golang.org/grpc/internal/testutils" "google.golang.org/grpc/metadata" "google.golang.org/grpc/resolver" "google.golang.org/grpc/resolver/manual" @@ -430,14 +432,9 @@ func (s) TestCredsHandshakeAuthority(t *testing.T) { defer lis.Close() cred := &authorityCheckCreds{} - 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) + s := grpc.NewServer() + go s.Serve(lis) + defer s.Stop() r := manual.NewBuilderWithScheme("whatever") @@ -449,16 +446,12 @@ func (s) TestCredsHandshakeAuthority(t *testing.T) { if err != nil { t.Fatalf("grpc.NewClient(%q) = %v", lis.Addr().String(), err) } + cc.Connect() defer cc.Close() ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout) defer cancel() - - // 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) - } + testutils.AwaitState(ctx, t, cc, connectivity.Ready) if cred.got != testAuthority { t.Fatalf("client creds got authority: %q, want: %q", cred.got, testAuthority) From 6e646721007594e1cff2729b1de81d742ac5696f Mon Sep 17 00:00:00 2001 From: Vissa Janardhan Krishna Sai Date: Fri, 13 Dec 2024 05:20:40 +0000 Subject: [PATCH 07/11] reverting stubserver changes --- test/creds_test.go | 28 ++++++---------------------- 1 file changed, 6 insertions(+), 22 deletions(-) diff --git a/test/creds_test.go b/test/creds_test.go index 0019d0f55454..6fe5e2455771 100644 --- a/test/creds_test.go +++ b/test/creds_test.go @@ -32,7 +32,6 @@ import ( "google.golang.org/grpc/connectivity" "google.golang.org/grpc/credentials" "google.golang.org/grpc/credentials/insecure" - "google.golang.org/grpc/internal/stubserver" "google.golang.org/grpc/internal/testutils" "google.golang.org/grpc/metadata" "google.golang.org/grpc/resolver" @@ -470,37 +469,22 @@ func (s) TestCredsHandshakeServerNameAuthority(t *testing.T) { cred := &authorityCheckCreds{} 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) + s := grpc.NewServer() + go s.Serve(lis) + defer s.Stop() r := manual.NewBuilderWithScheme("whatever") - + r.InitialState(resolver.State{Addresses: []resolver.Address{{Addr: lis.Addr().String(), ServerName: testServerName}}}) cc, err := grpc.NewClient(r.Scheme()+":///"+testAuthority, grpc.WithTransportCredentials(cred), grpc.WithResolvers(r)) if err != nil { t.Fatalf("grpc.NewClient(%q) = %v", lis.Addr().String(), err) } + cc.Connect() defer cc.Close() - // 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() - - // 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) - } + testutils.AwaitState(ctx, t, cc, connectivity.Ready) if cred.got != testServerName { t.Fatalf("client creds got authority: %q, want: %q", cred.got, testAuthority) From 0cb8e3968c6168d3af3ecf82cb21159799c45109 Mon Sep 17 00:00:00 2001 From: Vissa Janardhan Krishna Sai Date: Fri, 13 Dec 2024 08:10:17 +0000 Subject: [PATCH 08/11] reverting rb.InitialState from cc.UpdateState --- resolver_balancer_ext_test.go | 2 +- test/creds_test.go | 12 ++++-------- 2 files changed, 5 insertions(+), 9 deletions(-) diff --git a/resolver_balancer_ext_test.go b/resolver_balancer_ext_test.go index 2f187e8a32f9..75fac51a66db 100644 --- a/resolver_balancer_ext_test.go +++ b/resolver_balancer_ext_test.go @@ -57,7 +57,7 @@ func (s) TestResolverBalancerInteraction(t *testing.T) { rb := manual.NewBuilderWithScheme(name) rb.BuildCallback = func(_ resolver.Target, cc resolver.ClientConn, _ resolver.BuildOptions) { sc := cc.ParseServiceConfig(`{"loadBalancingConfig": [{"` + name + `":{}}]}`) - rb.InitialState(resolver.State{ + cc.UpdateState(resolver.State{ Addresses: []resolver.Address{{Addr: "test"}}, ServiceConfig: sc, }) diff --git a/test/creds_test.go b/test/creds_test.go index 6fe5e2455771..f0a964cf293f 100644 --- a/test/creds_test.go +++ b/test/creds_test.go @@ -428,7 +428,6 @@ func (s) TestCredsHandshakeAuthority(t *testing.T) { if err != nil { t.Fatal(err) } - defer lis.Close() cred := &authorityCheckCreds{} s := grpc.NewServer() @@ -437,10 +436,6 @@ func (s) TestCredsHandshakeAuthority(t *testing.T) { r := manual.NewBuilderWithScheme("whatever") - r.InitialState(resolver.State{ - Addresses: []resolver.Address{{Addr: lis.Addr().String()}}, - }) - cc, err := grpc.NewClient(r.Scheme()+":///"+testAuthority, grpc.WithTransportCredentials(cred), grpc.WithResolvers(r)) if err != nil { t.Fatalf("grpc.NewClient(%q) = %v", lis.Addr().String(), err) @@ -448,6 +443,8 @@ func (s) TestCredsHandshakeAuthority(t *testing.T) { cc.Connect() defer cc.Close() + 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) @@ -467,20 +464,19 @@ func (s) TestCredsHandshakeServerNameAuthority(t *testing.T) { t.Fatal(err) } cred := &authorityCheckCreds{} - defer lis.Close() - s := grpc.NewServer() go s.Serve(lis) defer s.Stop() r := manual.NewBuilderWithScheme("whatever") - r.InitialState(resolver.State{Addresses: []resolver.Address{{Addr: lis.Addr().String(), ServerName: testServerName}}}) + cc, err := grpc.NewClient(r.Scheme()+":///"+testAuthority, grpc.WithTransportCredentials(cred), grpc.WithResolvers(r)) if err != nil { t.Fatalf("grpc.NewClient(%q) = %v", lis.Addr().String(), err) } cc.Connect() defer cc.Close() + r.UpdateState(resolver.State{Addresses: []resolver.Address{{Addr: lis.Addr().String(), ServerName: testServerName}}}) ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout) defer cancel() From 46449b71cb37bfbb5e71f0b200bd89c1f0c4f97c Mon Sep 17 00:00:00 2001 From: Vissa Janardhan Krishna Sai Date: Fri, 13 Dec 2024 08:11:45 +0000 Subject: [PATCH 09/11] removing empty lines --- test/creds_test.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/test/creds_test.go b/test/creds_test.go index f0a964cf293f..53c041f17be2 100644 --- a/test/creds_test.go +++ b/test/creds_test.go @@ -428,7 +428,6 @@ func (s) TestCredsHandshakeAuthority(t *testing.T) { if err != nil { t.Fatal(err) } - cred := &authorityCheckCreds{} s := grpc.NewServer() go s.Serve(lis) @@ -442,7 +441,6 @@ func (s) TestCredsHandshakeAuthority(t *testing.T) { } cc.Connect() defer cc.Close() - r.UpdateState(resolver.State{Addresses: []resolver.Address{{Addr: lis.Addr().String()}}}) ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout) From 17366d0b5c3ef2de8904168bf9381c671210a77d Mon Sep 17 00:00:00 2001 From: Vissa Janardhan Krishna Sai Date: Fri, 13 Dec 2024 11:28:40 +0000 Subject: [PATCH 10/11] replacing dial with newclient --- balancer/grpclb/grpclb_test.go | 37 ++++++++++++++++++---------------- test/creds_test.go | 2 +- 2 files changed, 21 insertions(+), 18 deletions(-) diff --git a/balancer/grpclb/grpclb_test.go b/balancer/grpclb/grpclb_test.go index 62dc947e0ee5..0bba53ac1469 100644 --- a/balancer/grpclb/grpclb_test.go +++ b/balancer/grpclb/grpclb_test.go @@ -460,7 +460,7 @@ func (s) TestGRPCLB_Basic(t *testing.T) { } cc, err := grpc.NewClient(r.Scheme()+":///"+beServerName, dopts...) if err != nil { - t.Fatalf("Failed to dial to the backend %v", err) + t.Fatalf("Failed to create a newclient to the backend %v", err) } defer cc.Close() @@ -517,7 +517,7 @@ func (s) TestGRPCLB_Weighted(t *testing.T) { } cc, err := grpc.NewClient(r.Scheme()+":///"+beServerName, dopts...) if err != nil { - t.Fatalf("Failed to dial to the backend %v", err) + t.Fatalf("Failed to create a newclient to the backend %v", err) } defer cc.Close() @@ -597,7 +597,7 @@ func (s) TestGRPCLB_DropRequest(t *testing.T) { } cc, err := grpc.NewClient(r.Scheme()+":///"+beServerName, dopts...) if err != nil { - t.Fatalf("Failed to dial to the backend %v", err) + t.Fatalf("Failed to create a newclient to the backend %v", err) } defer cc.Close() testC := testgrpc.NewTestServiceClient(cc) @@ -769,7 +769,7 @@ func (s) TestGRPCLB_BalancerDisconnects(t *testing.T) { } cc, err := grpc.NewClient(r.Scheme()+":///"+beServerName, dopts...) if err != nil { - t.Fatalf("Failed to dial to the backend %v", err) + t.Fatalf("Failed to create a newclient to the backend %v", err) } defer cc.Close() testC := testgrpc.NewTestServiceClient(cc) @@ -940,7 +940,7 @@ func (s) TestGRPCLB_ExplicitFallback(t *testing.T) { } cc, err := grpc.NewClient(r.Scheme()+":///"+beServerName, dopts...) if err != nil { - t.Fatalf("Failed to dial to the backend %v", err) + t.Fatalf("Failed to create a newclient to the backend %v", err) } defer cc.Close() testC := testgrpc.NewTestServiceClient(cc) @@ -1008,11 +1008,12 @@ func (s) TestGRPCLB_FallBackWithNoServerAddress(t *testing.T) { grpc.WithTransportCredentials(&serverNameCheckCreds{}), grpc.WithContextDialer(fakeNameDialer), } - cc, err := grpc.Dial(r.Scheme()+":///"+beServerName, dopts...) + cc, err := grpc.NewClient(r.Scheme()+":///"+beServerName, dopts...) if err != nil { - t.Fatalf("Failed to dial to the backend %v", err) + t.Fatalf("Failed to create a newclient to the backend %v", err) } defer cc.Close() + cc.Connect() testC := testgrpc.NewTestServiceClient(cc) ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout) @@ -1102,10 +1103,11 @@ func (s) TestGRPCLB_PickFirst(t *testing.T) { grpc.WithTransportCredentials(&serverNameCheckCreds{}), grpc.WithContextDialer(fakeNameDialer), } - cc, err := grpc.Dial(r.Scheme()+":///"+beServerName, dopts...) + cc, err := grpc.NewClient(r.Scheme()+":///"+beServerName, dopts...) if err != nil { - t.Fatalf("Failed to dial to the backend %v", err) + t.Fatalf("Failed to create a newclient: %v", err) } + cc.Connect() defer cc.Close() // Push a service config with grpclb as the load balancing policy and @@ -1241,10 +1243,11 @@ func testGRPCLBEmptyServerList(t *testing.T, svcfg string) { grpc.WithTransportCredentials(&serverNameCheckCreds{}), grpc.WithContextDialer(fakeNameDialer), } - cc, err := grpc.DialContext(ctx, r.Scheme()+":///"+beServerName, dopts...) + cc, err := grpc.NewClient(r.Scheme()+":///"+beServerName, dopts...) if err != nil { - t.Fatalf("Failed to dial to the backend %v", err) + t.Fatalf("Failed to create a newclient to the backend %v", err) } + cc.Connect() defer cc.Close() testC := testgrpc.NewTestServiceClient(cc) @@ -1311,15 +1314,16 @@ func (s) TestGRPCLBWithTargetNameFieldInConfig(t *testing.T) { // Push the backend address to the remote balancer. tss.ls.sls <- sl - cc, err := grpc.Dial(r.Scheme()+":///"+beServerName, + cc, err := grpc.NewClient(r.Scheme()+":///"+beServerName, grpc.WithResolvers(r), grpc.WithTransportCredentials(&serverNameCheckCreds{}), grpc.WithContextDialer(fakeNameDialer), grpc.WithUserAgent(testUserAgent)) if err != nil { - t.Fatalf("Failed to dial to the backend %v", err) + t.Fatalf("Failed to create a newclient to the backend %v", err) } defer cc.Close() + cc.Connect() testC := testgrpc.NewTestServiceClient(cc) // 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, tss.ls.statsDura = 100 * time.Millisecond creds := serverNameCheckCreds{} - ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) - defer cancel() - cc, err := grpc.DialContext(ctx, r.Scheme()+":///"+beServerName, grpc.WithResolvers(r), + cc, err := grpc.NewClient(r.Scheme()+":///"+beServerName, grpc.WithResolvers(r), grpc.WithTransportCredentials(&creds), grpc.WithPerRPCCredentials(failPreRPCCred{}), grpc.WithContextDialer(fakeNameDialer)) if err != nil { - t.Fatalf("Failed to dial to the backend %v", err) + t.Fatalf("Failed to create a newclient to the backend %v", err) } + cc.Connect() defer cc.Close() rstate := resolver.State{ServiceConfig: r.CC.ParseServiceConfig(grpclbConfig)} diff --git a/test/creds_test.go b/test/creds_test.go index 53c041f17be2..00702fd73bfd 100644 --- a/test/creds_test.go +++ b/test/creds_test.go @@ -264,7 +264,7 @@ func (s) TestWaitForReadyRPCErrorOnBadCertificates(t *testing.T) { opts := []grpc.DialOption{grpc.WithTransportCredentials(clientAlwaysFailCred{})} 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() From f2c6c0072be1bb9739d87d32b514fbbf353f41d3 Mon Sep 17 00:00:00 2001 From: Vissa Janardhan Krishna Sai Date: Sun, 15 Dec 2024 06:35:58 +0000 Subject: [PATCH 11/11] updating default listener address --- test/end2end_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/end2end_test.go b/test/end2end_test.go index 2238f8f12422..6ea4670af1aa 100644 --- a/test/end2end_test.go +++ b/test/end2end_test.go @@ -609,7 +609,7 @@ func (te *test) listenAndServe(ts testgrpc.TestServiceServer, listen func(networ if te.serverInitialConnWindowSize > 0 { sopts = append(sopts, grpc.InitialConnWindowSize(te.serverInitialConnWindowSize)) } - la := "localhost:0" + la := "[::]:0" switch te.e.network { case "unix": la = "/tmp/testsock" + fmt.Sprintf("%d", time.Now().UnixNano())