Skip to content

Commit 47e1ebe

Browse files
yuqitaomenghanl
authored andcommitted
client: return helpful error message when wait-for-ready RPCs fail with timeout (#2777)
1 parent a90198c commit 47e1ebe

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

Diff for: picker_wrapper.go

+8
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,14 @@ func (bp *pickerWrapper) pick(ctx context.Context, failfast bool, opts balancer.
120120
bp.mu.Unlock()
121121
select {
122122
case <-ctx.Done():
123+
if connectionErr := bp.connectionError(); connectionErr != nil {
124+
switch ctx.Err() {
125+
case context.DeadlineExceeded:
126+
return nil, nil, status.Errorf(codes.DeadlineExceeded, "latest connection error: %v", connectionErr)
127+
case context.Canceled:
128+
return nil, nil, status.Errorf(codes.Canceled, "latest connection error: %v", connectionErr)
129+
}
130+
}
123131
return nil, nil, ctx.Err()
124132
case <-ch:
125133
}

Diff for: test/end2end_test.go

+23
Original file line numberDiff line numberDiff line change
@@ -6716,6 +6716,29 @@ func (s) TestFailFastRPCErrorOnBadCertificates(t *testing.T) {
67166716
te.t.Fatalf("TestService/EmptyCall(_, _) = _, %v, want err.Error() contains %q", err, clientAlwaysFailCredErrorMsg)
67176717
}
67186718

6719+
func (s) TestWaitForReadyRPCErrorOnBadCertificates(t *testing.T) {
6720+
te := newTest(t, env{name: "bad-cred", network: "tcp", security: "clientAlwaysFailCred", balancer: "round_robin"})
6721+
te.startServer(&testServer{security: te.e.security})
6722+
defer te.tearDown()
6723+
6724+
opts := []grpc.DialOption{grpc.WithTransportCredentials(clientAlwaysFailCred{})}
6725+
dctx, dcancel := context.WithTimeout(context.Background(), 10*time.Second)
6726+
defer dcancel()
6727+
cc, err := grpc.DialContext(dctx, te.srvAddr, opts...)
6728+
if err != nil {
6729+
t.Fatalf("Dial(_) = %v, want %v", err, nil)
6730+
}
6731+
defer cc.Close()
6732+
6733+
tc := testpb.NewTestServiceClient(cc)
6734+
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
6735+
defer cancel()
6736+
if _, err = tc.EmptyCall(ctx, &testpb.Empty{}, grpc.WaitForReady(true)); strings.Contains(err.Error(), clientAlwaysFailCredErrorMsg) {
6737+
return
6738+
}
6739+
te.t.Fatalf("TestService/EmptyCall(_, _) = _, %v, want err.Error() contains %q", err, clientAlwaysFailCredErrorMsg)
6740+
}
6741+
67196742
func (s) TestRPCTimeout(t *testing.T) {
67206743
for _, e := range listTestEnv() {
67216744
testRPCTimeout(t, e)

0 commit comments

Comments
 (0)