blob: 20104f9e12e54edf1f4343cb8801e322de8aff5e [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"
John Boiles57852792018-01-05 14:37:05 -080024 "context"
Jens Geyerf4598682014-05-08 23:18:44 +020025 "flag"
26 "gen/thrifttest"
27 t "log"
28 "reflect"
29 "thrift"
30)
31
32var host = flag.String("host", "localhost", "Host to connect")
33var port = flag.Int64("port", 9090, "Port number to connect")
34var 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 +020035var transport = flag.String("transport", "buffered", "Transport: buffered, framed, http, zlib")
Jens Geyerf4598682014-05-08 23:18:44 +020036var protocol = flag.String("protocol", "binary", "Protocol: binary, compact, json")
37var ssl = flag.Bool("ssl", false, "Encrypted Transport using SSL")
38var testloops = flag.Int("testloops", 1, "Number of Tests")
39
40func main() {
41 flag.Parse()
D. Can Celasun4f77ab82017-09-21 15:21:00 +020042 client, _, err := common.StartClient(*host, *port, *domain_socket, *transport, *protocol, *ssl)
Jens Geyerf4598682014-05-08 23:18:44 +020043 if err != nil {
44 t.Fatalf("Unable to start client: ", err)
45 }
46 for i := 0; i < *testloops; i++ {
47 callEverything(client)
48 }
49}
50
51var rmapmap = map[int32]map[int32]int32{
52 -4: map[int32]int32{-4: -4, -3: -3, -2: -2, -1: -1},
53 4: map[int32]int32{4: 4, 3: 3, 2: 2, 1: 1},
54}
55
56var xxs = &thrifttest.Xtruct{
57 StringThing: "Hello2",
58 ByteThing: 42,
59 I32Thing: 4242,
60 I64Thing: 424242,
61}
62
63var xcept = &thrifttest.Xception{ErrorCode: 1001, Message: "Xception"}
John Boiles57852792018-01-05 14:37:05 -080064var defaultCtx = context.Background()
Jens Geyerf4598682014-05-08 23:18:44 +020065
66func callEverything(client *thrifttest.ThriftTestClient) {
67 var err error
taozle5c302e02017-07-23 15:21:44 +020068 if err = client.TestVoid(defaultCtx); err != nil {
Jens Geyerf4598682014-05-08 23:18:44 +020069 t.Fatalf("Unexpected error in TestVoid() call: ", err)
70 }
71
taozle5c302e02017-07-23 15:21:44 +020072 thing, err := client.TestString(defaultCtx, "thing")
Jens Geyerf4598682014-05-08 23:18:44 +020073 if err != nil {
74 t.Fatalf("Unexpected error in TestString() call: ", err)
75 }
76 if thing != "thing" {
77 t.Fatalf("Unexpected TestString() result, expected 'thing' got '%s' ", thing)
78 }
79
taozle5c302e02017-07-23 15:21:44 +020080 bl, err := client.TestBool(defaultCtx, true)
Nobuaki Sukegawaa649e742015-09-21 13:53:25 +090081 if err != nil {
82 t.Fatalf("Unexpected error in TestBool() call: ", err)
83 }
84 if !bl {
85 t.Fatalf("Unexpected TestBool() result expected true, got %f ", bl)
86 }
taozle5c302e02017-07-23 15:21:44 +020087 bl, err = client.TestBool(defaultCtx, false)
Nobuaki Sukegawaa649e742015-09-21 13:53:25 +090088 if err != nil {
89 t.Fatalf("Unexpected error in TestBool() call: ", err)
90 }
91 if bl {
92 t.Fatalf("Unexpected TestBool() result expected false, got %f ", bl)
93 }
94
taozle5c302e02017-07-23 15:21:44 +020095 b, err := client.TestByte(defaultCtx, 42)
Jens Geyerf4598682014-05-08 23:18:44 +020096 if err != nil {
97 t.Fatalf("Unexpected error in TestByte() call: ", err)
98 }
99 if b != 42 {
100 t.Fatalf("Unexpected TestByte() result expected 42, got %d ", b)
101 }
102
taozle5c302e02017-07-23 15:21:44 +0200103 i32, err := client.TestI32(defaultCtx, 4242)
Jens Geyerf4598682014-05-08 23:18:44 +0200104 if err != nil {
105 t.Fatalf("Unexpected error in TestI32() call: ", err)
106 }
107 if i32 != 4242 {
108 t.Fatalf("Unexpected TestI32() result expected 4242, got %d ", i32)
109 }
110
taozle5c302e02017-07-23 15:21:44 +0200111 i64, err := client.TestI64(defaultCtx, 424242)
Jens Geyerf4598682014-05-08 23:18:44 +0200112 if err != nil {
113 t.Fatalf("Unexpected error in TestI64() call: ", err)
114 }
115 if i64 != 424242 {
116 t.Fatalf("Unexpected TestI64() result expected 424242, got %d ", i64)
117 }
118
taozle5c302e02017-07-23 15:21:44 +0200119 d, err := client.TestDouble(defaultCtx, 42.42)
Jens Geyerf4598682014-05-08 23:18:44 +0200120 if err != nil {
121 t.Fatalf("Unexpected error in TestDouble() call: ", err)
122 }
123 if d != 42.42 {
124 t.Fatalf("Unexpected TestDouble() result expected 42.42, got %f ", d)
125 }
126
Nobuaki Sukegawa9b35a7c2015-11-17 11:01:41 +0900127 binout := make([]byte, 256)
128 for i := 0; i < 256; i++ {
129 binout[i] = byte(i)
130 }
taozle5c302e02017-07-23 15:21:44 +0200131 bin, err := client.TestBinary(defaultCtx, binout)
Nobuaki Sukegawa9b35a7c2015-11-17 11:01:41 +0900132 for i := 0; i < 256; i++ {
D. Can Celasun4f77ab82017-09-21 15:21:00 +0200133 if binout[i] != bin[i] {
Nobuaki Sukegawa9b35a7c2015-11-17 11:01:41 +0900134 t.Fatalf("Unexpected TestBinary() result expected %d, got %d ", binout[i], bin[i])
135 }
136 }
taozle5c302e02017-07-23 15:21:44 +0200137
Jens Geyerf4598682014-05-08 23:18:44 +0200138 xs := thrifttest.NewXtruct()
139 xs.StringThing = "thing"
140 xs.ByteThing = 42
141 xs.I32Thing = 4242
142 xs.I64Thing = 424242
taozle5c302e02017-07-23 15:21:44 +0200143 xsret, err := client.TestStruct(defaultCtx, xs)
Jens Geyerf4598682014-05-08 23:18:44 +0200144 if err != nil {
145 t.Fatalf("Unexpected error in TestStruct() call: ", err)
146 }
147 if *xs != *xsret {
148 t.Fatalf("Unexpected TestStruct() result expected %#v, got %#v ", xs, xsret)
149 }
150
151 x2 := thrifttest.NewXtruct2()
152 x2.StructThing = xs
taozle5c302e02017-07-23 15:21:44 +0200153 x2ret, err := client.TestNest(defaultCtx, x2)
Jens Geyerf4598682014-05-08 23:18:44 +0200154 if err != nil {
155 t.Fatalf("Unexpected error in TestNest() call: ", err)
156 }
157 if !reflect.DeepEqual(x2, x2ret) {
158 t.Fatalf("Unexpected TestNest() result expected %#v, got %#v ", x2, x2ret)
159 }
160
161 m := map[int32]int32{1: 2, 3: 4, 5: 42}
taozle5c302e02017-07-23 15:21:44 +0200162 mret, err := client.TestMap(defaultCtx, m)
Jens Geyerf4598682014-05-08 23:18:44 +0200163 if err != nil {
164 t.Fatalf("Unexpected error in TestMap() call: ", err)
165 }
166 if !reflect.DeepEqual(m, mret) {
167 t.Fatalf("Unexpected TestMap() result expected %#v, got %#v ", m, mret)
168 }
169
170 sm := map[string]string{"a": "2", "b": "blah", "some": "thing"}
taozle5c302e02017-07-23 15:21:44 +0200171 smret, err := client.TestStringMap(defaultCtx, sm)
Jens Geyerf4598682014-05-08 23:18:44 +0200172 if err != nil {
173 t.Fatalf("Unexpected error in TestStringMap() call: ", err)
174 }
175 if !reflect.DeepEqual(sm, smret) {
176 t.Fatalf("Unexpected TestStringMap() result expected %#v, got %#v ", sm, smret)
177 }
178
D. Can Celasun43fb34d2017-01-15 10:53:19 +0100179 s := []int32{1, 2, 42}
taozle5c302e02017-07-23 15:21:44 +0200180 sret, err := client.TestSet(defaultCtx, s)
Jens Geyerf4598682014-05-08 23:18:44 +0200181 if err != nil {
182 t.Fatalf("Unexpected error in TestSet() call: ", err)
183 }
D. Can Celasun43fb34d2017-01-15 10:53:19 +0100184 // Sets can be in any order, but Go slices are ordered, so reflect.DeepEqual won't work.
185 stemp := map[int32]struct{}{}
186 for _, val := range s {
187 stemp[val] = struct{}{}
188 }
189 for _, val := range sret {
190 if _, ok := stemp[val]; !ok {
191 t.Fatalf("Unexpected TestSet() result expected %#v, got %#v ", s, sret)
192 }
Jens Geyerf4598682014-05-08 23:18:44 +0200193 }
194
195 l := []int32{1, 2, 42}
taozle5c302e02017-07-23 15:21:44 +0200196 lret, err := client.TestList(defaultCtx, l)
Jens Geyerf4598682014-05-08 23:18:44 +0200197 if err != nil {
198 t.Fatalf("Unexpected error in TestList() call: ", err)
199 }
200 if !reflect.DeepEqual(l, lret) {
D. Can Celasun43fb34d2017-01-15 10:53:19 +0100201 t.Fatalf("Unexpected TestList() result expected %#v, got %#v ", l, lret)
Jens Geyerf4598682014-05-08 23:18:44 +0200202 }
203
taozle5c302e02017-07-23 15:21:44 +0200204 eret, err := client.TestEnum(defaultCtx, thrifttest.Numberz_TWO)
Jens Geyerf4598682014-05-08 23:18:44 +0200205 if err != nil {
206 t.Fatalf("Unexpected error in TestEnum() call: ", err)
207 }
208 if eret != thrifttest.Numberz_TWO {
209 t.Fatalf("Unexpected TestEnum() result expected %#v, got %#v ", thrifttest.Numberz_TWO, eret)
210 }
211
taozle5c302e02017-07-23 15:21:44 +0200212 tret, err := client.TestTypedef(defaultCtx, thrifttest.UserId(42))
Jens Geyerf4598682014-05-08 23:18:44 +0200213 if err != nil {
214 t.Fatalf("Unexpected error in TestTypedef() call: ", err)
215 }
216 if tret != thrifttest.UserId(42) {
217 t.Fatalf("Unexpected TestTypedef() result expected %#v, got %#v ", thrifttest.UserId(42), tret)
218 }
219
taozle5c302e02017-07-23 15:21:44 +0200220 mapmap, err := client.TestMapMap(defaultCtx, 42)
Jens Geyerf4598682014-05-08 23:18:44 +0200221 if err != nil {
Nobuaki Sukegawaa649e742015-09-21 13:53:25 +0900222 t.Fatalf("Unexpected error in TestMapMap() call: ", err)
Jens Geyerf4598682014-05-08 23:18:44 +0200223 }
224 if !reflect.DeepEqual(mapmap, rmapmap) {
Nobuaki Sukegawaa649e742015-09-21 13:53:25 +0900225 t.Fatalf("Unexpected TestMapMap() result expected %#v, got %#v ", rmapmap, mapmap)
Jens Geyerf4598682014-05-08 23:18:44 +0200226 }
227
Nobuaki Sukegawa2fab3de2015-08-16 15:42:58 +0900228 crazy := thrifttest.NewInsanity()
D. Can Celasun4f77ab82017-09-21 15:21:00 +0200229 crazy.UserMap = map[thrifttest.Numberz]thrifttest.UserId{
230 thrifttest.Numberz_FIVE: 5,
Nobuaki Sukegawa2fab3de2015-08-16 15:42:58 +0900231 thrifttest.Numberz_EIGHT: 8,
232 }
233 truck1 := thrifttest.NewXtruct()
234 truck1.StringThing = "Goodbye4"
D. Can Celasun4f77ab82017-09-21 15:21:00 +0200235 truck1.ByteThing = 4
236 truck1.I32Thing = 4
237 truck1.I64Thing = 4
Nobuaki Sukegawa2fab3de2015-08-16 15:42:58 +0900238 truck2 := thrifttest.NewXtruct()
239 truck2.StringThing = "Hello2"
D. Can Celasun4f77ab82017-09-21 15:21:00 +0200240 truck2.ByteThing = 2
241 truck2.I32Thing = 2
242 truck2.I64Thing = 2
243 crazy.Xtructs = []*thrifttest.Xtruct{
Nobuaki Sukegawa2fab3de2015-08-16 15:42:58 +0900244 truck1,
245 truck2,
246 }
taozle5c302e02017-07-23 15:21:44 +0200247 insanity, err := client.TestInsanity(defaultCtx, crazy)
Nobuaki Sukegawa2fab3de2015-08-16 15:42:58 +0900248 if err != nil {
249 t.Fatalf("Unexpected error in TestInsanity() call: ", err)
250 }
251 if !reflect.DeepEqual(crazy, insanity[1][2]) {
252 t.Fatalf("Unexpected TestInsanity() first result expected %#v, got %#v ",
D. Can Celasun4f77ab82017-09-21 15:21:00 +0200253 crazy,
254 insanity[1][2])
Nobuaki Sukegawa2fab3de2015-08-16 15:42:58 +0900255 }
256 if !reflect.DeepEqual(crazy, insanity[1][3]) {
257 t.Fatalf("Unexpected TestInsanity() second result expected %#v, got %#v ",
D. Can Celasun4f77ab82017-09-21 15:21:00 +0200258 crazy,
259 insanity[1][3])
Nobuaki Sukegawa2fab3de2015-08-16 15:42:58 +0900260 }
261 if len(insanity[2][6].UserMap) > 0 || len(insanity[2][6].Xtructs) > 0 {
262 t.Fatalf("Unexpected TestInsanity() non-empty result got %#v ",
D. Can Celasun4f77ab82017-09-21 15:21:00 +0200263 insanity[2][6])
Nobuaki Sukegawa2fab3de2015-08-16 15:42:58 +0900264 }
265
taozle5c302e02017-07-23 15:21:44 +0200266 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 +0200267 if err != nil {
268 t.Fatalf("Unexpected error in TestMulti() call: ", err)
269 }
270 if !reflect.DeepEqual(xxs, xxsret) {
271 t.Fatalf("Unexpected TestMulti() result expected %#v, got %#v ", xxs, xxsret)
272 }
273
taozle5c302e02017-07-23 15:21:44 +0200274 err = client.TestException(defaultCtx, "Xception")
Jens Geyerf4598682014-05-08 23:18:44 +0200275 if err == nil {
276 t.Fatalf("Expecting exception in TestException() call")
277 }
278 if !reflect.DeepEqual(err, xcept) {
279 t.Fatalf("Unexpected TestException() result expected %#v, got %#v ", xcept, err)
280 }
281
taozle5c302e02017-07-23 15:21:44 +0200282 err = client.TestException(defaultCtx, "TException")
Nobuaki Sukegawa2fab3de2015-08-16 15:42:58 +0900283 _, ok := err.(thrift.TApplicationException)
284 if err == nil || !ok {
Jens Geyerf4598682014-05-08 23:18:44 +0200285 t.Fatalf("Unexpected TestException() result expected ApplicationError, got %#v ", err)
286 }
287
taozle5c302e02017-07-23 15:21:44 +0200288 ign, err := client.TestMultiException(defaultCtx, "Xception", "ignoreme")
Jens Geyerf4598682014-05-08 23:18:44 +0200289 if ign != nil || err == nil {
290 t.Fatalf("Expecting exception in TestMultiException() call")
291 }
292 if !reflect.DeepEqual(err, &thrifttest.Xception{ErrorCode: 1001, Message: "This is an Xception"}) {
293 t.Fatalf("Unexpected TestMultiException() %#v ", err)
294 }
295
taozle5c302e02017-07-23 15:21:44 +0200296 ign, err = client.TestMultiException(defaultCtx, "Xception2", "ignoreme")
Jens Geyerf4598682014-05-08 23:18:44 +0200297 if ign != nil || err == nil {
298 t.Fatalf("Expecting exception in TestMultiException() call")
299 }
300 expecting := &thrifttest.Xception2{ErrorCode: 2002, StructThing: &thrifttest.Xtruct{StringThing: "This is an Xception2"}}
301
302 if !reflect.DeepEqual(err, expecting) {
303 t.Fatalf("Unexpected TestMultiException() %#v ", err)
304 }
305
taozle5c302e02017-07-23 15:21:44 +0200306 err = client.TestOneway(defaultCtx, 2)
Jens Geyerf4598682014-05-08 23:18:44 +0200307 if err != nil {
308 t.Fatalf("Unexpected error in TestOneway() call: ", err)
309 }
310
311 //Make sure the connection still alive
taozle5c302e02017-07-23 15:21:44 +0200312 if err = client.TestVoid(defaultCtx); err != nil {
Jens Geyerf4598682014-05-08 23:18:44 +0200313 t.Fatalf("Unexpected error in TestVoid() call: ", err)
314 }
315}