github.com/stackdocker/rkt@v0.10.1-0.20151109095037-1aa827478248/Godeps/_workspace/src/google.golang.org/grpc/rpc_util_test.go (about) 1 /* 2 * 3 * Copyright 2014, Google Inc. 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions are 8 * met: 9 * 10 * * Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * * Redistributions in binary form must reproduce the above 13 * copyright notice, this list of conditions and the following disclaimer 14 * in the documentation and/or other materials provided with the 15 * distribution. 16 * * Neither the name of Google Inc. nor the names of its 17 * contributors may be used to endorse or promote products derived from 18 * this software without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 * 32 */ 33 34 package grpc 35 36 import ( 37 "bytes" 38 "io" 39 "reflect" 40 "testing" 41 42 "github.com/coreos/rkt/Godeps/_workspace/src/github.com/golang/protobuf/proto" 43 "github.com/coreos/rkt/Godeps/_workspace/src/golang.org/x/net/context" 44 "github.com/coreos/rkt/Godeps/_workspace/src/google.golang.org/grpc/codes" 45 perfpb "github.com/coreos/rkt/Godeps/_workspace/src/google.golang.org/grpc/test/codec_perf" 46 "github.com/coreos/rkt/Godeps/_workspace/src/google.golang.org/grpc/transport" 47 ) 48 49 func TestSimpleParsing(t *testing.T) { 50 for _, test := range []struct { 51 // input 52 p []byte 53 // outputs 54 err error 55 b []byte 56 pt payloadFormat 57 }{ 58 {nil, io.EOF, nil, compressionNone}, 59 {[]byte{0, 0, 0, 0, 0}, nil, nil, compressionNone}, 60 {[]byte{0, 0, 0, 0, 1, 'a'}, nil, []byte{'a'}, compressionNone}, 61 {[]byte{1, 0}, io.ErrUnexpectedEOF, nil, compressionNone}, 62 {[]byte{0, 0, 0, 0, 10, 'a'}, io.ErrUnexpectedEOF, nil, compressionNone}, 63 } { 64 buf := bytes.NewReader(test.p) 65 parser := &parser{buf} 66 pt, b, err := parser.recvMsg() 67 if err != test.err || !bytes.Equal(b, test.b) || pt != test.pt { 68 t.Fatalf("parser{%v}.recvMsg() = %v, %v, %v\nwant %v, %v, %v", test.p, pt, b, err, test.pt, test.b, test.err) 69 } 70 } 71 } 72 73 func TestMultipleParsing(t *testing.T) { 74 // Set a byte stream consists of 3 messages with their headers. 75 p := []byte{0, 0, 0, 0, 1, 'a', 0, 0, 0, 0, 2, 'b', 'c', 0, 0, 0, 0, 1, 'd'} 76 b := bytes.NewReader(p) 77 parser := &parser{b} 78 79 wantRecvs := []struct { 80 pt payloadFormat 81 data []byte 82 }{ 83 {compressionNone, []byte("a")}, 84 {compressionNone, []byte("bc")}, 85 {compressionNone, []byte("d")}, 86 } 87 for i, want := range wantRecvs { 88 pt, data, err := parser.recvMsg() 89 if err != nil || pt != want.pt || !reflect.DeepEqual(data, want.data) { 90 t.Fatalf("after %d calls, parser{%v}.recvMsg() = %v, %v, %v\nwant %v, %v, <nil>", 91 i, p, pt, data, err, want.pt, want.data) 92 } 93 } 94 95 pt, data, err := parser.recvMsg() 96 if err != io.EOF { 97 t.Fatalf("after %d recvMsgs calls, parser{%v}.recvMsg() = %v, %v, %v\nwant _, _, %v", 98 len(wantRecvs), p, pt, data, err, io.EOF) 99 } 100 } 101 102 func TestEncode(t *testing.T) { 103 for _, test := range []struct { 104 // input 105 msg proto.Message 106 pt payloadFormat 107 // outputs 108 b []byte 109 err error 110 }{ 111 {nil, compressionNone, []byte{0, 0, 0, 0, 0}, nil}, 112 } { 113 b, err := encode(protoCodec{}, test.msg, test.pt) 114 if err != test.err || !bytes.Equal(b, test.b) { 115 t.Fatalf("encode(_, _, %d) = %v, %v\nwant %v, %v", test.pt, b, err, test.b, test.err) 116 } 117 } 118 } 119 120 func TestToRPCErr(t *testing.T) { 121 for _, test := range []struct { 122 // input 123 errIn error 124 // outputs 125 errOut error 126 }{ 127 {transport.StreamErrorf(codes.Unknown, ""), Errorf(codes.Unknown, "")}, 128 {transport.ErrConnClosing, Errorf(codes.Internal, transport.ErrConnClosing.Desc)}, 129 } { 130 err := toRPCErr(test.errIn) 131 if err != test.errOut { 132 t.Fatalf("toRPCErr{%v} = %v \nwant %v", test.errIn, err, test.errOut) 133 } 134 } 135 } 136 137 func TestContextErr(t *testing.T) { 138 for _, test := range []struct { 139 // input 140 errIn error 141 // outputs 142 errOut transport.StreamError 143 }{ 144 {context.DeadlineExceeded, transport.StreamErrorf(codes.DeadlineExceeded, "%v", context.DeadlineExceeded)}, 145 {context.Canceled, transport.StreamErrorf(codes.Canceled, "%v", context.Canceled)}, 146 } { 147 err := transport.ContextErr(test.errIn) 148 if err != test.errOut { 149 t.Fatalf("ContextErr{%v} = %v \nwant %v", test.errIn, err, test.errOut) 150 } 151 } 152 } 153 154 // bmEncode benchmarks encoding a Protocol Buffer message containing mSize 155 // bytes. 156 func bmEncode(b *testing.B, mSize int) { 157 msg := &perfpb.Buffer{Body: make([]byte, mSize)} 158 encoded, _ := encode(protoCodec{}, msg, compressionNone) 159 encodedSz := int64(len(encoded)) 160 b.ReportAllocs() 161 b.ResetTimer() 162 for i := 0; i < b.N; i++ { 163 encode(protoCodec{}, msg, compressionNone) 164 } 165 b.SetBytes(encodedSz) 166 } 167 168 func BenchmarkEncode1B(b *testing.B) { 169 bmEncode(b, 1) 170 } 171 172 func BenchmarkEncode1KiB(b *testing.B) { 173 bmEncode(b, 1024) 174 } 175 176 func BenchmarkEncode8KiB(b *testing.B) { 177 bmEncode(b, 8*1024) 178 } 179 180 func BenchmarkEncode64KiB(b *testing.B) { 181 bmEncode(b, 64*1024) 182 } 183 184 func BenchmarkEncode512KiB(b *testing.B) { 185 bmEncode(b, 512*1024) 186 } 187 188 func BenchmarkEncode1MiB(b *testing.B) { 189 bmEncode(b, 1024*1024) 190 }