blob: 39ee95b0d95430de8165d5da00c512d3fc4e5424 [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")
James E. King IIIb2b767e2018-09-15 20:32:04 +000039var zlib = flag.Bool("zlib", false, "Wrapped Transport using Zlib")
Jens Geyerf4598682014-05-08 23:18:44 +020040var testloops = flag.Int("testloops", 1, "Number of Tests")
41
42func main() {
43 flag.Parse()
D. Can Celasun4f77ab82017-09-21 15:21:00 +020044 client, _, err := common.StartClient(*host, *port, *domain_socket, *transport, *protocol, *ssl)
Jens Geyerf4598682014-05-08 23:18:44 +020045 if err != nil {
46 t.Fatalf("Unable to start client: ", err)
47 }
48 for i := 0; i < *testloops; i++ {
49 callEverything(client)
50 }
51}
52
53var rmapmap = map[int32]map[int32]int32{
Yuxuan 'fishy' Wangb71f11e2021-03-22 15:01:00 -070054 -4: {-4: -4, -3: -3, -2: -2, -1: -1},
55 4: {4: 4, 3: 3, 2: 2, 1: 1},
Jens Geyerf4598682014-05-08 23:18:44 +020056}
57
58var xxs = &thrifttest.Xtruct{
59 StringThing: "Hello2",
60 ByteThing: 42,
61 I32Thing: 4242,
62 I64Thing: 424242,
63}
64
65var xcept = &thrifttest.Xception{ErrorCode: 1001, Message: "Xception"}
John Boiles57852792018-01-05 14:37:05 -080066var defaultCtx = context.Background()
Jens Geyerf4598682014-05-08 23:18:44 +020067
68func callEverything(client *thrifttest.ThriftTestClient) {
69 var err error
taozle5c302e02017-07-23 15:21:44 +020070 if err = client.TestVoid(defaultCtx); err != nil {
Jens Geyerf4598682014-05-08 23:18:44 +020071 t.Fatalf("Unexpected error in TestVoid() call: ", err)
72 }
73
taozle5c302e02017-07-23 15:21:44 +020074 thing, err := client.TestString(defaultCtx, "thing")
Jens Geyerf4598682014-05-08 23:18:44 +020075 if err != nil {
76 t.Fatalf("Unexpected error in TestString() call: ", err)
77 }
78 if thing != "thing" {
79 t.Fatalf("Unexpected TestString() result, expected 'thing' got '%s' ", thing)
80 }
81
taozle5c302e02017-07-23 15:21:44 +020082 bl, err := client.TestBool(defaultCtx, true)
Nobuaki Sukegawaa649e742015-09-21 13:53:25 +090083 if err != nil {
84 t.Fatalf("Unexpected error in TestBool() call: ", err)
85 }
86 if !bl {
87 t.Fatalf("Unexpected TestBool() result expected true, got %f ", bl)
88 }
taozle5c302e02017-07-23 15:21:44 +020089 bl, err = client.TestBool(defaultCtx, false)
Nobuaki Sukegawaa649e742015-09-21 13:53:25 +090090 if err != nil {
91 t.Fatalf("Unexpected error in TestBool() call: ", err)
92 }
93 if bl {
94 t.Fatalf("Unexpected TestBool() result expected false, got %f ", bl)
95 }
96
taozle5c302e02017-07-23 15:21:44 +020097 b, err := client.TestByte(defaultCtx, 42)
Jens Geyerf4598682014-05-08 23:18:44 +020098 if err != nil {
99 t.Fatalf("Unexpected error in TestByte() call: ", err)
100 }
101 if b != 42 {
102 t.Fatalf("Unexpected TestByte() result expected 42, got %d ", b)
103 }
104
taozle5c302e02017-07-23 15:21:44 +0200105 i32, err := client.TestI32(defaultCtx, 4242)
Jens Geyerf4598682014-05-08 23:18:44 +0200106 if err != nil {
107 t.Fatalf("Unexpected error in TestI32() call: ", err)
108 }
109 if i32 != 4242 {
110 t.Fatalf("Unexpected TestI32() result expected 4242, got %d ", i32)
111 }
112
taozle5c302e02017-07-23 15:21:44 +0200113 i64, err := client.TestI64(defaultCtx, 424242)
Jens Geyerf4598682014-05-08 23:18:44 +0200114 if err != nil {
115 t.Fatalf("Unexpected error in TestI64() call: ", err)
116 }
117 if i64 != 424242 {
118 t.Fatalf("Unexpected TestI64() result expected 424242, got %d ", i64)
119 }
120
taozle5c302e02017-07-23 15:21:44 +0200121 d, err := client.TestDouble(defaultCtx, 42.42)
Jens Geyerf4598682014-05-08 23:18:44 +0200122 if err != nil {
123 t.Fatalf("Unexpected error in TestDouble() call: ", err)
124 }
125 if d != 42.42 {
126 t.Fatalf("Unexpected TestDouble() result expected 42.42, got %f ", d)
127 }
128
Nobuaki Sukegawa9b35a7c2015-11-17 11:01:41 +0900129 binout := make([]byte, 256)
130 for i := 0; i < 256; i++ {
131 binout[i] = byte(i)
132 }
taozle5c302e02017-07-23 15:21:44 +0200133 bin, err := client.TestBinary(defaultCtx, binout)
Nobuaki Sukegawa9b35a7c2015-11-17 11:01:41 +0900134 for i := 0; i < 256; i++ {
D. Can Celasun4f77ab82017-09-21 15:21:00 +0200135 if binout[i] != bin[i] {
Nobuaki Sukegawa9b35a7c2015-11-17 11:01:41 +0900136 t.Fatalf("Unexpected TestBinary() result expected %d, got %d ", binout[i], bin[i])
137 }
138 }
taozle5c302e02017-07-23 15:21:44 +0200139
Jens Geyerf4598682014-05-08 23:18:44 +0200140 xs := thrifttest.NewXtruct()
141 xs.StringThing = "thing"
142 xs.ByteThing = 42
143 xs.I32Thing = 4242
144 xs.I64Thing = 424242
taozle5c302e02017-07-23 15:21:44 +0200145 xsret, err := client.TestStruct(defaultCtx, xs)
Jens Geyerf4598682014-05-08 23:18:44 +0200146 if err != nil {
147 t.Fatalf("Unexpected error in TestStruct() call: ", err)
148 }
149 if *xs != *xsret {
150 t.Fatalf("Unexpected TestStruct() result expected %#v, got %#v ", xs, xsret)
151 }
152
153 x2 := thrifttest.NewXtruct2()
154 x2.StructThing = xs
taozle5c302e02017-07-23 15:21:44 +0200155 x2ret, err := client.TestNest(defaultCtx, x2)
Jens Geyerf4598682014-05-08 23:18:44 +0200156 if err != nil {
157 t.Fatalf("Unexpected error in TestNest() call: ", err)
158 }
159 if !reflect.DeepEqual(x2, x2ret) {
160 t.Fatalf("Unexpected TestNest() result expected %#v, got %#v ", x2, x2ret)
161 }
162
163 m := map[int32]int32{1: 2, 3: 4, 5: 42}
taozle5c302e02017-07-23 15:21:44 +0200164 mret, err := client.TestMap(defaultCtx, m)
Jens Geyerf4598682014-05-08 23:18:44 +0200165 if err != nil {
166 t.Fatalf("Unexpected error in TestMap() call: ", err)
167 }
168 if !reflect.DeepEqual(m, mret) {
169 t.Fatalf("Unexpected TestMap() result expected %#v, got %#v ", m, mret)
170 }
171
172 sm := map[string]string{"a": "2", "b": "blah", "some": "thing"}
taozle5c302e02017-07-23 15:21:44 +0200173 smret, err := client.TestStringMap(defaultCtx, sm)
Jens Geyerf4598682014-05-08 23:18:44 +0200174 if err != nil {
175 t.Fatalf("Unexpected error in TestStringMap() call: ", err)
176 }
177 if !reflect.DeepEqual(sm, smret) {
178 t.Fatalf("Unexpected TestStringMap() result expected %#v, got %#v ", sm, smret)
179 }
180
D. Can Celasun43fb34d2017-01-15 10:53:19 +0100181 s := []int32{1, 2, 42}
taozle5c302e02017-07-23 15:21:44 +0200182 sret, err := client.TestSet(defaultCtx, s)
Jens Geyerf4598682014-05-08 23:18:44 +0200183 if err != nil {
184 t.Fatalf("Unexpected error in TestSet() call: ", err)
185 }
D. Can Celasun43fb34d2017-01-15 10:53:19 +0100186 // Sets can be in any order, but Go slices are ordered, so reflect.DeepEqual won't work.
187 stemp := map[int32]struct{}{}
188 for _, val := range s {
189 stemp[val] = struct{}{}
190 }
191 for _, val := range sret {
192 if _, ok := stemp[val]; !ok {
193 t.Fatalf("Unexpected TestSet() result expected %#v, got %#v ", s, sret)
194 }
Jens Geyerf4598682014-05-08 23:18:44 +0200195 }
196
197 l := []int32{1, 2, 42}
taozle5c302e02017-07-23 15:21:44 +0200198 lret, err := client.TestList(defaultCtx, l)
Jens Geyerf4598682014-05-08 23:18:44 +0200199 if err != nil {
200 t.Fatalf("Unexpected error in TestList() call: ", err)
201 }
202 if !reflect.DeepEqual(l, lret) {
D. Can Celasun43fb34d2017-01-15 10:53:19 +0100203 t.Fatalf("Unexpected TestList() result expected %#v, got %#v ", l, lret)
Jens Geyerf4598682014-05-08 23:18:44 +0200204 }
205
taozle5c302e02017-07-23 15:21:44 +0200206 eret, err := client.TestEnum(defaultCtx, thrifttest.Numberz_TWO)
Jens Geyerf4598682014-05-08 23:18:44 +0200207 if err != nil {
208 t.Fatalf("Unexpected error in TestEnum() call: ", err)
209 }
210 if eret != thrifttest.Numberz_TWO {
211 t.Fatalf("Unexpected TestEnum() result expected %#v, got %#v ", thrifttest.Numberz_TWO, eret)
212 }
213
taozle5c302e02017-07-23 15:21:44 +0200214 tret, err := client.TestTypedef(defaultCtx, thrifttest.UserId(42))
Jens Geyerf4598682014-05-08 23:18:44 +0200215 if err != nil {
216 t.Fatalf("Unexpected error in TestTypedef() call: ", err)
217 }
218 if tret != thrifttest.UserId(42) {
219 t.Fatalf("Unexpected TestTypedef() result expected %#v, got %#v ", thrifttest.UserId(42), tret)
220 }
221
taozle5c302e02017-07-23 15:21:44 +0200222 mapmap, err := client.TestMapMap(defaultCtx, 42)
Jens Geyerf4598682014-05-08 23:18:44 +0200223 if err != nil {
Nobuaki Sukegawaa649e742015-09-21 13:53:25 +0900224 t.Fatalf("Unexpected error in TestMapMap() call: ", err)
Jens Geyerf4598682014-05-08 23:18:44 +0200225 }
226 if !reflect.DeepEqual(mapmap, rmapmap) {
Nobuaki Sukegawaa649e742015-09-21 13:53:25 +0900227 t.Fatalf("Unexpected TestMapMap() result expected %#v, got %#v ", rmapmap, mapmap)
Jens Geyerf4598682014-05-08 23:18:44 +0200228 }
229
Nobuaki Sukegawa2fab3de2015-08-16 15:42:58 +0900230 crazy := thrifttest.NewInsanity()
D. Can Celasun4f77ab82017-09-21 15:21:00 +0200231 crazy.UserMap = map[thrifttest.Numberz]thrifttest.UserId{
232 thrifttest.Numberz_FIVE: 5,
Nobuaki Sukegawa2fab3de2015-08-16 15:42:58 +0900233 thrifttest.Numberz_EIGHT: 8,
234 }
235 truck1 := thrifttest.NewXtruct()
236 truck1.StringThing = "Goodbye4"
D. Can Celasun4f77ab82017-09-21 15:21:00 +0200237 truck1.ByteThing = 4
238 truck1.I32Thing = 4
239 truck1.I64Thing = 4
Nobuaki Sukegawa2fab3de2015-08-16 15:42:58 +0900240 truck2 := thrifttest.NewXtruct()
241 truck2.StringThing = "Hello2"
D. Can Celasun4f77ab82017-09-21 15:21:00 +0200242 truck2.ByteThing = 2
243 truck2.I32Thing = 2
244 truck2.I64Thing = 2
245 crazy.Xtructs = []*thrifttest.Xtruct{
Nobuaki Sukegawa2fab3de2015-08-16 15:42:58 +0900246 truck1,
247 truck2,
248 }
taozle5c302e02017-07-23 15:21:44 +0200249 insanity, err := client.TestInsanity(defaultCtx, crazy)
Nobuaki Sukegawa2fab3de2015-08-16 15:42:58 +0900250 if err != nil {
251 t.Fatalf("Unexpected error in TestInsanity() call: ", err)
252 }
253 if !reflect.DeepEqual(crazy, insanity[1][2]) {
254 t.Fatalf("Unexpected TestInsanity() first result expected %#v, got %#v ",
D. Can Celasun4f77ab82017-09-21 15:21:00 +0200255 crazy,
256 insanity[1][2])
Nobuaki Sukegawa2fab3de2015-08-16 15:42:58 +0900257 }
258 if !reflect.DeepEqual(crazy, insanity[1][3]) {
259 t.Fatalf("Unexpected TestInsanity() second result expected %#v, got %#v ",
D. Can Celasun4f77ab82017-09-21 15:21:00 +0200260 crazy,
261 insanity[1][3])
Nobuaki Sukegawa2fab3de2015-08-16 15:42:58 +0900262 }
263 if len(insanity[2][6].UserMap) > 0 || len(insanity[2][6].Xtructs) > 0 {
264 t.Fatalf("Unexpected TestInsanity() non-empty result got %#v ",
D. Can Celasun4f77ab82017-09-21 15:21:00 +0200265 insanity[2][6])
Nobuaki Sukegawa2fab3de2015-08-16 15:42:58 +0900266 }
267
taozle5c302e02017-07-23 15:21:44 +0200268 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 +0200269 if err != nil {
270 t.Fatalf("Unexpected error in TestMulti() call: ", err)
271 }
272 if !reflect.DeepEqual(xxs, xxsret) {
273 t.Fatalf("Unexpected TestMulti() result expected %#v, got %#v ", xxs, xxsret)
274 }
275
taozle5c302e02017-07-23 15:21:44 +0200276 err = client.TestException(defaultCtx, "Xception")
Jens Geyerf4598682014-05-08 23:18:44 +0200277 if err == nil {
278 t.Fatalf("Expecting exception in TestException() call")
279 }
280 if !reflect.DeepEqual(err, xcept) {
281 t.Fatalf("Unexpected TestException() result expected %#v, got %#v ", xcept, err)
282 }
283
taozle5c302e02017-07-23 15:21:44 +0200284 err = client.TestException(defaultCtx, "TException")
Nobuaki Sukegawa2fab3de2015-08-16 15:42:58 +0900285 _, ok := err.(thrift.TApplicationException)
286 if err == nil || !ok {
Jens Geyerf4598682014-05-08 23:18:44 +0200287 t.Fatalf("Unexpected TestException() result expected ApplicationError, got %#v ", err)
288 }
289
taozle5c302e02017-07-23 15:21:44 +0200290 ign, err := client.TestMultiException(defaultCtx, "Xception", "ignoreme")
Jens Geyerf4598682014-05-08 23:18:44 +0200291 if ign != nil || err == nil {
292 t.Fatalf("Expecting exception in TestMultiException() call")
293 }
294 if !reflect.DeepEqual(err, &thrifttest.Xception{ErrorCode: 1001, Message: "This is an Xception"}) {
295 t.Fatalf("Unexpected TestMultiException() %#v ", err)
296 }
297
taozle5c302e02017-07-23 15:21:44 +0200298 ign, err = client.TestMultiException(defaultCtx, "Xception2", "ignoreme")
Jens Geyerf4598682014-05-08 23:18:44 +0200299 if ign != nil || err == nil {
300 t.Fatalf("Expecting exception in TestMultiException() call")
301 }
302 expecting := &thrifttest.Xception2{ErrorCode: 2002, StructThing: &thrifttest.Xtruct{StringThing: "This is an Xception2"}}
303
304 if !reflect.DeepEqual(err, expecting) {
305 t.Fatalf("Unexpected TestMultiException() %#v ", err)
306 }
307
taozle5c302e02017-07-23 15:21:44 +0200308 err = client.TestOneway(defaultCtx, 2)
Jens Geyerf4598682014-05-08 23:18:44 +0200309 if err != nil {
310 t.Fatalf("Unexpected error in TestOneway() call: ", err)
311 }
312
313 //Make sure the connection still alive
taozle5c302e02017-07-23 15:21:44 +0200314 if err = client.TestVoid(defaultCtx); err != nil {
Jens Geyerf4598682014-05-08 23:18:44 +0200315 t.Fatalf("Unexpected error in TestVoid() call: ", err)
316 }
317}