2015-02-06 04:14:05 +03:00
|
|
|
/*
|
|
|
|
*
|
2017-06-08 15:42:19 +03:00
|
|
|
* Copyright 2014 gRPC authors.
|
2015-02-06 04:14:05 +03:00
|
|
|
*
|
2017-06-08 15:42:19 +03:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
2015-02-06 04:14:05 +03:00
|
|
|
*
|
2017-06-08 15:42:19 +03:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2015-02-06 04:14:05 +03:00
|
|
|
*
|
2017-06-08 15:42:19 +03:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
2015-02-06 04:14:05 +03:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2015-02-09 03:35:38 +03:00
|
|
|
package grpc
|
2015-02-06 04:14:05 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"io"
|
2016-07-30 02:19:20 +03:00
|
|
|
"math"
|
2015-02-06 04:14:05 +03:00
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/golang/protobuf/proto"
|
2015-02-09 03:39:06 +03:00
|
|
|
"google.golang.org/grpc/codes"
|
2017-04-05 20:35:40 +03:00
|
|
|
"google.golang.org/grpc/status"
|
2015-02-28 20:46:39 +03:00
|
|
|
perfpb "google.golang.org/grpc/test/codec_perf"
|
2015-02-09 03:39:06 +03:00
|
|
|
"google.golang.org/grpc/transport"
|
2015-02-06 04:14:05 +03:00
|
|
|
)
|
|
|
|
|
2017-05-23 21:39:15 +03:00
|
|
|
type fullReader struct {
|
|
|
|
reader io.Reader
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f fullReader) Read(p []byte) (int, error) {
|
|
|
|
return io.ReadFull(f.reader, p)
|
|
|
|
}
|
|
|
|
|
2017-05-18 00:16:19 +03:00
|
|
|
var _ CallOption = EmptyCallOption{} // ensure EmptyCallOption implements the interface
|
|
|
|
|
2015-02-06 04:14:05 +03:00
|
|
|
func TestSimpleParsing(t *testing.T) {
|
2015-10-15 10:43:49 +03:00
|
|
|
bigMsg := bytes.Repeat([]byte{'x'}, 1<<24)
|
2015-02-06 04:14:05 +03:00
|
|
|
for _, test := range []struct {
|
|
|
|
// input
|
|
|
|
p []byte
|
|
|
|
// outputs
|
|
|
|
err error
|
|
|
|
b []byte
|
|
|
|
pt payloadFormat
|
|
|
|
}{
|
|
|
|
{nil, io.EOF, nil, compressionNone},
|
|
|
|
{[]byte{0, 0, 0, 0, 0}, nil, nil, compressionNone},
|
|
|
|
{[]byte{0, 0, 0, 0, 1, 'a'}, nil, []byte{'a'}, compressionNone},
|
|
|
|
{[]byte{1, 0}, io.ErrUnexpectedEOF, nil, compressionNone},
|
|
|
|
{[]byte{0, 0, 0, 0, 10, 'a'}, io.ErrUnexpectedEOF, nil, compressionNone},
|
2015-10-15 10:43:49 +03:00
|
|
|
// Check that messages with length >= 2^24 are parsed.
|
|
|
|
{append([]byte{0, 1, 0, 0, 0}, bigMsg...), nil, bigMsg, compressionNone},
|
2015-02-06 04:14:05 +03:00
|
|
|
} {
|
2017-05-23 21:39:15 +03:00
|
|
|
buf := fullReader{bytes.NewReader(test.p)}
|
2016-02-24 19:32:08 +03:00
|
|
|
parser := &parser{r: buf}
|
2016-07-30 02:19:20 +03:00
|
|
|
pt, b, err := parser.recvMsg(math.MaxInt32)
|
2015-02-06 04:14:05 +03:00
|
|
|
if err != test.err || !bytes.Equal(b, test.b) || pt != test.pt {
|
2016-07-30 02:19:20 +03:00
|
|
|
t.Fatalf("parser{%v}.recvMsg(_) = %v, %v, %v\nwant %v, %v, %v", test.p, pt, b, err, test.pt, test.b, test.err)
|
2015-02-06 04:14:05 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMultipleParsing(t *testing.T) {
|
|
|
|
// Set a byte stream consists of 3 messages with their headers.
|
|
|
|
p := []byte{0, 0, 0, 0, 1, 'a', 0, 0, 0, 0, 2, 'b', 'c', 0, 0, 0, 0, 1, 'd'}
|
2017-05-23 21:39:15 +03:00
|
|
|
b := fullReader{bytes.NewReader(p)}
|
2016-02-24 19:32:08 +03:00
|
|
|
parser := &parser{r: b}
|
2015-02-06 04:14:05 +03:00
|
|
|
|
|
|
|
wantRecvs := []struct {
|
|
|
|
pt payloadFormat
|
|
|
|
data []byte
|
|
|
|
}{
|
|
|
|
{compressionNone, []byte("a")},
|
|
|
|
{compressionNone, []byte("bc")},
|
|
|
|
{compressionNone, []byte("d")},
|
|
|
|
}
|
|
|
|
for i, want := range wantRecvs {
|
2016-07-30 02:19:20 +03:00
|
|
|
pt, data, err := parser.recvMsg(math.MaxInt32)
|
2015-02-06 04:14:05 +03:00
|
|
|
if err != nil || pt != want.pt || !reflect.DeepEqual(data, want.data) {
|
2016-07-30 02:19:20 +03:00
|
|
|
t.Fatalf("after %d calls, parser{%v}.recvMsg(_) = %v, %v, %v\nwant %v, %v, <nil>",
|
2015-02-06 04:14:05 +03:00
|
|
|
i, p, pt, data, err, want.pt, want.data)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-30 02:19:20 +03:00
|
|
|
pt, data, err := parser.recvMsg(math.MaxInt32)
|
2015-02-06 04:14:05 +03:00
|
|
|
if err != io.EOF {
|
2016-07-30 02:19:20 +03:00
|
|
|
t.Fatalf("after %d recvMsgs calls, parser{%v}.recvMsg(_) = %v, %v, %v\nwant _, _, %v",
|
2015-02-06 04:14:05 +03:00
|
|
|
len(wantRecvs), p, pt, data, err, io.EOF)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEncode(t *testing.T) {
|
|
|
|
for _, test := range []struct {
|
|
|
|
// input
|
|
|
|
msg proto.Message
|
2016-01-23 05:21:41 +03:00
|
|
|
cp Compressor
|
2015-02-06 04:14:05 +03:00
|
|
|
// outputs
|
|
|
|
b []byte
|
|
|
|
err error
|
|
|
|
}{
|
2016-01-23 05:21:41 +03:00
|
|
|
{nil, nil, []byte{0, 0, 0, 0, 0}, nil},
|
2015-02-06 04:14:05 +03:00
|
|
|
} {
|
2016-10-23 01:06:41 +03:00
|
|
|
b, err := encode(protoCodec{}, test.msg, nil, nil, nil)
|
2015-02-06 04:14:05 +03:00
|
|
|
if err != test.err || !bytes.Equal(b, test.b) {
|
2016-01-23 05:21:41 +03:00
|
|
|
t.Fatalf("encode(_, _, %v, _) = %v, %v\nwant %v, %v", test.cp, b, err, test.b, test.err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCompress(t *testing.T) {
|
|
|
|
for _, test := range []struct {
|
|
|
|
// input
|
|
|
|
data []byte
|
|
|
|
cp Compressor
|
|
|
|
dc Decompressor
|
|
|
|
// outputs
|
|
|
|
err error
|
|
|
|
}{
|
2017-05-11 02:40:25 +03:00
|
|
|
{make([]byte, 1024), NewGZIPCompressor(), NewGZIPDecompressor(), nil},
|
2016-01-23 05:21:41 +03:00
|
|
|
} {
|
|
|
|
b := new(bytes.Buffer)
|
|
|
|
if err := test.cp.Do(b, test.data); err != test.err {
|
|
|
|
t.Fatalf("Compressor.Do(_, %v) = %v, want %v", test.data, err, test.err)
|
|
|
|
}
|
|
|
|
if b.Len() >= len(test.data) {
|
|
|
|
t.Fatalf("The compressor fails to compress data.")
|
|
|
|
}
|
|
|
|
if p, err := test.dc.Do(b); err != nil || !bytes.Equal(test.data, p) {
|
|
|
|
t.Fatalf("Decompressor.Do(%v) = %v, %v, want %v, <nil>", b, p, err, test.data)
|
2015-02-06 04:14:05 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestToRPCErr(t *testing.T) {
|
|
|
|
for _, test := range []struct {
|
|
|
|
// input
|
|
|
|
errIn error
|
|
|
|
// outputs
|
2017-04-05 20:35:40 +03:00
|
|
|
errOut error
|
2015-02-06 04:14:05 +03:00
|
|
|
}{
|
2017-04-05 20:35:40 +03:00
|
|
|
{transport.StreamError{Code: codes.Unknown, Desc: ""}, status.Error(codes.Unknown, "")},
|
2017-06-15 00:21:20 +03:00
|
|
|
{transport.ErrConnClosing, status.Error(codes.Unavailable, transport.ErrConnClosing.Desc)},
|
2015-02-06 04:14:05 +03:00
|
|
|
} {
|
|
|
|
err := toRPCErr(test.errIn)
|
2017-04-06 21:41:07 +03:00
|
|
|
if _, ok := status.FromError(err); !ok {
|
2017-04-05 20:35:40 +03:00
|
|
|
t.Fatalf("toRPCErr{%v} returned type %T, want %T", test.errIn, err, status.Error(codes.Unknown, ""))
|
2016-05-25 23:56:09 +03:00
|
|
|
}
|
2017-04-05 20:35:40 +03:00
|
|
|
if !reflect.DeepEqual(err, test.errOut) {
|
2015-02-06 04:14:05 +03:00
|
|
|
t.Fatalf("toRPCErr{%v} = %v \nwant %v", test.errIn, err, test.errOut)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-28 20:46:39 +03:00
|
|
|
// bmEncode benchmarks encoding a Protocol Buffer message containing mSize
|
|
|
|
// bytes.
|
|
|
|
func bmEncode(b *testing.B, mSize int) {
|
|
|
|
msg := &perfpb.Buffer{Body: make([]byte, mSize)}
|
2016-10-23 01:06:41 +03:00
|
|
|
encoded, _ := encode(protoCodec{}, msg, nil, nil, nil)
|
2015-02-28 20:46:39 +03:00
|
|
|
encodedSz := int64(len(encoded))
|
|
|
|
b.ReportAllocs()
|
|
|
|
b.ResetTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
2016-10-23 01:06:41 +03:00
|
|
|
encode(protoCodec{}, msg, nil, nil, nil)
|
2015-02-28 20:46:39 +03:00
|
|
|
}
|
|
|
|
b.SetBytes(encodedSz)
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkEncode1B(b *testing.B) {
|
|
|
|
bmEncode(b, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkEncode1KiB(b *testing.B) {
|
|
|
|
bmEncode(b, 1024)
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkEncode8KiB(b *testing.B) {
|
|
|
|
bmEncode(b, 8*1024)
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkEncode64KiB(b *testing.B) {
|
|
|
|
bmEncode(b, 64*1024)
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkEncode512KiB(b *testing.B) {
|
|
|
|
bmEncode(b, 512*1024)
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkEncode1MiB(b *testing.B) {
|
|
|
|
bmEncode(b, 1024*1024)
|
|
|
|
}
|
2017-05-11 02:40:25 +03:00
|
|
|
|
|
|
|
// bmCompressor benchmarks a compressor of a Protocol Buffer message containing
|
|
|
|
// mSize bytes.
|
|
|
|
func bmCompressor(b *testing.B, mSize int, cp Compressor) {
|
|
|
|
payload := make([]byte, mSize)
|
|
|
|
cBuf := bytes.NewBuffer(make([]byte, mSize))
|
|
|
|
b.ReportAllocs()
|
|
|
|
b.ResetTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
cp.Do(cBuf, payload)
|
|
|
|
cBuf.Reset()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkGZIPCompressor1B(b *testing.B) {
|
|
|
|
bmCompressor(b, 1, NewGZIPCompressor())
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkGZIPCompressor1KiB(b *testing.B) {
|
|
|
|
bmCompressor(b, 1024, NewGZIPCompressor())
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkGZIPCompressor8KiB(b *testing.B) {
|
|
|
|
bmCompressor(b, 8*1024, NewGZIPCompressor())
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkGZIPCompressor64KiB(b *testing.B) {
|
|
|
|
bmCompressor(b, 64*1024, NewGZIPCompressor())
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkGZIPCompressor512KiB(b *testing.B) {
|
|
|
|
bmCompressor(b, 512*1024, NewGZIPCompressor())
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkGZIPCompressor1MiB(b *testing.B) {
|
|
|
|
bmCompressor(b, 1024*1024, NewGZIPCompressor())
|
|
|
|
}
|