]>
Commit | Line | Data |
---|---|---|
f67539c2 TL |
1 | /* |
2 | * Licensed to the Apache Software Foundation (ASF) under one | |
3 | * or more contributor license agreements. See the NOTICE file | |
4 | * distributed with this work for additional information | |
5 | * regarding copyright ownership. The ASF licenses this file | |
6 | * to you under the Apache License, Version 2.0 (the | |
7 | * "License"); you may not use this file except in compliance | |
8 | * with the License. You may obtain a copy of the License at | |
9 | * | |
10 | * http://www.apache.org/licenses/LICENSE-2.0 | |
11 | * | |
12 | * Unless required by applicable law or agreed to in writing, | |
13 | * software distributed under the License is distributed on an | |
14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
15 | * KIND, either express or implied. See the License for the | |
16 | * specific language governing permissions and limitations | |
17 | * under the License. | |
18 | */ | |
19 | ||
20 | package main | |
21 | ||
22 | import ( | |
23 | "common" | |
24 | "context" | |
25 | "flag" | |
26 | "gen/thrifttest" | |
27 | t "log" | |
28 | "reflect" | |
29 | "thrift" | |
30 | ) | |
31 | ||
32 | var host = flag.String("host", "localhost", "Host to connect") | |
33 | var port = flag.Int64("port", 9090, "Port number to connect") | |
34 | var domain_socket = flag.String("domain-socket", "", "Domain Socket (e.g. /tmp/thrifttest.thrift), instead of host and port") | |
35 | var transport = flag.String("transport", "buffered", "Transport: buffered, framed, http, zlib") | |
36 | var protocol = flag.String("protocol", "binary", "Protocol: binary, compact, json") | |
37 | var ssl = flag.Bool("ssl", false, "Encrypted Transport using SSL") | |
38 | var zlib = flag.Bool("zlib", false, "Wrapped Transport using Zlib") | |
39 | var testloops = flag.Int("testloops", 1, "Number of Tests") | |
40 | ||
41 | func main() { | |
42 | flag.Parse() | |
43 | client, _, err := common.StartClient(*host, *port, *domain_socket, *transport, *protocol, *ssl) | |
44 | if err != nil { | |
45 | t.Fatalf("Unable to start client: ", err) | |
46 | } | |
47 | for i := 0; i < *testloops; i++ { | |
48 | callEverything(client) | |
49 | } | |
50 | } | |
51 | ||
52 | var rmapmap = map[int32]map[int32]int32{ | |
53 | -4: map[int32]int32{-4: -4, -3: -3, -2: -2, -1: -1}, | |
54 | 4: map[int32]int32{4: 4, 3: 3, 2: 2, 1: 1}, | |
55 | } | |
56 | ||
57 | var xxs = &thrifttest.Xtruct{ | |
58 | StringThing: "Hello2", | |
59 | ByteThing: 42, | |
60 | I32Thing: 4242, | |
61 | I64Thing: 424242, | |
62 | } | |
63 | ||
64 | var xcept = &thrifttest.Xception{ErrorCode: 1001, Message: "Xception"} | |
65 | var defaultCtx = context.Background() | |
66 | ||
67 | func callEverything(client *thrifttest.ThriftTestClient) { | |
68 | var err error | |
69 | if err = client.TestVoid(defaultCtx); err != nil { | |
70 | t.Fatalf("Unexpected error in TestVoid() call: ", err) | |
71 | } | |
72 | ||
73 | thing, err := client.TestString(defaultCtx, "thing") | |
74 | if err != nil { | |
75 | t.Fatalf("Unexpected error in TestString() call: ", err) | |
76 | } | |
77 | if thing != "thing" { | |
78 | t.Fatalf("Unexpected TestString() result, expected 'thing' got '%s' ", thing) | |
79 | } | |
80 | ||
81 | bl, err := client.TestBool(defaultCtx, true) | |
82 | if err != nil { | |
83 | t.Fatalf("Unexpected error in TestBool() call: ", err) | |
84 | } | |
85 | if !bl { | |
86 | t.Fatalf("Unexpected TestBool() result expected true, got %f ", bl) | |
87 | } | |
88 | bl, err = client.TestBool(defaultCtx, false) | |
89 | if err != nil { | |
90 | t.Fatalf("Unexpected error in TestBool() call: ", err) | |
91 | } | |
92 | if bl { | |
93 | t.Fatalf("Unexpected TestBool() result expected false, got %f ", bl) | |
94 | } | |
95 | ||
96 | b, err := client.TestByte(defaultCtx, 42) | |
97 | if err != nil { | |
98 | t.Fatalf("Unexpected error in TestByte() call: ", err) | |
99 | } | |
100 | if b != 42 { | |
101 | t.Fatalf("Unexpected TestByte() result expected 42, got %d ", b) | |
102 | } | |
103 | ||
104 | i32, err := client.TestI32(defaultCtx, 4242) | |
105 | if err != nil { | |
106 | t.Fatalf("Unexpected error in TestI32() call: ", err) | |
107 | } | |
108 | if i32 != 4242 { | |
109 | t.Fatalf("Unexpected TestI32() result expected 4242, got %d ", i32) | |
110 | } | |
111 | ||
112 | i64, err := client.TestI64(defaultCtx, 424242) | |
113 | if err != nil { | |
114 | t.Fatalf("Unexpected error in TestI64() call: ", err) | |
115 | } | |
116 | if i64 != 424242 { | |
117 | t.Fatalf("Unexpected TestI64() result expected 424242, got %d ", i64) | |
118 | } | |
119 | ||
120 | d, err := client.TestDouble(defaultCtx, 42.42) | |
121 | if err != nil { | |
122 | t.Fatalf("Unexpected error in TestDouble() call: ", err) | |
123 | } | |
124 | if d != 42.42 { | |
125 | t.Fatalf("Unexpected TestDouble() result expected 42.42, got %f ", d) | |
126 | } | |
127 | ||
128 | binout := make([]byte, 256) | |
129 | for i := 0; i < 256; i++ { | |
130 | binout[i] = byte(i) | |
131 | } | |
132 | bin, err := client.TestBinary(defaultCtx, binout) | |
133 | for i := 0; i < 256; i++ { | |
134 | if binout[i] != bin[i] { | |
135 | t.Fatalf("Unexpected TestBinary() result expected %d, got %d ", binout[i], bin[i]) | |
136 | } | |
137 | } | |
138 | ||
139 | xs := thrifttest.NewXtruct() | |
140 | xs.StringThing = "thing" | |
141 | xs.ByteThing = 42 | |
142 | xs.I32Thing = 4242 | |
143 | xs.I64Thing = 424242 | |
144 | xsret, err := client.TestStruct(defaultCtx, xs) | |
145 | if err != nil { | |
146 | t.Fatalf("Unexpected error in TestStruct() call: ", err) | |
147 | } | |
148 | if *xs != *xsret { | |
149 | t.Fatalf("Unexpected TestStruct() result expected %#v, got %#v ", xs, xsret) | |
150 | } | |
151 | ||
152 | x2 := thrifttest.NewXtruct2() | |
153 | x2.StructThing = xs | |
154 | x2ret, err := client.TestNest(defaultCtx, x2) | |
155 | if err != nil { | |
156 | t.Fatalf("Unexpected error in TestNest() call: ", err) | |
157 | } | |
158 | if !reflect.DeepEqual(x2, x2ret) { | |
159 | t.Fatalf("Unexpected TestNest() result expected %#v, got %#v ", x2, x2ret) | |
160 | } | |
161 | ||
162 | m := map[int32]int32{1: 2, 3: 4, 5: 42} | |
163 | mret, err := client.TestMap(defaultCtx, m) | |
164 | if err != nil { | |
165 | t.Fatalf("Unexpected error in TestMap() call: ", err) | |
166 | } | |
167 | if !reflect.DeepEqual(m, mret) { | |
168 | t.Fatalf("Unexpected TestMap() result expected %#v, got %#v ", m, mret) | |
169 | } | |
170 | ||
171 | sm := map[string]string{"a": "2", "b": "blah", "some": "thing"} | |
172 | smret, err := client.TestStringMap(defaultCtx, sm) | |
173 | if err != nil { | |
174 | t.Fatalf("Unexpected error in TestStringMap() call: ", err) | |
175 | } | |
176 | if !reflect.DeepEqual(sm, smret) { | |
177 | t.Fatalf("Unexpected TestStringMap() result expected %#v, got %#v ", sm, smret) | |
178 | } | |
179 | ||
180 | s := []int32{1, 2, 42} | |
181 | sret, err := client.TestSet(defaultCtx, s) | |
182 | if err != nil { | |
183 | t.Fatalf("Unexpected error in TestSet() call: ", err) | |
184 | } | |
185 | // Sets can be in any order, but Go slices are ordered, so reflect.DeepEqual won't work. | |
186 | stemp := map[int32]struct{}{} | |
187 | for _, val := range s { | |
188 | stemp[val] = struct{}{} | |
189 | } | |
190 | for _, val := range sret { | |
191 | if _, ok := stemp[val]; !ok { | |
192 | t.Fatalf("Unexpected TestSet() result expected %#v, got %#v ", s, sret) | |
193 | } | |
194 | } | |
195 | ||
196 | l := []int32{1, 2, 42} | |
197 | lret, err := client.TestList(defaultCtx, l) | |
198 | if err != nil { | |
199 | t.Fatalf("Unexpected error in TestList() call: ", err) | |
200 | } | |
201 | if !reflect.DeepEqual(l, lret) { | |
202 | t.Fatalf("Unexpected TestList() result expected %#v, got %#v ", l, lret) | |
203 | } | |
204 | ||
205 | eret, err := client.TestEnum(defaultCtx, thrifttest.Numberz_TWO) | |
206 | if err != nil { | |
207 | t.Fatalf("Unexpected error in TestEnum() call: ", err) | |
208 | } | |
209 | if eret != thrifttest.Numberz_TWO { | |
210 | t.Fatalf("Unexpected TestEnum() result expected %#v, got %#v ", thrifttest.Numberz_TWO, eret) | |
211 | } | |
212 | ||
213 | tret, err := client.TestTypedef(defaultCtx, thrifttest.UserId(42)) | |
214 | if err != nil { | |
215 | t.Fatalf("Unexpected error in TestTypedef() call: ", err) | |
216 | } | |
217 | if tret != thrifttest.UserId(42) { | |
218 | t.Fatalf("Unexpected TestTypedef() result expected %#v, got %#v ", thrifttest.UserId(42), tret) | |
219 | } | |
220 | ||
221 | mapmap, err := client.TestMapMap(defaultCtx, 42) | |
222 | if err != nil { | |
223 | t.Fatalf("Unexpected error in TestMapMap() call: ", err) | |
224 | } | |
225 | if !reflect.DeepEqual(mapmap, rmapmap) { | |
226 | t.Fatalf("Unexpected TestMapMap() result expected %#v, got %#v ", rmapmap, mapmap) | |
227 | } | |
228 | ||
229 | crazy := thrifttest.NewInsanity() | |
230 | crazy.UserMap = map[thrifttest.Numberz]thrifttest.UserId{ | |
231 | thrifttest.Numberz_FIVE: 5, | |
232 | thrifttest.Numberz_EIGHT: 8, | |
233 | } | |
234 | truck1 := thrifttest.NewXtruct() | |
235 | truck1.StringThing = "Goodbye4" | |
236 | truck1.ByteThing = 4 | |
237 | truck1.I32Thing = 4 | |
238 | truck1.I64Thing = 4 | |
239 | truck2 := thrifttest.NewXtruct() | |
240 | truck2.StringThing = "Hello2" | |
241 | truck2.ByteThing = 2 | |
242 | truck2.I32Thing = 2 | |
243 | truck2.I64Thing = 2 | |
244 | crazy.Xtructs = []*thrifttest.Xtruct{ | |
245 | truck1, | |
246 | truck2, | |
247 | } | |
248 | insanity, err := client.TestInsanity(defaultCtx, crazy) | |
249 | if err != nil { | |
250 | t.Fatalf("Unexpected error in TestInsanity() call: ", err) | |
251 | } | |
252 | if !reflect.DeepEqual(crazy, insanity[1][2]) { | |
253 | t.Fatalf("Unexpected TestInsanity() first result expected %#v, got %#v ", | |
254 | crazy, | |
255 | insanity[1][2]) | |
256 | } | |
257 | if !reflect.DeepEqual(crazy, insanity[1][3]) { | |
258 | t.Fatalf("Unexpected TestInsanity() second result expected %#v, got %#v ", | |
259 | crazy, | |
260 | insanity[1][3]) | |
261 | } | |
262 | if len(insanity[2][6].UserMap) > 0 || len(insanity[2][6].Xtructs) > 0 { | |
263 | t.Fatalf("Unexpected TestInsanity() non-empty result got %#v ", | |
264 | insanity[2][6]) | |
265 | } | |
266 | ||
267 | xxsret, err := client.TestMulti(defaultCtx, 42, 4242, 424242, map[int16]string{1: "blah", 2: "thing"}, thrifttest.Numberz_EIGHT, thrifttest.UserId(24)) | |
268 | if err != nil { | |
269 | t.Fatalf("Unexpected error in TestMulti() call: ", err) | |
270 | } | |
271 | if !reflect.DeepEqual(xxs, xxsret) { | |
272 | t.Fatalf("Unexpected TestMulti() result expected %#v, got %#v ", xxs, xxsret) | |
273 | } | |
274 | ||
275 | err = client.TestException(defaultCtx, "Xception") | |
276 | if err == nil { | |
277 | t.Fatalf("Expecting exception in TestException() call") | |
278 | } | |
279 | if !reflect.DeepEqual(err, xcept) { | |
280 | t.Fatalf("Unexpected TestException() result expected %#v, got %#v ", xcept, err) | |
281 | } | |
282 | ||
283 | err = client.TestException(defaultCtx, "TException") | |
284 | _, ok := err.(thrift.TApplicationException) | |
285 | if err == nil || !ok { | |
286 | t.Fatalf("Unexpected TestException() result expected ApplicationError, got %#v ", err) | |
287 | } | |
288 | ||
289 | ign, err := client.TestMultiException(defaultCtx, "Xception", "ignoreme") | |
290 | if ign != nil || err == nil { | |
291 | t.Fatalf("Expecting exception in TestMultiException() call") | |
292 | } | |
293 | if !reflect.DeepEqual(err, &thrifttest.Xception{ErrorCode: 1001, Message: "This is an Xception"}) { | |
294 | t.Fatalf("Unexpected TestMultiException() %#v ", err) | |
295 | } | |
296 | ||
297 | ign, err = client.TestMultiException(defaultCtx, "Xception2", "ignoreme") | |
298 | if ign != nil || err == nil { | |
299 | t.Fatalf("Expecting exception in TestMultiException() call") | |
300 | } | |
301 | expecting := &thrifttest.Xception2{ErrorCode: 2002, StructThing: &thrifttest.Xtruct{StringThing: "This is an Xception2"}} | |
302 | ||
303 | if !reflect.DeepEqual(err, expecting) { | |
304 | t.Fatalf("Unexpected TestMultiException() %#v ", err) | |
305 | } | |
306 | ||
307 | err = client.TestOneway(defaultCtx, 2) | |
308 | if err != nil { | |
309 | t.Fatalf("Unexpected error in TestOneway() call: ", err) | |
310 | } | |
311 | ||
312 | //Make sure the connection still alive | |
313 | if err = client.TestVoid(defaultCtx); err != nil { | |
314 | t.Fatalf("Unexpected error in TestVoid() call: ", err) | |
315 | } | |
316 | } |