blob: 95fcd47cc72907d46aacf970e9a5be0f2898ed16 [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")
Aki Sukegawa7594da82022-03-07 00:28:26 -050037var _ = flag.Bool("zlib", false, "For compatibility. Ignored.")
Jens Geyerf4598682014-05-08 23:18:44 +020038var protocol = flag.String("protocol", "binary", "Protocol: binary, compact, json")
39var ssl = flag.Bool("ssl", false, "Encrypted Transport using SSL")
40var 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)
Yuxuan 'fishy' Wang17373a32021-08-26 11:04:27 -0700134 if err != nil {
135 t.Fatalf("TestBinary failed with %v", err)
136 }
Nobuaki Sukegawa9b35a7c2015-11-17 11:01:41 +0900137 for i := 0; i < 256; i++ {
D. Can Celasun4f77ab82017-09-21 15:21:00 +0200138 if binout[i] != bin[i] {
Nobuaki Sukegawa9b35a7c2015-11-17 11:01:41 +0900139 t.Fatalf("Unexpected TestBinary() result expected %d, got %d ", binout[i], bin[i])
140 }
141 }
taozle5c302e02017-07-23 15:21:44 +0200142
Yuxuan 'fishy' Wang2acfe0f2022-10-21 10:27:40 -0700143 uout := thrift.Tuuid{
144 0x00, 0x11, 0x22, 0x33,
145 0x44, 0x55,
146 0x66, 0x77,
147 0x88, 0x99,
148 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
149 }
150 u, err := client.TestUuid(defaultCtx, uout)
151 if err != nil {
152 t.Fatalf("TestUuid failed with %v", err)
153 }
154 if u != uout {
155 t.Fatalf("Unexpected TestUuid() result expected %v, got %v", uout, u)
156 }
157
Jens Geyerf4598682014-05-08 23:18:44 +0200158 xs := thrifttest.NewXtruct()
159 xs.StringThing = "thing"
160 xs.ByteThing = 42
161 xs.I32Thing = 4242
162 xs.I64Thing = 424242
taozle5c302e02017-07-23 15:21:44 +0200163 xsret, err := client.TestStruct(defaultCtx, xs)
Jens Geyerf4598682014-05-08 23:18:44 +0200164 if err != nil {
165 t.Fatalf("Unexpected error in TestStruct() call: ", err)
166 }
167 if *xs != *xsret {
168 t.Fatalf("Unexpected TestStruct() result expected %#v, got %#v ", xs, xsret)
169 }
170
171 x2 := thrifttest.NewXtruct2()
172 x2.StructThing = xs
taozle5c302e02017-07-23 15:21:44 +0200173 x2ret, err := client.TestNest(defaultCtx, x2)
Jens Geyerf4598682014-05-08 23:18:44 +0200174 if err != nil {
175 t.Fatalf("Unexpected error in TestNest() call: ", err)
176 }
177 if !reflect.DeepEqual(x2, x2ret) {
178 t.Fatalf("Unexpected TestNest() result expected %#v, got %#v ", x2, x2ret)
179 }
180
181 m := map[int32]int32{1: 2, 3: 4, 5: 42}
taozle5c302e02017-07-23 15:21:44 +0200182 mret, err := client.TestMap(defaultCtx, m)
Jens Geyerf4598682014-05-08 23:18:44 +0200183 if err != nil {
184 t.Fatalf("Unexpected error in TestMap() call: ", err)
185 }
186 if !reflect.DeepEqual(m, mret) {
187 t.Fatalf("Unexpected TestMap() result expected %#v, got %#v ", m, mret)
188 }
189
190 sm := map[string]string{"a": "2", "b": "blah", "some": "thing"}
taozle5c302e02017-07-23 15:21:44 +0200191 smret, err := client.TestStringMap(defaultCtx, sm)
Jens Geyerf4598682014-05-08 23:18:44 +0200192 if err != nil {
193 t.Fatalf("Unexpected error in TestStringMap() call: ", err)
194 }
195 if !reflect.DeepEqual(sm, smret) {
196 t.Fatalf("Unexpected TestStringMap() result expected %#v, got %#v ", sm, smret)
197 }
198
D. Can Celasun43fb34d2017-01-15 10:53:19 +0100199 s := []int32{1, 2, 42}
taozle5c302e02017-07-23 15:21:44 +0200200 sret, err := client.TestSet(defaultCtx, s)
Jens Geyerf4598682014-05-08 23:18:44 +0200201 if err != nil {
202 t.Fatalf("Unexpected error in TestSet() call: ", err)
203 }
D. Can Celasun43fb34d2017-01-15 10:53:19 +0100204 // Sets can be in any order, but Go slices are ordered, so reflect.DeepEqual won't work.
205 stemp := map[int32]struct{}{}
206 for _, val := range s {
207 stemp[val] = struct{}{}
208 }
209 for _, val := range sret {
210 if _, ok := stemp[val]; !ok {
211 t.Fatalf("Unexpected TestSet() result expected %#v, got %#v ", s, sret)
212 }
Jens Geyerf4598682014-05-08 23:18:44 +0200213 }
214
215 l := []int32{1, 2, 42}
taozle5c302e02017-07-23 15:21:44 +0200216 lret, err := client.TestList(defaultCtx, l)
Jens Geyerf4598682014-05-08 23:18:44 +0200217 if err != nil {
218 t.Fatalf("Unexpected error in TestList() call: ", err)
219 }
220 if !reflect.DeepEqual(l, lret) {
D. Can Celasun43fb34d2017-01-15 10:53:19 +0100221 t.Fatalf("Unexpected TestList() result expected %#v, got %#v ", l, lret)
Jens Geyerf4598682014-05-08 23:18:44 +0200222 }
223
taozle5c302e02017-07-23 15:21:44 +0200224 eret, err := client.TestEnum(defaultCtx, thrifttest.Numberz_TWO)
Jens Geyerf4598682014-05-08 23:18:44 +0200225 if err != nil {
226 t.Fatalf("Unexpected error in TestEnum() call: ", err)
227 }
228 if eret != thrifttest.Numberz_TWO {
229 t.Fatalf("Unexpected TestEnum() result expected %#v, got %#v ", thrifttest.Numberz_TWO, eret)
230 }
231
taozle5c302e02017-07-23 15:21:44 +0200232 tret, err := client.TestTypedef(defaultCtx, thrifttest.UserId(42))
Jens Geyerf4598682014-05-08 23:18:44 +0200233 if err != nil {
234 t.Fatalf("Unexpected error in TestTypedef() call: ", err)
235 }
236 if tret != thrifttest.UserId(42) {
237 t.Fatalf("Unexpected TestTypedef() result expected %#v, got %#v ", thrifttest.UserId(42), tret)
238 }
239
taozle5c302e02017-07-23 15:21:44 +0200240 mapmap, err := client.TestMapMap(defaultCtx, 42)
Jens Geyerf4598682014-05-08 23:18:44 +0200241 if err != nil {
Nobuaki Sukegawaa649e742015-09-21 13:53:25 +0900242 t.Fatalf("Unexpected error in TestMapMap() call: ", err)
Jens Geyerf4598682014-05-08 23:18:44 +0200243 }
244 if !reflect.DeepEqual(mapmap, rmapmap) {
Nobuaki Sukegawaa649e742015-09-21 13:53:25 +0900245 t.Fatalf("Unexpected TestMapMap() result expected %#v, got %#v ", rmapmap, mapmap)
Jens Geyerf4598682014-05-08 23:18:44 +0200246 }
247
Nobuaki Sukegawa2fab3de2015-08-16 15:42:58 +0900248 crazy := thrifttest.NewInsanity()
D. Can Celasun4f77ab82017-09-21 15:21:00 +0200249 crazy.UserMap = map[thrifttest.Numberz]thrifttest.UserId{
250 thrifttest.Numberz_FIVE: 5,
Nobuaki Sukegawa2fab3de2015-08-16 15:42:58 +0900251 thrifttest.Numberz_EIGHT: 8,
252 }
253 truck1 := thrifttest.NewXtruct()
254 truck1.StringThing = "Goodbye4"
D. Can Celasun4f77ab82017-09-21 15:21:00 +0200255 truck1.ByteThing = 4
256 truck1.I32Thing = 4
257 truck1.I64Thing = 4
Nobuaki Sukegawa2fab3de2015-08-16 15:42:58 +0900258 truck2 := thrifttest.NewXtruct()
259 truck2.StringThing = "Hello2"
D. Can Celasun4f77ab82017-09-21 15:21:00 +0200260 truck2.ByteThing = 2
261 truck2.I32Thing = 2
262 truck2.I64Thing = 2
263 crazy.Xtructs = []*thrifttest.Xtruct{
Nobuaki Sukegawa2fab3de2015-08-16 15:42:58 +0900264 truck1,
265 truck2,
266 }
taozle5c302e02017-07-23 15:21:44 +0200267 insanity, err := client.TestInsanity(defaultCtx, crazy)
Nobuaki Sukegawa2fab3de2015-08-16 15:42:58 +0900268 if err != nil {
269 t.Fatalf("Unexpected error in TestInsanity() call: ", err)
270 }
271 if !reflect.DeepEqual(crazy, insanity[1][2]) {
272 t.Fatalf("Unexpected TestInsanity() first result expected %#v, got %#v ",
D. Can Celasun4f77ab82017-09-21 15:21:00 +0200273 crazy,
274 insanity[1][2])
Nobuaki Sukegawa2fab3de2015-08-16 15:42:58 +0900275 }
276 if !reflect.DeepEqual(crazy, insanity[1][3]) {
277 t.Fatalf("Unexpected TestInsanity() second result expected %#v, got %#v ",
D. Can Celasun4f77ab82017-09-21 15:21:00 +0200278 crazy,
279 insanity[1][3])
Nobuaki Sukegawa2fab3de2015-08-16 15:42:58 +0900280 }
281 if len(insanity[2][6].UserMap) > 0 || len(insanity[2][6].Xtructs) > 0 {
282 t.Fatalf("Unexpected TestInsanity() non-empty result got %#v ",
D. Can Celasun4f77ab82017-09-21 15:21:00 +0200283 insanity[2][6])
Nobuaki Sukegawa2fab3de2015-08-16 15:42:58 +0900284 }
285
taozle5c302e02017-07-23 15:21:44 +0200286 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 +0200287 if err != nil {
288 t.Fatalf("Unexpected error in TestMulti() call: ", err)
289 }
290 if !reflect.DeepEqual(xxs, xxsret) {
291 t.Fatalf("Unexpected TestMulti() result expected %#v, got %#v ", xxs, xxsret)
292 }
293
taozle5c302e02017-07-23 15:21:44 +0200294 err = client.TestException(defaultCtx, "Xception")
Jens Geyerf4598682014-05-08 23:18:44 +0200295 if err == nil {
296 t.Fatalf("Expecting exception in TestException() call")
297 }
298 if !reflect.DeepEqual(err, xcept) {
299 t.Fatalf("Unexpected TestException() result expected %#v, got %#v ", xcept, err)
300 }
301
taozle5c302e02017-07-23 15:21:44 +0200302 err = client.TestException(defaultCtx, "TException")
Nobuaki Sukegawa2fab3de2015-08-16 15:42:58 +0900303 _, ok := err.(thrift.TApplicationException)
304 if err == nil || !ok {
Jens Geyerf4598682014-05-08 23:18:44 +0200305 t.Fatalf("Unexpected TestException() result expected ApplicationError, got %#v ", err)
306 }
307
taozle5c302e02017-07-23 15:21:44 +0200308 ign, err := client.TestMultiException(defaultCtx, "Xception", "ignoreme")
Jens Geyerf4598682014-05-08 23:18:44 +0200309 if ign != nil || err == nil {
310 t.Fatalf("Expecting exception in TestMultiException() call")
311 }
312 if !reflect.DeepEqual(err, &thrifttest.Xception{ErrorCode: 1001, Message: "This is an Xception"}) {
313 t.Fatalf("Unexpected TestMultiException() %#v ", err)
314 }
315
taozle5c302e02017-07-23 15:21:44 +0200316 ign, err = client.TestMultiException(defaultCtx, "Xception2", "ignoreme")
Jens Geyerf4598682014-05-08 23:18:44 +0200317 if ign != nil || err == nil {
318 t.Fatalf("Expecting exception in TestMultiException() call")
319 }
320 expecting := &thrifttest.Xception2{ErrorCode: 2002, StructThing: &thrifttest.Xtruct{StringThing: "This is an Xception2"}}
321
322 if !reflect.DeepEqual(err, expecting) {
323 t.Fatalf("Unexpected TestMultiException() %#v ", err)
324 }
325
taozle5c302e02017-07-23 15:21:44 +0200326 err = client.TestOneway(defaultCtx, 2)
Jens Geyerf4598682014-05-08 23:18:44 +0200327 if err != nil {
328 t.Fatalf("Unexpected error in TestOneway() call: ", err)
329 }
330
331 //Make sure the connection still alive
taozle5c302e02017-07-23 15:21:44 +0200332 if err = client.TestVoid(defaultCtx); err != nil {
Jens Geyerf4598682014-05-08 23:18:44 +0200333 t.Fatalf("Unexpected error in TestVoid() call: ", err)
334 }
335}