interop: Fix unimplemented method test (#2040)
* Don't send nil requests. * Fix import name and get rid of condition. * Let registered encoder deal with nil requests. * Break encode into encode and compress.
This commit is contained in:
76
rpc_util.go
76
rpc_util.go
@@ -435,45 +435,49 @@ func recvMsg(s *transport.Stream, maxRecvMsgSize int) (bool, []byte, error) {
|
||||
}
|
||||
|
||||
// encode serializes msg and returns a buffer of msg.
|
||||
// If msg is nil, it generates an empty buffer.
|
||||
// TODO(ddyihai): eliminate extra Compressor parameter.
|
||||
func encode(c baseCodec, msg interface{}, cp Compressor, outPayload *stats.OutPayload, compressor encoding.Compressor) ([]byte, error) {
|
||||
var (
|
||||
b []byte
|
||||
cbuf *bytes.Buffer
|
||||
)
|
||||
if msg != nil {
|
||||
var err error
|
||||
b, err = c.Marshal(msg)
|
||||
if err != nil {
|
||||
return nil, status.Errorf(codes.Internal, "grpc: error while marshaling: %v", err.Error())
|
||||
func encode(c baseCodec, msg interface{}, outPayload *stats.OutPayload) ([]byte, error) {
|
||||
b, err := c.Marshal(msg)
|
||||
if err != nil {
|
||||
return nil, status.Errorf(codes.Internal, "grpc: error while marshaling: %v", err.Error())
|
||||
}
|
||||
if b == nil {
|
||||
// If there was no error while marshalling, yet payload was nil,
|
||||
// we update it to an empty slice, since a nil payload leads to
|
||||
// an empty data frame(no gRPC message header is added).
|
||||
b = []byte{}
|
||||
}
|
||||
if outPayload != nil {
|
||||
outPayload.Payload = msg
|
||||
// TODO truncate large payload.
|
||||
outPayload.Data = b
|
||||
outPayload.Length = len(b)
|
||||
}
|
||||
return b, nil
|
||||
}
|
||||
|
||||
// compress the message if there is a compressor registered.
|
||||
// TODO(mmukhi, dfawley): eliminate extra Compressor parameter.
|
||||
func compress(b []byte, cp Compressor, compressor encoding.Compressor, outPayload *stats.OutPayload) ([]byte, bool, error) {
|
||||
if len(b) <= 0 || (compressor == nil && cp == nil) {
|
||||
return b, false, nil
|
||||
}
|
||||
cbuf := new(bytes.Buffer)
|
||||
// Has compressor, check Compressor is set by UseCompressor first.
|
||||
if compressor != nil {
|
||||
z, _ := compressor.Compress(cbuf)
|
||||
if _, err := z.Write(b); err != nil {
|
||||
return nil, false, status.Errorf(codes.Internal, "grpc: error while compressing: %v", err.Error())
|
||||
}
|
||||
if outPayload != nil {
|
||||
outPayload.Payload = msg
|
||||
// TODO truncate large payload.
|
||||
outPayload.Data = b
|
||||
outPayload.Length = len(b)
|
||||
}
|
||||
if compressor != nil || cp != nil {
|
||||
cbuf = new(bytes.Buffer)
|
||||
// Has compressor, check Compressor is set by UseCompressor first.
|
||||
if compressor != nil {
|
||||
z, _ := compressor.Compress(cbuf)
|
||||
if _, err := z.Write(b); err != nil {
|
||||
return nil, status.Errorf(codes.Internal, "grpc: error while compressing: %v", err.Error())
|
||||
}
|
||||
z.Close()
|
||||
} else {
|
||||
// If Compressor is not set by UseCompressor, use default Compressor
|
||||
if err := cp.Do(cbuf, b); err != nil {
|
||||
return nil, status.Errorf(codes.Internal, "grpc: error while compressing: %v", err.Error())
|
||||
}
|
||||
}
|
||||
b = cbuf.Bytes()
|
||||
z.Close()
|
||||
} else {
|
||||
// If Compressor is not set by UseCompressor, use default Compressor
|
||||
if err := cp.Do(cbuf, b); err != nil {
|
||||
return nil, false, status.Errorf(codes.Internal, "grpc: error while compressing: %v", err.Error())
|
||||
}
|
||||
}
|
||||
b = cbuf.Bytes()
|
||||
if uint(len(b)) > math.MaxUint32 {
|
||||
return nil, status.Errorf(codes.ResourceExhausted, "grpc: message too large (%d bytes)", len(b))
|
||||
return nil, false, status.Errorf(codes.ResourceExhausted, "grpc: message too large (%d bytes)", len(b))
|
||||
}
|
||||
|
||||
if outPayload != nil {
|
||||
@@ -481,7 +485,7 @@ func encode(c baseCodec, msg interface{}, cp Compressor, outPayload *stats.OutPa
|
||||
// before it's put on wire.
|
||||
outPayload.WireLength = 5 + len(b)
|
||||
}
|
||||
return b, nil
|
||||
return b, true, nil
|
||||
}
|
||||
|
||||
func checkRecvPayload(recvCompress string, haveCompressor bool) *status.Status {
|
||||
|
||||
Reference in New Issue
Block a user