blob: b34c539274459e99ee9b076c2217c5ce476ea854 [file] [log] [blame]
Jens Geyerf4598682014-05-08 23:18:44 +02001/*
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
20package main
21
22import (
23 "common"
24 "flag"
25 "gen/thrifttest"
26 t "log"
27 "reflect"
28 "thrift"
29)
30
31var host = flag.String("host", "localhost", "Host to connect")
32var port = flag.Int64("port", 9090, "Port number to connect")
33var domain_socket = flag.String("domain-socket", "", "Domain Socket (e.g. /tmp/thrifttest.thrift), instead of host and port")
Jens Geyerc6b991f2015-08-07 23:41:09 +020034var transport = flag.String("transport", "buffered", "Transport: buffered, framed, http, zlib")
Jens Geyerf4598682014-05-08 23:18:44 +020035var protocol = flag.String("protocol", "binary", "Protocol: binary, compact, json")
36var ssl = flag.Bool("ssl", false, "Encrypted Transport using SSL")
37var testloops = flag.Int("testloops", 1, "Number of Tests")
38
39func main() {
40 flag.Parse()
41 client, err := common.StartClient(*host, *port, *domain_socket, *transport, *protocol, *ssl)
42 if err != nil {
43 t.Fatalf("Unable to start client: ", err)
44 }
45 for i := 0; i < *testloops; i++ {
46 callEverything(client)
47 }
48}
49
50var rmapmap = map[int32]map[int32]int32{
51 -4: map[int32]int32{-4: -4, -3: -3, -2: -2, -1: -1},
52 4: map[int32]int32{4: 4, 3: 3, 2: 2, 1: 1},
53}
54
55var xxs = &thrifttest.Xtruct{
56 StringThing: "Hello2",
57 ByteThing: 42,
58 I32Thing: 4242,
59 I64Thing: 424242,
60}
61
62var xcept = &thrifttest.Xception{ErrorCode: 1001, Message: "Xception"}
63
64func callEverything(client *thrifttest.ThriftTestClient) {
65 var err error
taozle5c302e02017-07-23 15:21:44 +020066 if err = client.TestVoid(defaultCtx); err != nil {
Jens Geyerf4598682014-05-08 23:18:44 +020067 t.Fatalf("Unexpected error in TestVoid() call: ", err)
68 }
69
taozle5c302e02017-07-23 15:21:44 +020070 thing, err := client.TestString(defaultCtx, "thing")
Jens Geyerf4598682014-05-08 23:18:44 +020071 if err != nil {
72 t.Fatalf("Unexpected error in TestString() call: ", err)
73 }
74 if thing != "thing" {
75 t.Fatalf("Unexpected TestString() result, expected 'thing' got '%s' ", thing)
76 }
77
taozle5c302e02017-07-23 15:21:44 +020078 bl, err := client.TestBool(defaultCtx, true)
Nobuaki Sukegawaa649e742015-09-21 13:53:25 +090079 if err != nil {
80 t.Fatalf("Unexpected error in TestBool() call: ", err)
81 }
82 if !bl {
83 t.Fatalf("Unexpected TestBool() result expected true, got %f ", bl)
84 }
taozle5c302e02017-07-23 15:21:44 +020085 bl, err = client.TestBool(defaultCtx, false)
Nobuaki Sukegawaa649e742015-09-21 13:53:25 +090086 if err != nil {
87 t.Fatalf("Unexpected error in TestBool() call: ", err)
88 }
89 if bl {
90 t.Fatalf("Unexpected TestBool() result expected false, got %f ", bl)
91 }
92
taozle5c302e02017-07-23 15:21:44 +020093 b, err := client.TestByte(defaultCtx, 42)
Jens Geyerf4598682014-05-08 23:18:44 +020094 if err != nil {
95 t.Fatalf("Unexpected error in TestByte() call: ", err)
96 }
97 if b != 42 {
98 t.Fatalf("Unexpected TestByte() result expected 42, got %d ", b)
99 }
100
taozle5c302e02017-07-23 15:21:44 +0200101 i32, err := client.TestI32(defaultCtx, 4242)
Jens Geyerf4598682014-05-08 23:18:44 +0200102 if err != nil {
103 t.Fatalf("Unexpected error in TestI32() call: ", err)
104 }
105 if i32 != 4242 {
106 t.Fatalf("Unexpected TestI32() result expected 4242, got %d ", i32)
107 }
108
taozle5c302e02017-07-23 15:21:44 +0200109 i64, err := client.TestI64(defaultCtx, 424242)
Jens Geyerf4598682014-05-08 23:18:44 +0200110 if err != nil {
111 t.Fatalf("Unexpected error in TestI64() call: ", err)
112 }
113 if i64 != 424242 {
114 t.Fatalf("Unexpected TestI64() result expected 424242, got %d ", i64)
115 }
116
taozle5c302e02017-07-23 15:21:44 +0200117 d, err := client.TestDouble(defaultCtx, 42.42)
Jens Geyerf4598682014-05-08 23:18:44 +0200118 if err != nil {
119 t.Fatalf("Unexpected error in TestDouble() call: ", err)
120 }
121 if d != 42.42 {
122 t.Fatalf("Unexpected TestDouble() result expected 42.42, got %f ", d)
123 }
124
Nobuaki Sukegawa9b35a7c2015-11-17 11:01:41 +0900125 binout := make([]byte, 256)
126 for i := 0; i < 256; i++ {
127 binout[i] = byte(i)
128 }
taozle5c302e02017-07-23 15:21:44 +0200129 bin, err := client.TestBinary(defaultCtx, binout)
Nobuaki Sukegawa9b35a7c2015-11-17 11:01:41 +0900130 for i := 0; i < 256; i++ {
131 if (binout[i] != bin[i]) {
132 t.Fatalf("Unexpected TestBinary() result expected %d, got %d ", binout[i], bin[i])
133 }
134 }
taozle5c302e02017-07-23 15:21:44 +0200135
Jens Geyerf4598682014-05-08 23:18:44 +0200136 xs := thrifttest.NewXtruct()
137 xs.StringThing = "thing"
138 xs.ByteThing = 42
139 xs.I32Thing = 4242
140 xs.I64Thing = 424242
taozle5c302e02017-07-23 15:21:44 +0200141 xsret, err := client.TestStruct(defaultCtx, xs)
Jens Geyerf4598682014-05-08 23:18:44 +0200142 if err != nil {
143 t.Fatalf("Unexpected error in TestStruct() call: ", err)
144 }
145 if *xs != *xsret {
146 t.Fatalf("Unexpected TestStruct() result expected %#v, got %#v ", xs, xsret)
147 }
148
149 x2 := thrifttest.NewXtruct2()
150 x2.StructThing = xs
taozle5c302e02017-07-23 15:21:44 +0200151 x2ret, err := client.TestNest(defaultCtx, x2)
Jens Geyerf4598682014-05-08 23:18:44 +0200152 if err != nil {
153 t.Fatalf("Unexpected error in TestNest() call: ", err)
154 }
155 if !reflect.DeepEqual(x2, x2ret) {
156 t.Fatalf("Unexpected TestNest() result expected %#v, got %#v ", x2, x2ret)
157 }
158
159 m := map[int32]int32{1: 2, 3: 4, 5: 42}
taozle5c302e02017-07-23 15:21:44 +0200160 mret, err := client.TestMap(defaultCtx, m)
Jens Geyerf4598682014-05-08 23:18:44 +0200161 if err != nil {
162 t.Fatalf("Unexpected error in TestMap() call: ", err)
163 }
164 if !reflect.DeepEqual(m, mret) {
165 t.Fatalf("Unexpected TestMap() result expected %#v, got %#v ", m, mret)
166 }
167
168 sm := map[string]string{"a": "2", "b": "blah", "some": "thing"}
taozle5c302e02017-07-23 15:21:44 +0200169 smret, err := client.TestStringMap(defaultCtx, sm)
Jens Geyerf4598682014-05-08 23:18:44 +0200170 if err != nil {
171 t.Fatalf("Unexpected error in TestStringMap() call: ", err)
172 }
173 if !reflect.DeepEqual(sm, smret) {
174 t.Fatalf("Unexpected TestStringMap() result expected %#v, got %#v ", sm, smret)
175 }
176
D. Can Celasun43fb34d2017-01-15 10:53:19 +0100177 s := []int32{1, 2, 42}
taozle5c302e02017-07-23 15:21:44 +0200178 sret, err := client.TestSet(defaultCtx, s)
Jens Geyerf4598682014-05-08 23:18:44 +0200179 if err != nil {
180 t.Fatalf("Unexpected error in TestSet() call: ", err)
181 }
D. Can Celasun43fb34d2017-01-15 10:53:19 +0100182 // Sets can be in any order, but Go slices are ordered, so reflect.DeepEqual won't work.
183 stemp := map[int32]struct{}{}
184 for _, val := range s {
185 stemp[val] = struct{}{}
186 }
187 for _, val := range sret {
188 if _, ok := stemp[val]; !ok {
189 t.Fatalf("Unexpected TestSet() result expected %#v, got %#v ", s, sret)
190 }
Jens Geyerf4598682014-05-08 23:18:44 +0200191 }
192
193 l := []int32{1, 2, 42}
taozle5c302e02017-07-23 15:21:44 +0200194 lret, err := client.TestList(defaultCtx, l)
Jens Geyerf4598682014-05-08 23:18:44 +0200195 if err != nil {
196 t.Fatalf("Unexpected error in TestList() call: ", err)
197 }
198 if !reflect.DeepEqual(l, lret) {
D. Can Celasun43fb34d2017-01-15 10:53:19 +0100199 t.Fatalf("Unexpected TestList() result expected %#v, got %#v ", l, lret)
Jens Geyerf4598682014-05-08 23:18:44 +0200200 }
201
taozle5c302e02017-07-23 15:21:44 +0200202 eret, err := client.TestEnum(defaultCtx, thrifttest.Numberz_TWO)
Jens Geyerf4598682014-05-08 23:18:44 +0200203 if err != nil {
204 t.Fatalf("Unexpected error in TestEnum() call: ", err)
205 }
206 if eret != thrifttest.Numberz_TWO {
207 t.Fatalf("Unexpected TestEnum() result expected %#v, got %#v ", thrifttest.Numberz_TWO, eret)
208 }
209
taozle5c302e02017-07-23 15:21:44 +0200210 tret, err := client.TestTypedef(defaultCtx, thrifttest.UserId(42))
Jens Geyerf4598682014-05-08 23:18:44 +0200211 if err != nil {
212 t.Fatalf("Unexpected error in TestTypedef() call: ", err)
213 }
214 if tret != thrifttest.UserId(42) {
215 t.Fatalf("Unexpected TestTypedef() result expected %#v, got %#v ", thrifttest.UserId(42), tret)
216 }
217
taozle5c302e02017-07-23 15:21:44 +0200218 mapmap, err := client.TestMapMap(defaultCtx, 42)
Jens Geyerf4598682014-05-08 23:18:44 +0200219 if err != nil {
Nobuaki Sukegawaa649e742015-09-21 13:53:25 +0900220 t.Fatalf("Unexpected error in TestMapMap() call: ", err)
Jens Geyerf4598682014-05-08 23:18:44 +0200221 }
222 if !reflect.DeepEqual(mapmap, rmapmap) {
Nobuaki Sukegawaa649e742015-09-21 13:53:25 +0900223 t.Fatalf("Unexpected TestMapMap() result expected %#v, got %#v ", rmapmap, mapmap)
Jens Geyerf4598682014-05-08 23:18:44 +0200224 }
225
Nobuaki Sukegawa2fab3de2015-08-16 15:42:58 +0900226 crazy := thrifttest.NewInsanity()
227 crazy.UserMap = map[thrifttest.Numberz]thrifttest.UserId {
228 thrifttest.Numberz_FIVE: 5,
229 thrifttest.Numberz_EIGHT: 8,
230 }
231 truck1 := thrifttest.NewXtruct()
232 truck1.StringThing = "Goodbye4"
233 truck1.ByteThing = 4;
234 truck1.I32Thing = 4;
235 truck1.I64Thing = 4;
236 truck2 := thrifttest.NewXtruct()
237 truck2.StringThing = "Hello2"
238 truck2.ByteThing = 2;
239 truck2.I32Thing = 2;
240 truck2.I64Thing = 2;
241 crazy.Xtructs = []*thrifttest.Xtruct {
242 truck1,
243 truck2,
244 }
taozle5c302e02017-07-23 15:21:44 +0200245 insanity, err := client.TestInsanity(defaultCtx, crazy)
Nobuaki Sukegawa2fab3de2015-08-16 15:42:58 +0900246 if err != nil {
247 t.Fatalf("Unexpected error in TestInsanity() call: ", err)
248 }
249 if !reflect.DeepEqual(crazy, insanity[1][2]) {
250 t.Fatalf("Unexpected TestInsanity() first result expected %#v, got %#v ",
251 crazy,
252 insanity[1][2])
253 }
254 if !reflect.DeepEqual(crazy, insanity[1][3]) {
255 t.Fatalf("Unexpected TestInsanity() second result expected %#v, got %#v ",
256 crazy,
257 insanity[1][3])
258 }
259 if len(insanity[2][6].UserMap) > 0 || len(insanity[2][6].Xtructs) > 0 {
260 t.Fatalf("Unexpected TestInsanity() non-empty result got %#v ",
261 insanity[2][6])
262 }
263
taozle5c302e02017-07-23 15:21:44 +0200264 xxsret, err := client.TestMulti(defaultCtx, 42, 4242, 424242, map[int16]string{1: "blah", 2: "thing"}, thrifttest.Numberz_EIGHT, thrifttest.UserId(24))
Jens Geyerf4598682014-05-08 23:18:44 +0200265 if err != nil {
266 t.Fatalf("Unexpected error in TestMulti() call: ", err)
267 }
268 if !reflect.DeepEqual(xxs, xxsret) {
269 t.Fatalf("Unexpected TestMulti() result expected %#v, got %#v ", xxs, xxsret)
270 }
271
taozle5c302e02017-07-23 15:21:44 +0200272 err = client.TestException(defaultCtx, "Xception")
Jens Geyerf4598682014-05-08 23:18:44 +0200273 if err == nil {
274 t.Fatalf("Expecting exception in TestException() call")
275 }
276 if !reflect.DeepEqual(err, xcept) {
277 t.Fatalf("Unexpected TestException() result expected %#v, got %#v ", xcept, err)
278 }
279
taozle5c302e02017-07-23 15:21:44 +0200280 err = client.TestException(defaultCtx, "TException")
Nobuaki Sukegawa2fab3de2015-08-16 15:42:58 +0900281 _, ok := err.(thrift.TApplicationException)
282 if err == nil || !ok {
Jens Geyerf4598682014-05-08 23:18:44 +0200283 t.Fatalf("Unexpected TestException() result expected ApplicationError, got %#v ", err)
284 }
285
taozle5c302e02017-07-23 15:21:44 +0200286 ign, err := client.TestMultiException(defaultCtx, "Xception", "ignoreme")
Jens Geyerf4598682014-05-08 23:18:44 +0200287 if ign != nil || err == nil {
288 t.Fatalf("Expecting exception in TestMultiException() call")
289 }
290 if !reflect.DeepEqual(err, &thrifttest.Xception{ErrorCode: 1001, Message: "This is an Xception"}) {
291 t.Fatalf("Unexpected TestMultiException() %#v ", err)
292 }
293
taozle5c302e02017-07-23 15:21:44 +0200294 ign, err = client.TestMultiException(defaultCtx, "Xception2", "ignoreme")
Jens Geyerf4598682014-05-08 23:18:44 +0200295 if ign != nil || err == nil {
296 t.Fatalf("Expecting exception in TestMultiException() call")
297 }
298 expecting := &thrifttest.Xception2{ErrorCode: 2002, StructThing: &thrifttest.Xtruct{StringThing: "This is an Xception2"}}
299
300 if !reflect.DeepEqual(err, expecting) {
301 t.Fatalf("Unexpected TestMultiException() %#v ", err)
302 }
303
taozle5c302e02017-07-23 15:21:44 +0200304 err = client.TestOneway(defaultCtx, 2)
Jens Geyerf4598682014-05-08 23:18:44 +0200305 if err != nil {
306 t.Fatalf("Unexpected error in TestOneway() call: ", err)
307 }
308
309 //Make sure the connection still alive
taozle5c302e02017-07-23 15:21:44 +0200310 if err = client.TestVoid(defaultCtx); err != nil {
Jens Geyerf4598682014-05-08 23:18:44 +0200311 t.Fatalf("Unexpected error in TestVoid() call: ", err)
312 }
313}