blob: f0ce0528e9e56001426222032ec648732dd71fcb [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 (
John Boiles57852792018-01-05 14:37:05 -080023 "context"
Jens Geyerf4598682014-05-08 23:18:44 +020024 "flag"
Jens Geyerf4598682014-05-08 23:18:44 +020025 t "log"
26 "reflect"
Yuxuan 'fishy' Wangb71f11e2021-03-22 15:01:00 -070027
28 "github.com/apache/thrift/lib/go/thrift"
29 "github.com/apache/thrift/test/go/src/common"
30 "github.com/apache/thrift/test/go/src/gen/thrifttest"
Jens Geyerf4598682014-05-08 23:18:44 +020031)
32
33var host = flag.String("host", "localhost", "Host to connect")
34var port = flag.Int64("port", 9090, "Port number to connect")
35var 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 +020036var transport = flag.String("transport", "buffered", "Transport: buffered, framed, http, zlib")
Jens Geyerf4598682014-05-08 23:18:44 +020037var protocol = flag.String("protocol", "binary", "Protocol: binary, compact, json")
38var ssl = flag.Bool("ssl", false, "Encrypted Transport using SSL")
39var testloops = flag.Int("testloops", 1, "Number of Tests")
40
41func main() {
42 flag.Parse()
D. Can Celasun4f77ab82017-09-21 15:21:00 +020043 client, _, err := common.StartClient(*host, *port, *domain_socket, *transport, *protocol, *ssl)
Jens Geyerf4598682014-05-08 23:18:44 +020044 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
52var rmapmap = map[int32]map[int32]int32{
Yuxuan 'fishy' Wangb71f11e2021-03-22 15:01:00 -070053 -4: {-4: -4, -3: -3, -2: -2, -1: -1},
54 4: {4: 4, 3: 3, 2: 2, 1: 1},
Jens Geyerf4598682014-05-08 23:18:44 +020055}
56
57var xxs = &thrifttest.Xtruct{
58 StringThing: "Hello2",
59 ByteThing: 42,
60 I32Thing: 4242,
61 I64Thing: 424242,
62}
63
64var xcept = &thrifttest.Xception{ErrorCode: 1001, Message: "Xception"}
John Boiles57852792018-01-05 14:37:05 -080065var defaultCtx = context.Background()
Jens Geyerf4598682014-05-08 23:18:44 +020066
67func callEverything(client *thrifttest.ThriftTestClient) {
68 var err error
taozle5c302e02017-07-23 15:21:44 +020069 if err = client.TestVoid(defaultCtx); err != nil {
Jens Geyerf4598682014-05-08 23:18:44 +020070 t.Fatalf("Unexpected error in TestVoid() call: ", err)
71 }
72
taozle5c302e02017-07-23 15:21:44 +020073 thing, err := client.TestString(defaultCtx, "thing")
Jens Geyerf4598682014-05-08 23:18:44 +020074 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
taozle5c302e02017-07-23 15:21:44 +020081 bl, err := client.TestBool(defaultCtx, true)
Nobuaki Sukegawaa649e742015-09-21 13:53:25 +090082 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 }
taozle5c302e02017-07-23 15:21:44 +020088 bl, err = client.TestBool(defaultCtx, false)
Nobuaki Sukegawaa649e742015-09-21 13:53:25 +090089 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
taozle5c302e02017-07-23 15:21:44 +020096 b, err := client.TestByte(defaultCtx, 42)
Jens Geyerf4598682014-05-08 23:18:44 +020097 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
taozle5c302e02017-07-23 15:21:44 +0200104 i32, err := client.TestI32(defaultCtx, 4242)
Jens Geyerf4598682014-05-08 23:18:44 +0200105 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
taozle5c302e02017-07-23 15:21:44 +0200112 i64, err := client.TestI64(defaultCtx, 424242)
Jens Geyerf4598682014-05-08 23:18:44 +0200113 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
taozle5c302e02017-07-23 15:21:44 +0200120 d, err := client.TestDouble(defaultCtx, 42.42)
Jens Geyerf4598682014-05-08 23:18:44 +0200121 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
Nobuaki Sukegawa9b35a7c2015-11-17 11:01:41 +0900128 binout := make([]byte, 256)
129 for i := 0; i < 256; i++ {
130 binout[i] = byte(i)
131 }
taozle5c302e02017-07-23 15:21:44 +0200132 bin, err := client.TestBinary(defaultCtx, binout)
Yuxuan 'fishy' Wang17373a32021-08-26 11:04:27 -0700133 if err != nil {
134 t.Fatalf("TestBinary failed with %v", err)
135 }
Nobuaki Sukegawa9b35a7c2015-11-17 11:01:41 +0900136 for i := 0; i < 256; i++ {
D. Can Celasun4f77ab82017-09-21 15:21:00 +0200137 if binout[i] != bin[i] {
Nobuaki Sukegawa9b35a7c2015-11-17 11:01:41 +0900138 t.Fatalf("Unexpected TestBinary() result expected %d, got %d ", binout[i], bin[i])
139 }
140 }
taozle5c302e02017-07-23 15:21:44 +0200141
Jens Geyerf4598682014-05-08 23:18:44 +0200142 xs := thrifttest.NewXtruct()
143 xs.StringThing = "thing"
144 xs.ByteThing = 42
145 xs.I32Thing = 4242
146 xs.I64Thing = 424242
taozle5c302e02017-07-23 15:21:44 +0200147 xsret, err := client.TestStruct(defaultCtx, xs)
Jens Geyerf4598682014-05-08 23:18:44 +0200148 if err != nil {
149 t.Fatalf("Unexpected error in TestStruct() call: ", err)
150 }
151 if *xs != *xsret {
152 t.Fatalf("Unexpected TestStruct() result expected %#v, got %#v ", xs, xsret)
153 }
154
155 x2 := thrifttest.NewXtruct2()
156 x2.StructThing = xs
taozle5c302e02017-07-23 15:21:44 +0200157 x2ret, err := client.TestNest(defaultCtx, x2)
Jens Geyerf4598682014-05-08 23:18:44 +0200158 if err != nil {
159 t.Fatalf("Unexpected error in TestNest() call: ", err)
160 }
161 if !reflect.DeepEqual(x2, x2ret) {
162 t.Fatalf("Unexpected TestNest() result expected %#v, got %#v ", x2, x2ret)
163 }
164
165 m := map[int32]int32{1: 2, 3: 4, 5: 42}
taozle5c302e02017-07-23 15:21:44 +0200166 mret, err := client.TestMap(defaultCtx, m)
Jens Geyerf4598682014-05-08 23:18:44 +0200167 if err != nil {
168 t.Fatalf("Unexpected error in TestMap() call: ", err)
169 }
170 if !reflect.DeepEqual(m, mret) {
171 t.Fatalf("Unexpected TestMap() result expected %#v, got %#v ", m, mret)
172 }
173
174 sm := map[string]string{"a": "2", "b": "blah", "some": "thing"}
taozle5c302e02017-07-23 15:21:44 +0200175 smret, err := client.TestStringMap(defaultCtx, sm)
Jens Geyerf4598682014-05-08 23:18:44 +0200176 if err != nil {
177 t.Fatalf("Unexpected error in TestStringMap() call: ", err)
178 }
179 if !reflect.DeepEqual(sm, smret) {
180 t.Fatalf("Unexpected TestStringMap() result expected %#v, got %#v ", sm, smret)
181 }
182
D. Can Celasun43fb34d2017-01-15 10:53:19 +0100183 s := []int32{1, 2, 42}
taozle5c302e02017-07-23 15:21:44 +0200184 sret, err := client.TestSet(defaultCtx, s)
Jens Geyerf4598682014-05-08 23:18:44 +0200185 if err != nil {
186 t.Fatalf("Unexpected error in TestSet() call: ", err)
187 }
D. Can Celasun43fb34d2017-01-15 10:53:19 +0100188 // Sets can be in any order, but Go slices are ordered, so reflect.DeepEqual won't work.
189 stemp := map[int32]struct{}{}
190 for _, val := range s {
191 stemp[val] = struct{}{}
192 }
193 for _, val := range sret {
194 if _, ok := stemp[val]; !ok {
195 t.Fatalf("Unexpected TestSet() result expected %#v, got %#v ", s, sret)
196 }
Jens Geyerf4598682014-05-08 23:18:44 +0200197 }
198
199 l := []int32{1, 2, 42}
taozle5c302e02017-07-23 15:21:44 +0200200 lret, err := client.TestList(defaultCtx, l)
Jens Geyerf4598682014-05-08 23:18:44 +0200201 if err != nil {
202 t.Fatalf("Unexpected error in TestList() call: ", err)
203 }
204 if !reflect.DeepEqual(l, lret) {
D. Can Celasun43fb34d2017-01-15 10:53:19 +0100205 t.Fatalf("Unexpected TestList() result expected %#v, got %#v ", l, lret)
Jens Geyerf4598682014-05-08 23:18:44 +0200206 }
207
taozle5c302e02017-07-23 15:21:44 +0200208 eret, err := client.TestEnum(defaultCtx, thrifttest.Numberz_TWO)
Jens Geyerf4598682014-05-08 23:18:44 +0200209 if err != nil {
210 t.Fatalf("Unexpected error in TestEnum() call: ", err)
211 }
212 if eret != thrifttest.Numberz_TWO {
213 t.Fatalf("Unexpected TestEnum() result expected %#v, got %#v ", thrifttest.Numberz_TWO, eret)
214 }
215
taozle5c302e02017-07-23 15:21:44 +0200216 tret, err := client.TestTypedef(defaultCtx, thrifttest.UserId(42))
Jens Geyerf4598682014-05-08 23:18:44 +0200217 if err != nil {
218 t.Fatalf("Unexpected error in TestTypedef() call: ", err)
219 }
220 if tret != thrifttest.UserId(42) {
221 t.Fatalf("Unexpected TestTypedef() result expected %#v, got %#v ", thrifttest.UserId(42), tret)
222 }
223
taozle5c302e02017-07-23 15:21:44 +0200224 mapmap, err := client.TestMapMap(defaultCtx, 42)
Jens Geyerf4598682014-05-08 23:18:44 +0200225 if err != nil {
Nobuaki Sukegawaa649e742015-09-21 13:53:25 +0900226 t.Fatalf("Unexpected error in TestMapMap() call: ", err)
Jens Geyerf4598682014-05-08 23:18:44 +0200227 }
228 if !reflect.DeepEqual(mapmap, rmapmap) {
Nobuaki Sukegawaa649e742015-09-21 13:53:25 +0900229 t.Fatalf("Unexpected TestMapMap() result expected %#v, got %#v ", rmapmap, mapmap)
Jens Geyerf4598682014-05-08 23:18:44 +0200230 }
231
Nobuaki Sukegawa2fab3de2015-08-16 15:42:58 +0900232 crazy := thrifttest.NewInsanity()
D. Can Celasun4f77ab82017-09-21 15:21:00 +0200233 crazy.UserMap = map[thrifttest.Numberz]thrifttest.UserId{
234 thrifttest.Numberz_FIVE: 5,
Nobuaki Sukegawa2fab3de2015-08-16 15:42:58 +0900235 thrifttest.Numberz_EIGHT: 8,
236 }
237 truck1 := thrifttest.NewXtruct()
238 truck1.StringThing = "Goodbye4"
D. Can Celasun4f77ab82017-09-21 15:21:00 +0200239 truck1.ByteThing = 4
240 truck1.I32Thing = 4
241 truck1.I64Thing = 4
Nobuaki Sukegawa2fab3de2015-08-16 15:42:58 +0900242 truck2 := thrifttest.NewXtruct()
243 truck2.StringThing = "Hello2"
D. Can Celasun4f77ab82017-09-21 15:21:00 +0200244 truck2.ByteThing = 2
245 truck2.I32Thing = 2
246 truck2.I64Thing = 2
247 crazy.Xtructs = []*thrifttest.Xtruct{
Nobuaki Sukegawa2fab3de2015-08-16 15:42:58 +0900248 truck1,
249 truck2,
250 }
taozle5c302e02017-07-23 15:21:44 +0200251 insanity, err := client.TestInsanity(defaultCtx, crazy)
Nobuaki Sukegawa2fab3de2015-08-16 15:42:58 +0900252 if err != nil {
253 t.Fatalf("Unexpected error in TestInsanity() call: ", err)
254 }
255 if !reflect.DeepEqual(crazy, insanity[1][2]) {
256 t.Fatalf("Unexpected TestInsanity() first result expected %#v, got %#v ",
D. Can Celasun4f77ab82017-09-21 15:21:00 +0200257 crazy,
258 insanity[1][2])
Nobuaki Sukegawa2fab3de2015-08-16 15:42:58 +0900259 }
260 if !reflect.DeepEqual(crazy, insanity[1][3]) {
261 t.Fatalf("Unexpected TestInsanity() second result expected %#v, got %#v ",
D. Can Celasun4f77ab82017-09-21 15:21:00 +0200262 crazy,
263 insanity[1][3])
Nobuaki Sukegawa2fab3de2015-08-16 15:42:58 +0900264 }
265 if len(insanity[2][6].UserMap) > 0 || len(insanity[2][6].Xtructs) > 0 {
266 t.Fatalf("Unexpected TestInsanity() non-empty result got %#v ",
D. Can Celasun4f77ab82017-09-21 15:21:00 +0200267 insanity[2][6])
Nobuaki Sukegawa2fab3de2015-08-16 15:42:58 +0900268 }
269
taozle5c302e02017-07-23 15:21:44 +0200270 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 +0200271 if err != nil {
272 t.Fatalf("Unexpected error in TestMulti() call: ", err)
273 }
274 if !reflect.DeepEqual(xxs, xxsret) {
275 t.Fatalf("Unexpected TestMulti() result expected %#v, got %#v ", xxs, xxsret)
276 }
277
taozle5c302e02017-07-23 15:21:44 +0200278 err = client.TestException(defaultCtx, "Xception")
Jens Geyerf4598682014-05-08 23:18:44 +0200279 if err == nil {
280 t.Fatalf("Expecting exception in TestException() call")
281 }
282 if !reflect.DeepEqual(err, xcept) {
283 t.Fatalf("Unexpected TestException() result expected %#v, got %#v ", xcept, err)
284 }
285
taozle5c302e02017-07-23 15:21:44 +0200286 err = client.TestException(defaultCtx, "TException")
Nobuaki Sukegawa2fab3de2015-08-16 15:42:58 +0900287 _, ok := err.(thrift.TApplicationException)
288 if err == nil || !ok {
Jens Geyerf4598682014-05-08 23:18:44 +0200289 t.Fatalf("Unexpected TestException() result expected ApplicationError, got %#v ", err)
290 }
291
taozle5c302e02017-07-23 15:21:44 +0200292 ign, err := client.TestMultiException(defaultCtx, "Xception", "ignoreme")
Jens Geyerf4598682014-05-08 23:18:44 +0200293 if ign != nil || err == nil {
294 t.Fatalf("Expecting exception in TestMultiException() call")
295 }
296 if !reflect.DeepEqual(err, &thrifttest.Xception{ErrorCode: 1001, Message: "This is an Xception"}) {
297 t.Fatalf("Unexpected TestMultiException() %#v ", err)
298 }
299
taozle5c302e02017-07-23 15:21:44 +0200300 ign, err = client.TestMultiException(defaultCtx, "Xception2", "ignoreme")
Jens Geyerf4598682014-05-08 23:18:44 +0200301 if ign != nil || err == nil {
302 t.Fatalf("Expecting exception in TestMultiException() call")
303 }
304 expecting := &thrifttest.Xception2{ErrorCode: 2002, StructThing: &thrifttest.Xtruct{StringThing: "This is an Xception2"}}
305
306 if !reflect.DeepEqual(err, expecting) {
307 t.Fatalf("Unexpected TestMultiException() %#v ", err)
308 }
309
taozle5c302e02017-07-23 15:21:44 +0200310 err = client.TestOneway(defaultCtx, 2)
Jens Geyerf4598682014-05-08 23:18:44 +0200311 if err != nil {
312 t.Fatalf("Unexpected error in TestOneway() call: ", err)
313 }
314
315 //Make sure the connection still alive
taozle5c302e02017-07-23 15:21:44 +0200316 if err = client.TestVoid(defaultCtx); err != nil {
Jens Geyerf4598682014-05-08 23:18:44 +0200317 t.Fatalf("Unexpected error in TestVoid() call: ", err)
318 }
319}