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

Method TestDecodeDoesntPanicOnServer

encoding/encoding_test.go:157–194  ·  view source on GitHub ↗

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

(t *testing.T)

Source from the content-addressed store, hash-verified

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.
157func (s) TestDecodeDoesntPanicOnServer(t *testing.T) {
158 // Create a codec that errors when decoding messages.
159 decodingErr := errors.New("decoding failed")
160 ec := &errProtoCodec{name: t.Name(), decodingErr: decodingErr}
161
162 // Start a server with the above codec and a channel to the server.
163 backend1 := stubserver.StubServer{
164 EmptyCallF: func(context.Context, *testpb.Empty) (*testpb.Empty, error) { return &testpb.Empty{}, nil },
165 }
166 if err := backend1.Start([]grpc.ServerOption{grpc.ForceServerCodecV2(ec)}, grpc.WithTransportCredentials(insecure.NewCredentials())); err != nil {
167 t.Fatal(err)
168 }
169 defer backend1.Stop()
170
171 // Make an RPC and expect it to fail. Since we do not specify any codec
172 // here, the proto codec will get automatically used.
173 ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
174 defer cancel()
175 _, err := backend1.Client.EmptyCall(ctx, &testpb.Empty{})
176 if err == nil || !strings.Contains(err.Error(), decodingErr.Error()) || !strings.Contains(err.Error(), "grpc: error unmarshalling request") {
177 t.Fatalf("RPC failed with error: %v, want: %v", err, decodingErr)
178 }
179
180 // Configure the codec on the server to not return errors anymore and expect
181 // the RPC to succeed.
182 ec = &errProtoCodec{name: t.Name()}
183 backend2 := stubserver.StubServer{
184 EmptyCallF: func(context.Context, *testpb.Empty) (*testpb.Empty, error) { return &testpb.Empty{}, nil },
185 }
186 if err := backend2.Start([]grpc.ServerOption{grpc.ForceServerCodecV2(ec)}, grpc.WithTransportCredentials(insecure.NewCredentials())); err != nil {
187 t.Fatal(err)
188 }
189 defer backend2.Stop()
190
191 if _, err := backend2.Client.EmptyCall(ctx, &testpb.Empty{}); err != nil {
192 t.Fatalf("RPC failed with error: %v", err)
193 }
194}
195
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.

Callers

nothing calls this directly

Calls 10

StartMethod · 0.95
StopMethod · 0.95
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