@@ -454,6 +454,7 @@ type test struct {
454454 clientInitialWindowSize int32
455455 clientInitialConnWindowSize int32
456456 perRPCCreds credentials.PerRPCCredentials
457+ customCodec grpc.Codec
457458
458459 // srv and srvAddr are set once startServer is called.
459460 srv * grpc.Server
@@ -4795,3 +4796,46 @@ func testPerRPCCredentialsViaDialOptionsAndCallOptions(t *testing.T, e env) {
47954796 t .Fatalf ("Test failed. Reason: %v" , err )
47964797 }
47974798}
4799+
4800+ type errCodec struct {
4801+ noError bool
4802+ }
4803+
4804+ func (c * errCodec ) Marshal (v interface {}) ([]byte , error ) {
4805+ if c .noError {
4806+ return []byte {}, nil
4807+ }
4808+ return nil , fmt .Errorf ("3987^12 + 4365^12 = 4472^12" )
4809+ }
4810+
4811+ func (c * errCodec ) Unmarshal (data []byte , v interface {}) error {
4812+ return nil
4813+ }
4814+
4815+ func (c * errCodec ) String () string {
4816+ return "Fermat's near-miss."
4817+ }
4818+
4819+ func TestEncodeDoesntPanic (t * testing.T ) {
4820+ defer leakCheck (t )()
4821+ for _ , e := range listTestEnv () {
4822+ testEncodeDoesntPanic (t , e )
4823+ }
4824+ }
4825+
4826+ func testEncodeDoesntPanic (t * testing.T , e env ) {
4827+ te := newTest (t , e )
4828+ erc := & errCodec {}
4829+ te .customCodec = erc
4830+ te .startServer (& testServer {security : e .security })
4831+ defer te .tearDown ()
4832+ te .customCodec = nil
4833+ tc := testpb .NewTestServiceClient (te .clientConn ())
4834+ // Failure case, should not panic.
4835+ tc .EmptyCall (context .Background (), & testpb.Empty {})
4836+ erc .noError = true
4837+ // Passing case.
4838+ if _ , err := tc .EmptyCall (context .Background (), & testpb.Empty {}); err != nil {
4839+ t .Fatalf ("EmptyCall(_, _) = _, %v, want _, <nil>" , err )
4840+ }
4841+ }
0 commit comments