blob: b7119589edb12d616f94edf0413fc7b766393feb [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
Jens Geyerf4598682014-05-08 23:18:44 +0200143 xs := thrifttest.NewXtruct()
144 xs.StringThing = "thing"
145 xs.ByteThing = 42
146 xs.I32Thing = 4242
147 xs.I64Thing = 424242
taozle5c302e02017-07-23 15:21:44 +0200148 xsret, err := client.TestStruct(defaultCtx, xs)
Jens Geyerf4598682014-05-08 23:18:44 +0200149 if err != nil {
150 t.Fatalf("Unexpected error in TestStruct() call: ", err)
151 }
152 if *xs != *xsret {
153 t.Fatalf("Unexpected TestStruct() result expected %#v, got %#v ", xs, xsret)
154 }
155
156 x2 := thrifttest.NewXtruct2()
157 x2.StructThing = xs
taozle5c302e02017-07-23 15:21:44 +0200158 x2ret, err := client.TestNest(defaultCtx, x2)
Jens Geyerf4598682014-05-08 23:18:44 +0200159 if err != nil {
160 t.Fatalf("Unexpected error in TestNest() call: ", err)
161 }
162 if !reflect.DeepEqual(x2, x2ret) {
163 t.Fatalf("Unexpected TestNest() result expected %#v, got %#v ", x2, x2ret)
164 }
165
166 m := map[int32]int32{1: 2, 3: 4, 5: 42}
taozle5c302e02017-07-23 15:21:44 +0200167 mret, err := client.TestMap(defaultCtx, m)
Jens Geyerf4598682014-05-08 23:18:44 +0200168 if err != nil {
169 t.Fatalf("Unexpected error in TestMap() call: ", err)
170 }
171 if !reflect.DeepEqual(m, mret) {
172 t.Fatalf("Unexpected TestMap() result expected %#v, got %#v ", m, mret)
173 }
174
175 sm := map[string]string{"a": "2", "b": "blah", "some": "thing"}
taozle5c302e02017-07-23 15:21:44 +0200176 smret, err := client.TestStringMap(defaultCtx, sm)
Jens Geyerf4598682014-05-08 23:18:44 +0200177 if err != nil {
178 t.Fatalf("Unexpected error in TestStringMap() call: ", err)
179 }
180 if !reflect.DeepEqual(sm, smret) {
181 t.Fatalf("Unexpected TestStringMap() result expected %#v, got %#v ", sm, smret)
182 }
183
D. Can Celasun43fb34d2017-01-15 10:53:19 +0100184 s := []int32{1, 2, 42}
taozle5c302e02017-07-23 15:21:44 +0200185 sret, err := client.TestSet(defaultCtx, s)
Jens Geyerf4598682014-05-08 23:18:44 +0200186 if err != nil {
187 t.Fatalf("Unexpected error in TestSet() call: ", err)
188 }
D. Can Celasun43fb34d2017-01-15 10:53:19 +0100189 // Sets can be in any order, but Go slices are ordered, so reflect.DeepEqual won't work.
190 stemp := map[int32]struct{}{}
191 for _, val := range s {
192 stemp[val] = struct{}{}
193 }
194 for _, val := range sret {
195 if _, ok := stemp[val]; !ok {
196 t.Fatalf("Unexpected TestSet() result expected %#v, got %#v ", s, sret)
197 }
Jens Geyerf4598682014-05-08 23:18:44 +0200198 }
199
200 l := []int32{1, 2, 42}
taozle5c302e02017-07-23 15:21:44 +0200201 lret, err := client.TestList(defaultCtx, l)
Jens Geyerf4598682014-05-08 23:18:44 +0200202 if err != nil {
203 t.Fatalf("Unexpected error in TestList() call: ", err)
204 }
205 if !reflect.DeepEqual(l, lret) {
D. Can Celasun43fb34d2017-01-15 10:53:19 +0100206 t.Fatalf("Unexpected TestList() result expected %#v, got %#v ", l, lret)
Jens Geyerf4598682014-05-08 23:18:44 +0200207 }
208
taozle5c302e02017-07-23 15:21:44 +0200209 eret, err := client.TestEnum(defaultCtx, thrifttest.Numberz_TWO)
Jens Geyerf4598682014-05-08 23:18:44 +0200210 if err != nil {
211 t.Fatalf("Unexpected error in TestEnum() call: ", err)
212 }
213 if eret != thrifttest.Numberz_TWO {
214 t.Fatalf("Unexpected TestEnum() result expected %#v, got %#v ", thrifttest.Numberz_TWO, eret)
215 }
216
taozle5c302e02017-07-23 15:21:44 +0200217 tret, err := client.TestTypedef(defaultCtx, thrifttest.UserId(42))
Jens Geyerf4598682014-05-08 23:18:44 +0200218 if err != nil {
219 t.Fatalf("Unexpected error in TestTypedef() call: ", err)
220 }
221 if tret != thrifttest.UserId(42) {
222 t.Fatalf("Unexpected TestTypedef() result expected %#v, got %#v ", thrifttest.UserId(42), tret)
223 }
224
taozle5c302e02017-07-23 15:21:44 +0200225 mapmap, err := client.TestMapMap(defaultCtx, 42)
Jens Geyerf4598682014-05-08 23:18:44 +0200226 if err != nil {
Nobuaki Sukegawaa649e742015-09-21 13:53:25 +0900227 t.Fatalf("Unexpected error in TestMapMap() call: ", err)
Jens Geyerf4598682014-05-08 23:18:44 +0200228 }
229 if !reflect.DeepEqual(mapmap, rmapmap) {
Nobuaki Sukegawaa649e742015-09-21 13:53:25 +0900230 t.Fatalf("Unexpected TestMapMap() result expected %#v, got %#v ", rmapmap, mapmap)
Jens Geyerf4598682014-05-08 23:18:44 +0200231 }
232
Nobuaki Sukegawa2fab3de2015-08-16 15:42:58 +0900233 crazy := thrifttest.NewInsanity()
D. Can Celasun4f77ab82017-09-21 15:21:00 +0200234 crazy.UserMap = map[thrifttest.Numberz]thrifttest.UserId{
235 thrifttest.Numberz_FIVE: 5,
Nobuaki Sukegawa2fab3de2015-08-16 15:42:58 +0900236 thrifttest.Numberz_EIGHT: 8,
237 }
238 truck1 := thrifttest.NewXtruct()
239 truck1.StringThing = "Goodbye4"
D. Can Celasun4f77ab82017-09-21 15:21:00 +0200240 truck1.ByteThing = 4
241 truck1.I32Thing = 4
242 truck1.I64Thing = 4
Nobuaki Sukegawa2fab3de2015-08-16 15:42:58 +0900243 truck2 := thrifttest.NewXtruct()
244 truck2.StringThing = "Hello2"
D. Can Celasun4f77ab82017-09-21 15:21:00 +0200245 truck2.ByteThing = 2
246 truck2.I32Thing = 2
247 truck2.I64Thing = 2
248 crazy.Xtructs = []*thrifttest.Xtruct{
Nobuaki Sukegawa2fab3de2015-08-16 15:42:58 +0900249 truck1,
250 truck2,
251 }
taozle5c302e02017-07-23 15:21:44 +0200252 insanity, err := client.TestInsanity(defaultCtx, crazy)
Nobuaki Sukegawa2fab3de2015-08-16 15:42:58 +0900253 if err != nil {
254 t.Fatalf("Unexpected error in TestInsanity() call: ", err)
255 }
256 if !reflect.DeepEqual(crazy, insanity[1][2]) {
257 t.Fatalf("Unexpected TestInsanity() first result expected %#v, got %#v ",
D. Can Celasun4f77ab82017-09-21 15:21:00 +0200258 crazy,
259 insanity[1][2])
Nobuaki Sukegawa2fab3de2015-08-16 15:42:58 +0900260 }
261 if !reflect.DeepEqual(crazy, insanity[1][3]) {
262 t.Fatalf("Unexpected TestInsanity() second result expected %#v, got %#v ",
D. Can Celasun4f77ab82017-09-21 15:21:00 +0200263 crazy,
264 insanity[1][3])
Nobuaki Sukegawa2fab3de2015-08-16 15:42:58 +0900265 }
266 if len(insanity[2][6].UserMap) > 0 || len(insanity[2][6].Xtructs) > 0 {
267 t.Fatalf("Unexpected TestInsanity() non-empty result got %#v ",
D. Can Celasun4f77ab82017-09-21 15:21:00 +0200268 insanity[2][6])
Nobuaki Sukegawa2fab3de2015-08-16 15:42:58 +0900269 }
270
taozle5c302e02017-07-23 15:21:44 +0200271 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 +0200272 if err != nil {
273 t.Fatalf("Unexpected error in TestMulti() call: ", err)
274 }
275 if !reflect.DeepEqual(xxs, xxsret) {
276 t.Fatalf("Unexpected TestMulti() result expected %#v, got %#v ", xxs, xxsret)
277 }
278
taozle5c302e02017-07-23 15:21:44 +0200279 err = client.TestException(defaultCtx, "Xception")
Jens Geyerf4598682014-05-08 23:18:44 +0200280 if err == nil {
281 t.Fatalf("Expecting exception in TestException() call")
282 }
283 if !reflect.DeepEqual(err, xcept) {
284 t.Fatalf("Unexpected TestException() result expected %#v, got %#v ", xcept, err)
285 }
286
taozle5c302e02017-07-23 15:21:44 +0200287 err = client.TestException(defaultCtx, "TException")
Nobuaki Sukegawa2fab3de2015-08-16 15:42:58 +0900288 _, ok := err.(thrift.TApplicationException)
289 if err == nil || !ok {
Jens Geyerf4598682014-05-08 23:18:44 +0200290 t.Fatalf("Unexpected TestException() result expected ApplicationError, got %#v ", err)
291 }
292
taozle5c302e02017-07-23 15:21:44 +0200293 ign, err := client.TestMultiException(defaultCtx, "Xception", "ignoreme")
Jens Geyerf4598682014-05-08 23:18:44 +0200294 if ign != nil || err == nil {
295 t.Fatalf("Expecting exception in TestMultiException() call")
296 }
297 if !reflect.DeepEqual(err, &thrifttest.Xception{ErrorCode: 1001, Message: "This is an Xception"}) {
298 t.Fatalf("Unexpected TestMultiException() %#v ", err)
299 }
300
taozle5c302e02017-07-23 15:21:44 +0200301 ign, err = client.TestMultiException(defaultCtx, "Xception2", "ignoreme")
Jens Geyerf4598682014-05-08 23:18:44 +0200302 if ign != nil || err == nil {
303 t.Fatalf("Expecting exception in TestMultiException() call")
304 }
305 expecting := &thrifttest.Xception2{ErrorCode: 2002, StructThing: &thrifttest.Xtruct{StringThing: "This is an Xception2"}}
306
307 if !reflect.DeepEqual(err, expecting) {
308 t.Fatalf("Unexpected TestMultiException() %#v ", err)
309 }
310
taozle5c302e02017-07-23 15:21:44 +0200311 err = client.TestOneway(defaultCtx, 2)
Jens Geyerf4598682014-05-08 23:18:44 +0200312 if err != nil {
313 t.Fatalf("Unexpected error in TestOneway() call: ", err)
314 }
315
316 //Make sure the connection still alive
taozle5c302e02017-07-23 15:21:44 +0200317 if err = client.TestVoid(defaultCtx); err != nil {
Jens Geyerf4598682014-05-08 23:18:44 +0200318 t.Fatalf("Unexpected error in TestVoid() call: ", err)
319 }
320}