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

Method TestEncodeDoesntPanicOnServer

encoding/encoding_test.go:114–153  ·  view source on GitHub ↗

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

(t *testing.T)

Source from the content-addressed store, hash-verified

112// Tests the case where encoding fails on the server. Verifies that there is
113// no panic and that the encoding error is propagated to the client.
114func (s) TestEncodeDoesntPanicOnServer(t *testing.T) {
115 grpctest.ExpectError("grpc: server failed to encode response")
116
117 // Create a codec that errors when encoding messages.
118 encodingErr := errors.New("encoding failed")
119 ec := &errProtoCodec{name: t.Name(), encodingErr: encodingErr}
120
121 // Start a server with the above codec.
122 backend1 := stubserver.StubServer{
123 EmptyCallF: func(context.Context, *testpb.Empty) (*testpb.Empty, error) { return &testpb.Empty{}, nil },
124 }
125 if err := backend1.Start([]grpc.ServerOption{grpc.ForceServerCodecV2(ec)}, grpc.WithTransportCredentials(insecure.NewCredentials())); err != nil {
126 t.Fatal(err)
127 }
128 defer backend1.Stop()
129
130 // Make an RPC and expect it to fail. Since we do not specify any codec
131 // here, the proto codec will get automatically used.
132 ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
133 defer cancel()
134 _, err := backend1.Client.EmptyCall(ctx, &testpb.Empty{})
135 if err == nil || !strings.Contains(err.Error(), encodingErr.Error()) {
136 t.Fatalf("RPC failed with error: %v, want: %v", err, encodingErr)
137 }
138
139 // Configure the codec on the server to not return errors anymore and expect
140 // the RPC to succeed.
141 ec = &errProtoCodec{name: t.Name()}
142 backend2 := stubserver.StubServer{
143 EmptyCallF: func(context.Context, *testpb.Empty) (*testpb.Empty, error) { return &testpb.Empty{}, nil },
144 }
145 if err := backend2.Start([]grpc.ServerOption{grpc.ForceServerCodecV2(ec)}, grpc.WithTransportCredentials(insecure.NewCredentials())); err != nil {
146 t.Fatal(err)
147 }
148 defer backend2.Stop()
149
150 if _, err := backend2.Client.EmptyCall(ctx, &testpb.Empty{}); err != nil {
151 t.Fatalf("RPC failed with error: %v", err)
152 }
153}
154
155// Tests the case where decoding fails on the server. Verifies that there is
156// no panic and that the decoding error is propagated to the client.

Callers

nothing calls this directly

Calls 11

StartMethod · 0.95
StopMethod · 0.95
ExpectErrorFunction · 0.92
ForceServerCodecV2Function · 0.92
WithTransportCredentialsFunction · 0.92
NewCredentialsFunction · 0.92
NameMethod · 0.65
FatalMethod · 0.65
EmptyCallMethod · 0.65
ErrorMethod · 0.65
FatalfMethod · 0.65

Tested by

no test coverage detected