(t *testing.T)
| 87 | } |
| 88 | |
| 89 | func (s) TestPickerStatusCodes(t *testing.T) { |
| 90 | testCases := []struct { |
| 91 | name string |
| 92 | pickerErr error |
| 93 | want error |
| 94 | }{{ |
| 95 | name: "legal status code", |
| 96 | pickerErr: status.Errorf(codes.Unavailable, "this error is fine"), |
| 97 | want: status.Errorf(codes.Unavailable, "this error is fine"), |
| 98 | }, { |
| 99 | name: "illegal status code", |
| 100 | pickerErr: status.Errorf(codes.NotFound, "this error is bad"), |
| 101 | want: status.Errorf(codes.Internal, "this error is bad"), |
| 102 | }} |
| 103 | |
| 104 | for _, tc := range testCases { |
| 105 | t.Run(tc.name, func(t *testing.T) { |
| 106 | ss := &stubserver.StubServer{ |
| 107 | EmptyCallF: func(context.Context, *testpb.Empty) (*testpb.Empty, error) { |
| 108 | return &testpb.Empty{}, nil |
| 109 | }, |
| 110 | } |
| 111 | |
| 112 | if err := ss.Start(nil); err != nil { |
| 113 | t.Fatalf("Error starting endpoint server: %v", err) |
| 114 | } |
| 115 | defer ss.Stop() |
| 116 | |
| 117 | // Create a stub balancer that creates a picker that always returns |
| 118 | // an error. |
| 119 | sbf := stub.BalancerFuncs{ |
| 120 | UpdateClientConnState: func(d *stub.BalancerData, _ balancer.ClientConnState) error { |
| 121 | d.ClientConn.UpdateState(balancer.State{ |
| 122 | ConnectivityState: connectivity.TransientFailure, |
| 123 | Picker: base.NewErrPicker(tc.pickerErr), |
| 124 | }) |
| 125 | return nil |
| 126 | }, |
| 127 | } |
| 128 | stub.Register("testPickerStatusCodesBalancer", sbf) |
| 129 | |
| 130 | ss.NewServiceConfig(`{"loadBalancingConfig": [{"testPickerStatusCodesBalancer":{}}] }`) |
| 131 | |
| 132 | // Make calls until pickerErr is received. |
| 133 | ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout) |
| 134 | defer cancel() |
| 135 | |
| 136 | var lastErr error |
| 137 | for ctx.Err() == nil { |
| 138 | if _, lastErr = ss.Client.EmptyCall(ctx, &testpb.Empty{}); status.Code(lastErr) == status.Code(tc.want) && strings.Contains(lastErr.Error(), status.Convert(tc.want).Message()) { |
| 139 | // Success! |
| 140 | return |
| 141 | } |
| 142 | time.Sleep(time.Millisecond) |
| 143 | } |
| 144 | |
| 145 | t.Fatalf("client.EmptyCall(_, _) = _, %v; want _, %v", lastErr, tc.want) |
| 146 | }) |
nothing calls this directly
no test coverage detected