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

Method TestForceServerCodec

encoding/encoding_test.go:294–326  ·  view source on GitHub ↗

Tests the case where ForceServerCodec option is used on the server. Verifies that encoding and decoding happen once per RPC.

(t *testing.T)

Source from the content-addressed store, hash-verified

292// Tests the case where ForceServerCodec option is used on the server. Verifies
293// that encoding and decoding happen once per RPC.
294func (s) TestForceServerCodec(t *testing.T) {
295 // Create a server with the counting proto codec.
296 codec := &countingProtoCodec{name: t.Name()}
297 backend := stubserver.StartTestService(t, nil, grpc.ForceServerCodecV2(codec))
298 defer backend.Stop()
299
300 // Create a channel to the above server.
301 cc, err := grpc.NewClient(backend.Address, grpc.WithTransportCredentials(insecure.NewCredentials()))
302 if err != nil {
303 t.Fatalf("Failed to dial test backend at %q: %v", backend.Address, err)
304 }
305 defer cc.Close()
306
307 // Make an RPC and expect it to fail. Since we do not specify any codec
308 // here, the proto codec will get automatically used.
309 ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
310 defer cancel()
311 client := testgrpc.NewTestServiceClient(cc)
312 if _, err := client.EmptyCall(ctx, &testpb.Empty{}); err != nil {
313 t.Fatalf("ss.Client.EmptyCall(_, _) = _, %v; want _, nil", err)
314 }
315
316 unmarshalCount := atomic.LoadInt32(&codec.unmarshalCount)
317 const wantUnmarshalCount = 1
318 if unmarshalCount != wantUnmarshalCount {
319 t.Fatalf("Unmarshal Count = %d; want %d", unmarshalCount, wantUnmarshalCount)
320 }
321 marshalCount := atomic.LoadInt32(&codec.marshalCount)
322 const wantMarshalCount = 1
323 if marshalCount != wantMarshalCount {
324 t.Fatalf("MarshalCount = %d; want %d", marshalCount, wantMarshalCount)
325 }
326}
327
328// renameProtoCodec wraps the proto codec and allows customizing the Name().
329type renameProtoCodec struct {

Callers

nothing calls this directly

Calls 10

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

Tested by

no test coverage detected