blob: a71223ca602ed153b0d968055ede754765986faf [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 (
Yuxuan 'fishy' Wang91565d42024-08-14 09:01:15 -070023 "bytes"
John Boiles57852792018-01-05 14:37:05 -080024 "context"
Jens Geyerf4598682014-05-08 23:18:44 +020025 "flag"
Yuxuan 'fishy' Wang91565d42024-08-14 09:01:15 -070026 "fmt"
Jens Geyerf4598682014-05-08 23:18:44 +020027 t "log"
28 "reflect"
Yuxuan 'fishy' Wangb71f11e2021-03-22 15:01:00 -070029
30 "github.com/apache/thrift/lib/go/thrift"
31 "github.com/apache/thrift/test/go/src/common"
32 "github.com/apache/thrift/test/go/src/gen/thrifttest"
Jens Geyerf4598682014-05-08 23:18:44 +020033)
34
35var host = flag.String("host", "localhost", "Host to connect")
36var port = flag.Int64("port", 9090, "Port number to connect")
37var 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 +020038var transport = flag.String("transport", "buffered", "Transport: buffered, framed, http, zlib")
Aki Sukegawa7594da82022-03-07 00:28:26 -050039var _ = flag.Bool("zlib", false, "For compatibility. Ignored.")
Jens Geyerf4598682014-05-08 23:18:44 +020040var protocol = flag.String("protocol", "binary", "Protocol: binary, compact, json")
41var ssl = flag.Bool("ssl", false, "Encrypted Transport using SSL")
42var testloops = flag.Int("testloops", 1, "Number of Tests")
43
44func main() {
45 flag.Parse()
Yuxuan 'fishy' Wang91565d42024-08-14 09:01:15 -070046 addr := *domain_socket
47 if addr == "" {
48 addr = fmt.Sprintf("%s:%d", *host, *port)
49 }
50 client, _, err := common.StartClient(addr, *transport, *protocol, *ssl)
Jens Geyerf4598682014-05-08 23:18:44 +020051 if err != nil {
52 t.Fatalf("Unable to start client: ", err)
53 }
Yuxuan 'fishy' Wang91565d42024-08-14 09:01:15 -070054 for range *testloops {
Jens Geyerf4598682014-05-08 23:18:44 +020055 callEverything(client)
56 }
57}
58
59var rmapmap = map[int32]map[int32]int32{
Yuxuan 'fishy' Wangb71f11e2021-03-22 15:01:00 -070060 -4: {-4: -4, -3: -3, -2: -2, -1: -1},
61 4: {4: 4, 3: 3, 2: 2, 1: 1},
Jens Geyerf4598682014-05-08 23:18:44 +020062}
63
64var xxs = &thrifttest.Xtruct{
65 StringThing: "Hello2",
66 ByteThing: 42,
67 I32Thing: 4242,
68 I64Thing: 424242,
69}
70
71var xcept = &thrifttest.Xception{ErrorCode: 1001, Message: "Xception"}
John Boiles57852792018-01-05 14:37:05 -080072var defaultCtx = context.Background()
Jens Geyerf4598682014-05-08 23:18:44 +020073
74func callEverything(client *thrifttest.ThriftTestClient) {
75 var err error
taozle5c302e02017-07-23 15:21:44 +020076 if err = client.TestVoid(defaultCtx); err != nil {
Jens Geyerf4598682014-05-08 23:18:44 +020077 t.Fatalf("Unexpected error in TestVoid() call: ", err)
78 }
79
taozle5c302e02017-07-23 15:21:44 +020080 thing, err := client.TestString(defaultCtx, "thing")
Jens Geyerf4598682014-05-08 23:18:44 +020081 if err != nil {
82 t.Fatalf("Unexpected error in TestString() call: ", err)
83 }
84 if thing != "thing" {
85 t.Fatalf("Unexpected TestString() result, expected 'thing' got '%s' ", thing)
86 }
87
taozle5c302e02017-07-23 15:21:44 +020088 bl, err := client.TestBool(defaultCtx, true)
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 true, got %f ", bl)
94 }
taozle5c302e02017-07-23 15:21:44 +020095 bl, err = client.TestBool(defaultCtx, false)
Nobuaki Sukegawaa649e742015-09-21 13:53:25 +090096 if err != nil {
97 t.Fatalf("Unexpected error in TestBool() call: ", err)
98 }
99 if bl {
100 t.Fatalf("Unexpected TestBool() result expected false, got %f ", bl)
101 }
102
taozle5c302e02017-07-23 15:21:44 +0200103 b, err := client.TestByte(defaultCtx, 42)
Jens Geyerf4598682014-05-08 23:18:44 +0200104 if err != nil {
105 t.Fatalf("Unexpected error in TestByte() call: ", err)
106 }
107 if b != 42 {
108 t.Fatalf("Unexpected TestByte() result expected 42, got %d ", b)
109 }
110
taozle5c302e02017-07-23 15:21:44 +0200111 i32, err := client.TestI32(defaultCtx, 4242)
Jens Geyerf4598682014-05-08 23:18:44 +0200112 if err != nil {
113 t.Fatalf("Unexpected error in TestI32() call: ", err)
114 }
115 if i32 != 4242 {
116 t.Fatalf("Unexpected TestI32() result expected 4242, got %d ", i32)
117 }
118
taozle5c302e02017-07-23 15:21:44 +0200119 i64, err := client.TestI64(defaultCtx, 424242)
Jens Geyerf4598682014-05-08 23:18:44 +0200120 if err != nil {
121 t.Fatalf("Unexpected error in TestI64() call: ", err)
122 }
123 if i64 != 424242 {
124 t.Fatalf("Unexpected TestI64() result expected 424242, got %d ", i64)
125 }
126
taozle5c302e02017-07-23 15:21:44 +0200127 d, err := client.TestDouble(defaultCtx, 42.42)
Jens Geyerf4598682014-05-08 23:18:44 +0200128 if err != nil {
129 t.Fatalf("Unexpected error in TestDouble() call: ", err)
130 }
131 if d != 42.42 {
132 t.Fatalf("Unexpected TestDouble() result expected 42.42, got %f ", d)
133 }
134
Nobuaki Sukegawa9b35a7c2015-11-17 11:01:41 +0900135 binout := make([]byte, 256)
Yuxuan 'fishy' Wang91565d42024-08-14 09:01:15 -0700136 for i := range binout {
Nobuaki Sukegawa9b35a7c2015-11-17 11:01:41 +0900137 binout[i] = byte(i)
138 }
taozle5c302e02017-07-23 15:21:44 +0200139 bin, err := client.TestBinary(defaultCtx, binout)
Yuxuan 'fishy' Wang17373a32021-08-26 11:04:27 -0700140 if err != nil {
141 t.Fatalf("TestBinary failed with %v", err)
142 }
Yuxuan 'fishy' Wang91565d42024-08-14 09:01:15 -0700143 if !bytes.Equal(binout, bin) {
144 t.Fatalf("Unexpected TestBinary() result expected % 02x, got % 02x ", binout, bin)
Nobuaki Sukegawa9b35a7c2015-11-17 11:01:41 +0900145 }
taozle5c302e02017-07-23 15:21:44 +0200146
Yuxuan 'fishy' Wang2acfe0f2022-10-21 10:27:40 -0700147 uout := thrift.Tuuid{
148 0x00, 0x11, 0x22, 0x33,
149 0x44, 0x55,
150 0x66, 0x77,
151 0x88, 0x99,
152 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
153 }
154 u, err := client.TestUuid(defaultCtx, uout)
155 if err != nil {
156 t.Fatalf("TestUuid failed with %v", err)
157 }
158 if u != uout {
159 t.Fatalf("Unexpected TestUuid() result expected %v, got %v", uout, u)
160 }
161
Jens Geyerf4598682014-05-08 23:18:44 +0200162 xs := thrifttest.NewXtruct()
163 xs.StringThing = "thing"
164 xs.ByteThing = 42
165 xs.I32Thing = 4242
166 xs.I64Thing = 424242
taozle5c302e02017-07-23 15:21:44 +0200167 xsret, err := client.TestStruct(defaultCtx, xs)
Jens Geyerf4598682014-05-08 23:18:44 +0200168 if err != nil {
169 t.Fatalf("Unexpected error in TestStruct() call: ", err)
170 }
171 if *xs != *xsret {
172 t.Fatalf("Unexpected TestStruct() result expected %#v, got %#v ", xs, xsret)
173 }
174
175 x2 := thrifttest.NewXtruct2()
176 x2.StructThing = xs
taozle5c302e02017-07-23 15:21:44 +0200177 x2ret, err := client.TestNest(defaultCtx, x2)
Jens Geyerf4598682014-05-08 23:18:44 +0200178 if err != nil {
179 t.Fatalf("Unexpected error in TestNest() call: ", err)
180 }
181 if !reflect.DeepEqual(x2, x2ret) {
182 t.Fatalf("Unexpected TestNest() result expected %#v, got %#v ", x2, x2ret)
183 }
184
185 m := map[int32]int32{1: 2, 3: 4, 5: 42}
taozle5c302e02017-07-23 15:21:44 +0200186 mret, err := client.TestMap(defaultCtx, m)
Jens Geyerf4598682014-05-08 23:18:44 +0200187 if err != nil {
188 t.Fatalf("Unexpected error in TestMap() call: ", err)
189 }
190 if !reflect.DeepEqual(m, mret) {
191 t.Fatalf("Unexpected TestMap() result expected %#v, got %#v ", m, mret)
192 }
193
194 sm := map[string]string{"a": "2", "b": "blah", "some": "thing"}
taozle5c302e02017-07-23 15:21:44 +0200195 smret, err := client.TestStringMap(defaultCtx, sm)
Jens Geyerf4598682014-05-08 23:18:44 +0200196 if err != nil {
197 t.Fatalf("Unexpected error in TestStringMap() call: ", err)
198 }
199 if !reflect.DeepEqual(sm, smret) {
200 t.Fatalf("Unexpected TestStringMap() result expected %#v, got %#v ", sm, smret)
201 }
202
D. Can Celasun43fb34d2017-01-15 10:53:19 +0100203 s := []int32{1, 2, 42}
taozle5c302e02017-07-23 15:21:44 +0200204 sret, err := client.TestSet(defaultCtx, s)
Jens Geyerf4598682014-05-08 23:18:44 +0200205 if err != nil {
206 t.Fatalf("Unexpected error in TestSet() call: ", err)
207 }
D. Can Celasun43fb34d2017-01-15 10:53:19 +0100208 // Sets can be in any order, but Go slices are ordered, so reflect.DeepEqual won't work.
209 stemp := map[int32]struct{}{}
210 for _, val := range s {
211 stemp[val] = struct{}{}
212 }
213 for _, val := range sret {
214 if _, ok := stemp[val]; !ok {
215 t.Fatalf("Unexpected TestSet() result expected %#v, got %#v ", s, sret)
216 }
Jens Geyerf4598682014-05-08 23:18:44 +0200217 }
218
219 l := []int32{1, 2, 42}
taozle5c302e02017-07-23 15:21:44 +0200220 lret, err := client.TestList(defaultCtx, l)
Jens Geyerf4598682014-05-08 23:18:44 +0200221 if err != nil {
222 t.Fatalf("Unexpected error in TestList() call: ", err)
223 }
224 if !reflect.DeepEqual(l, lret) {
D. Can Celasun43fb34d2017-01-15 10:53:19 +0100225 t.Fatalf("Unexpected TestList() result expected %#v, got %#v ", l, lret)
Jens Geyerf4598682014-05-08 23:18:44 +0200226 }
227
taozle5c302e02017-07-23 15:21:44 +0200228 eret, err := client.TestEnum(defaultCtx, thrifttest.Numberz_TWO)
Jens Geyerf4598682014-05-08 23:18:44 +0200229 if err != nil {
230 t.Fatalf("Unexpected error in TestEnum() call: ", err)
231 }
232 if eret != thrifttest.Numberz_TWO {
233 t.Fatalf("Unexpected TestEnum() result expected %#v, got %#v ", thrifttest.Numberz_TWO, eret)
234 }
235
taozle5c302e02017-07-23 15:21:44 +0200236 tret, err := client.TestTypedef(defaultCtx, thrifttest.UserId(42))
Jens Geyerf4598682014-05-08 23:18:44 +0200237 if err != nil {
238 t.Fatalf("Unexpected error in TestTypedef() call: ", err)
239 }
240 if tret != thrifttest.UserId(42) {
241 t.Fatalf("Unexpected TestTypedef() result expected %#v, got %#v ", thrifttest.UserId(42), tret)
242 }
243
taozle5c302e02017-07-23 15:21:44 +0200244 mapmap, err := client.TestMapMap(defaultCtx, 42)
Jens Geyerf4598682014-05-08 23:18:44 +0200245 if err != nil {
Nobuaki Sukegawaa649e742015-09-21 13:53:25 +0900246 t.Fatalf("Unexpected error in TestMapMap() call: ", err)
Jens Geyerf4598682014-05-08 23:18:44 +0200247 }
248 if !reflect.DeepEqual(mapmap, rmapmap) {
Nobuaki Sukegawaa649e742015-09-21 13:53:25 +0900249 t.Fatalf("Unexpected TestMapMap() result expected %#v, got %#v ", rmapmap, mapmap)
Jens Geyerf4598682014-05-08 23:18:44 +0200250 }
251
Nobuaki Sukegawa2fab3de2015-08-16 15:42:58 +0900252 crazy := thrifttest.NewInsanity()
D. Can Celasun4f77ab82017-09-21 15:21:00 +0200253 crazy.UserMap = map[thrifttest.Numberz]thrifttest.UserId{
254 thrifttest.Numberz_FIVE: 5,
Nobuaki Sukegawa2fab3de2015-08-16 15:42:58 +0900255 thrifttest.Numberz_EIGHT: 8,
256 }
257 truck1 := thrifttest.NewXtruct()
258 truck1.StringThing = "Goodbye4"
D. Can Celasun4f77ab82017-09-21 15:21:00 +0200259 truck1.ByteThing = 4
260 truck1.I32Thing = 4
261 truck1.I64Thing = 4
Nobuaki Sukegawa2fab3de2015-08-16 15:42:58 +0900262 truck2 := thrifttest.NewXtruct()
263 truck2.StringThing = "Hello2"
D. Can Celasun4f77ab82017-09-21 15:21:00 +0200264 truck2.ByteThing = 2
265 truck2.I32Thing = 2
266 truck2.I64Thing = 2
267 crazy.Xtructs = []*thrifttest.Xtruct{
Nobuaki Sukegawa2fab3de2015-08-16 15:42:58 +0900268 truck1,
269 truck2,
270 }
taozle5c302e02017-07-23 15:21:44 +0200271 insanity, err := client.TestInsanity(defaultCtx, crazy)
Nobuaki Sukegawa2fab3de2015-08-16 15:42:58 +0900272 if err != nil {
273 t.Fatalf("Unexpected error in TestInsanity() call: ", err)
274 }
275 if !reflect.DeepEqual(crazy, insanity[1][2]) {
276 t.Fatalf("Unexpected TestInsanity() first result expected %#v, got %#v ",
D. Can Celasun4f77ab82017-09-21 15:21:00 +0200277 crazy,
278 insanity[1][2])
Nobuaki Sukegawa2fab3de2015-08-16 15:42:58 +0900279 }
280 if !reflect.DeepEqual(crazy, insanity[1][3]) {
281 t.Fatalf("Unexpected TestInsanity() second result expected %#v, got %#v ",
D. Can Celasun4f77ab82017-09-21 15:21:00 +0200282 crazy,
283 insanity[1][3])
Nobuaki Sukegawa2fab3de2015-08-16 15:42:58 +0900284 }
285 if len(insanity[2][6].UserMap) > 0 || len(insanity[2][6].Xtructs) > 0 {
286 t.Fatalf("Unexpected TestInsanity() non-empty result got %#v ",
D. Can Celasun4f77ab82017-09-21 15:21:00 +0200287 insanity[2][6])
Nobuaki Sukegawa2fab3de2015-08-16 15:42:58 +0900288 }
289
taozle5c302e02017-07-23 15:21:44 +0200290 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 +0200291 if err != nil {
292 t.Fatalf("Unexpected error in TestMulti() call: ", err)
293 }
294 if !reflect.DeepEqual(xxs, xxsret) {
295 t.Fatalf("Unexpected TestMulti() result expected %#v, got %#v ", xxs, xxsret)
296 }
297
taozle5c302e02017-07-23 15:21:44 +0200298 err = client.TestException(defaultCtx, "Xception")
Jens Geyerf4598682014-05-08 23:18:44 +0200299 if err == nil {
300 t.Fatalf("Expecting exception in TestException() call")
301 }
302 if !reflect.DeepEqual(err, xcept) {
303 t.Fatalf("Unexpected TestException() result expected %#v, got %#v ", xcept, err)
304 }
305
taozle5c302e02017-07-23 15:21:44 +0200306 err = client.TestException(defaultCtx, "TException")
Nobuaki Sukegawa2fab3de2015-08-16 15:42:58 +0900307 _, ok := err.(thrift.TApplicationException)
308 if err == nil || !ok {
Jens Geyerf4598682014-05-08 23:18:44 +0200309 t.Fatalf("Unexpected TestException() result expected ApplicationError, got %#v ", err)
310 }
311
taozle5c302e02017-07-23 15:21:44 +0200312 ign, err := client.TestMultiException(defaultCtx, "Xception", "ignoreme")
Jens Geyerf4598682014-05-08 23:18:44 +0200313 if ign != nil || err == nil {
314 t.Fatalf("Expecting exception in TestMultiException() call")
315 }
316 if !reflect.DeepEqual(err, &thrifttest.Xception{ErrorCode: 1001, Message: "This is an Xception"}) {
317 t.Fatalf("Unexpected TestMultiException() %#v ", err)
318 }
319
taozle5c302e02017-07-23 15:21:44 +0200320 ign, err = client.TestMultiException(defaultCtx, "Xception2", "ignoreme")
Jens Geyerf4598682014-05-08 23:18:44 +0200321 if ign != nil || err == nil {
322 t.Fatalf("Expecting exception in TestMultiException() call")
323 }
324 expecting := &thrifttest.Xception2{ErrorCode: 2002, StructThing: &thrifttest.Xtruct{StringThing: "This is an Xception2"}}
325
326 if !reflect.DeepEqual(err, expecting) {
327 t.Fatalf("Unexpected TestMultiException() %#v ", err)
328 }
329
taozle5c302e02017-07-23 15:21:44 +0200330 err = client.TestOneway(defaultCtx, 2)
Jens Geyerf4598682014-05-08 23:18:44 +0200331 if err != nil {
332 t.Fatalf("Unexpected error in TestOneway() call: ", err)
333 }
334
335 //Make sure the connection still alive
taozle5c302e02017-07-23 15:21:44 +0200336 if err = client.TestVoid(defaultCtx); err != nil {
Jens Geyerf4598682014-05-08 23:18:44 +0200337 t.Fatalf("Unexpected error in TestVoid() call: ", err)
338 }
339}