OSDN Git Service

new repo
[bytom/vapor.git] / vendor / google.golang.org / grpc / service_config_test.go
1 /*
2  *
3  * Copyright 2017 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         "reflect"
23         "testing"
24         "time"
25 )
26
27 func TestParseLoadBalancer(t *testing.T) {
28         testcases := []struct {
29                 scjs    string
30                 wantSC  ServiceConfig
31                 wantErr bool
32         }{
33                 {
34                         `{
35     "loadBalancingPolicy": "round_robin",
36     "methodConfig": [
37         {
38             "name": [
39                 {
40                     "service": "foo",
41                     "method": "Bar"
42                 }
43             ],
44             "waitForReady": true
45         }
46     ]
47 }`,
48                         ServiceConfig{
49                                 LB: newString("round_robin"),
50                                 Methods: map[string]MethodConfig{
51                                         "/foo/Bar": {
52                                                 WaitForReady: newBool(true),
53                                         },
54                                 },
55                         },
56                         false,
57                 },
58                 {
59                         `{
60     "loadBalancingPolicy": 1,
61     "methodConfig": [
62         {
63             "name": [
64                 {
65                     "service": "foo",
66                     "method": "Bar"
67                 }
68             ],
69             "waitForReady": false
70         }
71     ]
72 }`,
73                         ServiceConfig{},
74                         true,
75                 },
76         }
77
78         for _, c := range testcases {
79                 sc, err := parseServiceConfig(c.scjs)
80                 if c.wantErr != (err != nil) || !reflect.DeepEqual(sc, c.wantSC) {
81                         t.Fatalf("parseServiceConfig(%s) = %+v, %v, want %+v, %v", c.scjs, sc, err, c.wantSC, c.wantErr)
82                 }
83         }
84 }
85
86 func TestPraseWaitForReady(t *testing.T) {
87         testcases := []struct {
88                 scjs    string
89                 wantSC  ServiceConfig
90                 wantErr bool
91         }{
92                 {
93                         `{
94     "methodConfig": [
95         {
96             "name": [
97                 {
98                     "service": "foo",
99                     "method": "Bar"
100                 }
101             ],
102             "waitForReady": true
103         }
104     ]
105 }`,
106                         ServiceConfig{
107                                 Methods: map[string]MethodConfig{
108                                         "/foo/Bar": {
109                                                 WaitForReady: newBool(true),
110                                         },
111                                 },
112                         },
113                         false,
114                 },
115                 {
116                         `{
117     "methodConfig": [
118         {
119             "name": [
120                 {
121                     "service": "foo",
122                     "method": "Bar"
123                 }
124             ],
125             "waitForReady": false
126         }
127     ]
128 }`,
129                         ServiceConfig{
130                                 Methods: map[string]MethodConfig{
131                                         "/foo/Bar": {
132                                                 WaitForReady: newBool(false),
133                                         },
134                                 },
135                         },
136                         false,
137                 },
138                 {
139                         `{
140     "methodConfig": [
141         {
142             "name": [
143                 {
144                     "service": "foo",
145                     "method": "Bar"
146                 }
147             ],
148             "waitForReady": fall
149         },
150         {
151             "name": [
152                 {
153                     "service": "foo",
154                     "method": "Bar"
155                 }
156             ],
157             "waitForReady": true
158         }
159     ]
160 }`,
161                         ServiceConfig{},
162                         true,
163                 },
164         }
165
166         for _, c := range testcases {
167                 sc, err := parseServiceConfig(c.scjs)
168                 if c.wantErr != (err != nil) || !reflect.DeepEqual(sc, c.wantSC) {
169                         t.Fatalf("parseServiceConfig(%s) = %+v, %v, want %+v, %v", c.scjs, sc, err, c.wantSC, c.wantErr)
170                 }
171         }
172 }
173
174 func TestPraseTimeOut(t *testing.T) {
175         testcases := []struct {
176                 scjs    string
177                 wantSC  ServiceConfig
178                 wantErr bool
179         }{
180                 {
181                         `{
182     "methodConfig": [
183         {
184             "name": [
185                 {
186                     "service": "foo",
187                     "method": "Bar"
188                 }
189             ],
190             "timeout": "1s"
191         }
192     ]
193 }`,
194                         ServiceConfig{
195                                 Methods: map[string]MethodConfig{
196                                         "/foo/Bar": {
197                                                 Timeout: newDuration(time.Second),
198                                         },
199                                 },
200                         },
201                         false,
202                 },
203                 {
204                         `{
205     "methodConfig": [
206         {
207             "name": [
208                 {
209                     "service": "foo",
210                     "method": "Bar"
211                 }
212             ],
213             "timeout": "3c"
214         }
215     ]
216 }`,
217                         ServiceConfig{},
218                         true,
219                 },
220                 {
221                         `{
222     "methodConfig": [
223         {
224             "name": [
225                 {
226                     "service": "foo",
227                     "method": "Bar"
228                 }
229             ],
230             "timeout": "3c"
231         },
232         {
233             "name": [
234                 {
235                     "service": "foo",
236                     "method": "Bar"
237                 }
238             ],
239             "timeout": "1s"
240         }
241     ]
242 }`,
243                         ServiceConfig{},
244                         true,
245                 },
246         }
247
248         for _, c := range testcases {
249                 sc, err := parseServiceConfig(c.scjs)
250                 if c.wantErr != (err != nil) || !reflect.DeepEqual(sc, c.wantSC) {
251                         t.Fatalf("parseServiceConfig(%s) = %+v, %v, want %+v, %v", c.scjs, sc, err, c.wantSC, c.wantErr)
252                 }
253         }
254 }
255
256 func TestPraseMsgSize(t *testing.T) {
257         testcases := []struct {
258                 scjs    string
259                 wantSC  ServiceConfig
260                 wantErr bool
261         }{
262                 {
263                         `{
264     "methodConfig": [
265         {
266             "name": [
267                 {
268                     "service": "foo",
269                     "method": "Bar"
270                 }
271             ],
272             "maxRequestMessageBytes": 1024,
273             "maxResponseMessageBytes": 2048
274         }
275     ]
276 }`,
277                         ServiceConfig{
278                                 Methods: map[string]MethodConfig{
279                                         "/foo/Bar": {
280                                                 MaxReqSize:  newInt(1024),
281                                                 MaxRespSize: newInt(2048),
282                                         },
283                                 },
284                         },
285                         false,
286                 },
287                 {
288                         `{
289     "methodConfig": [
290         {
291             "name": [
292                 {
293                     "service": "foo",
294                     "method": "Bar"
295                 }
296             ],
297             "maxRequestMessageBytes": "1024",
298             "maxResponseMessageBytes": "2048"
299         },
300         {
301             "name": [
302                 {
303                     "service": "foo",
304                     "method": "Bar"
305                 }
306             ],
307             "maxRequestMessageBytes": 1024,
308             "maxResponseMessageBytes": 2048
309         }
310     ]
311 }`,
312                         ServiceConfig{},
313                         true,
314                 },
315         }
316
317         for _, c := range testcases {
318                 sc, err := parseServiceConfig(c.scjs)
319                 if c.wantErr != (err != nil) || !reflect.DeepEqual(sc, c.wantSC) {
320                         t.Fatalf("parseServiceConfig(%s) = %+v, %v, want %+v, %v", c.scjs, sc, err, c.wantSC, c.wantErr)
321                 }
322         }
323 }