Tests the case where ForceServerCodec option is used on the server. Verifies that encoding and decoding happen once per RPC.
(t *testing.T)
| 292 | // Tests the case where ForceServerCodec option is used on the server. Verifies |
| 293 | // that encoding and decoding happen once per RPC. |
| 294 | func (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(). |
| 329 | type renameProtoCodec struct { |
nothing calls this directly
no test coverage detected