OSDN Git Service

new repo
[bytom/vapor.git] / vendor / google.golang.org / grpc / codec_test.go
1 /*
2  *
3  * Copyright 2014 gRPC authors.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18
19 package grpc
20
21 import (
22         "bytes"
23         "sync"
24         "testing"
25
26         "google.golang.org/grpc/test/codec_perf"
27 )
28
29 func marshalAndUnmarshal(t *testing.T, protoCodec Codec, expectedBody []byte) {
30         p := &codec_perf.Buffer{}
31         p.Body = expectedBody
32
33         marshalledBytes, err := protoCodec.Marshal(p)
34         if err != nil {
35                 t.Errorf("protoCodec.Marshal(_) returned an error")
36         }
37
38         if err := protoCodec.Unmarshal(marshalledBytes, p); err != nil {
39                 t.Errorf("protoCodec.Unmarshal(_) returned an error")
40         }
41
42         if bytes.Compare(p.GetBody(), expectedBody) != 0 {
43                 t.Errorf("Unexpected body; got %v; want %v", p.GetBody(), expectedBody)
44         }
45 }
46
47 func TestBasicProtoCodecMarshalAndUnmarshal(t *testing.T) {
48         marshalAndUnmarshal(t, protoCodec{}, []byte{1, 2, 3})
49 }
50
51 // Try to catch possible race conditions around use of pools
52 func TestConcurrentUsage(t *testing.T) {
53         const (
54                 numGoRoutines   = 100
55                 numMarshUnmarsh = 1000
56         )
57
58         // small, arbitrary byte slices
59         protoBodies := [][]byte{
60                 []byte("one"),
61                 []byte("two"),
62                 []byte("three"),
63                 []byte("four"),
64                 []byte("five"),
65         }
66
67         var wg sync.WaitGroup
68         codec := protoCodec{}
69
70         for i := 0; i < numGoRoutines; i++ {
71                 wg.Add(1)
72                 go func() {
73                         defer wg.Done()
74                         for k := 0; k < numMarshUnmarsh; k++ {
75                                 marshalAndUnmarshal(t, codec, protoBodies[k%len(protoBodies)])
76                         }
77                 }()
78         }
79
80         wg.Wait()
81 }
82
83 // TestStaggeredMarshalAndUnmarshalUsingSamePool tries to catch potential errors in which slices get
84 // stomped on during reuse of a proto.Buffer.
85 func TestStaggeredMarshalAndUnmarshalUsingSamePool(t *testing.T) {
86         codec1 := protoCodec{}
87         codec2 := protoCodec{}
88
89         expectedBody1 := []byte{1, 2, 3}
90         expectedBody2 := []byte{4, 5, 6}
91
92         proto1 := codec_perf.Buffer{Body: expectedBody1}
93         proto2 := codec_perf.Buffer{Body: expectedBody2}
94
95         var m1, m2 []byte
96         var err error
97
98         if m1, err = codec1.Marshal(&proto1); err != nil {
99                 t.Errorf("protoCodec.Marshal(%v) failed", proto1)
100         }
101
102         if m2, err = codec2.Marshal(&proto2); err != nil {
103                 t.Errorf("protoCodec.Marshal(%v) failed", proto2)
104         }
105
106         if err = codec1.Unmarshal(m1, &proto1); err != nil {
107                 t.Errorf("protoCodec.Unmarshal(%v) failed", m1)
108         }
109
110         if err = codec2.Unmarshal(m2, &proto2); err != nil {
111                 t.Errorf("protoCodec.Unmarshal(%v) failed", m2)
112         }
113
114         b1 := proto1.GetBody()
115         b2 := proto2.GetBody()
116
117         for i, v := range b1 {
118                 if expectedBody1[i] != v {
119                         t.Errorf("expected %v at index %v but got %v", i, expectedBody1[i], v)
120                 }
121         }
122
123         for i, v := range b2 {
124                 if expectedBody2[i] != v {
125                         t.Errorf("expected %v at index %v but got %v", i, expectedBody2[i], v)
126                 }
127         }
128 }