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

Method TestEncodeDoesntPanicOnClient

encoding/encoding_test.go:198–230  ·  view source on GitHub ↗

Tests the case where encoding fails on the client . Verifies that there is no panic and that the encoding error is propagated to the RPC caller.

(t *testing.T)

Source from the content-addressed store, hash-verified

196// Tests the case where encoding fails on the client . Verifies that there is
197// no panic and that the encoding error is propagated to the RPC caller.
198func (s) TestEncodeDoesntPanicOnClient(t *testing.T) {
199 // Start a server and since we do not specify any codec here, the proto
200 // codec will get automatically used.
201 backend := stubserver.StartTestService(t, nil)
202 defer backend.Stop()
203
204 // Create a codec that errors when encoding messages.
205 encodingErr := errors.New("encoding failed")
206 ec := &errProtoCodec{name: t.Name(), encodingErr: encodingErr}
207
208 // Create a channel to the above server.
209 cc, err := grpc.NewClient(backend.Address, grpc.WithTransportCredentials(insecure.NewCredentials()))
210 if err != nil {
211 t.Fatalf("Failed to dial test backend at %q: %v", backend.Address, err)
212 }
213 defer cc.Close()
214
215 // Make an RPC with the erroring codec and expect it to fail.
216 ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
217 defer cancel()
218 client := testgrpc.NewTestServiceClient(cc)
219 _, err = client.EmptyCall(ctx, &testpb.Empty{}, grpc.ForceCodecV2(ec))
220 if err == nil || !strings.Contains(err.Error(), encodingErr.Error()) {
221 t.Fatalf("RPC failed with error: %v, want: %v", err, encodingErr)
222 }
223
224 // Configure the codec on the client to not return errors anymore and expect
225 // the RPC to succeed.
226 ec.encodingErr = nil
227 if _, err := client.EmptyCall(ctx, &testpb.Empty{}, grpc.ForceCodecV2(ec)); err != nil {
228 t.Fatalf("RPC failed with error: %v", err)
229 }
230}
231
232// Tests the case where decoding fails on the server. Verifies that there is
233// no panic and that the decoding error is propagated to the RPC caller.

Callers

nothing calls this directly

Calls 11

EmptyCallMethod · 0.95
StartTestServiceFunction · 0.92
NewClientFunction · 0.92
WithTransportCredentialsFunction · 0.92
NewCredentialsFunction · 0.92
ForceCodecV2Function · 0.92
StopMethod · 0.65
NameMethod · 0.65
FatalfMethod · 0.65
CloseMethod · 0.65
ErrorMethod · 0.65

Tested by

no test coverage detected