MCPcopy
hub / github.com/grpc/grpc-go / TestPickerStatusCodes

Method TestPickerStatusCodes

test/control_plane_status_test.go:89–148  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

87}
88
89func (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 })

Callers

nothing calls this directly

Calls 14

StartMethod · 0.95
StopMethod · 0.95
NewServiceConfigMethod · 0.95
ErrorfFunction · 0.92
NewErrPickerFunction · 0.92
RegisterFunction · 0.92
CodeFunction · 0.92
ConvertFunction · 0.92
ErrMethod · 0.80
MessageMethod · 0.80
FatalfMethod · 0.65
UpdateStateMethod · 0.65

Tested by

no test coverage detected