blob: f19743a8e2f5f27d08429409d24419f287d2ae07 [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 (
23 "common"
24 "flag"
25 "gen/thrifttest"
26 t "log"
27 "reflect"
28 "thrift"
29)
30
31var host = flag.String("host", "localhost", "Host to connect")
32var port = flag.Int64("port", 9090, "Port number to connect")
33var 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 +020034var transport = flag.String("transport", "buffered", "Transport: buffered, framed, http, zlib")
Jens Geyerf4598682014-05-08 23:18:44 +020035var protocol = flag.String("protocol", "binary", "Protocol: binary, compact, json")
36var ssl = flag.Bool("ssl", false, "Encrypted Transport using SSL")
37var testloops = flag.Int("testloops", 1, "Number of Tests")
38
39func main() {
40 flag.Parse()
41 client, err := common.StartClient(*host, *port, *domain_socket, *transport, *protocol, *ssl)
42 if err != nil {
43 t.Fatalf("Unable to start client: ", err)
44 }
45 for i := 0; i < *testloops; i++ {
46 callEverything(client)
47 }
48}
49
50var rmapmap = map[int32]map[int32]int32{
51 -4: map[int32]int32{-4: -4, -3: -3, -2: -2, -1: -1},
52 4: map[int32]int32{4: 4, 3: 3, 2: 2, 1: 1},
53}
54
55var xxs = &thrifttest.Xtruct{
56 StringThing: "Hello2",
57 ByteThing: 42,
58 I32Thing: 4242,
59 I64Thing: 424242,
60}
61
62var xcept = &thrifttest.Xception{ErrorCode: 1001, Message: "Xception"}
63
64func callEverything(client *thrifttest.ThriftTestClient) {
65 var err error
66 if err = client.TestVoid(); err != nil {
67 t.Fatalf("Unexpected error in TestVoid() call: ", err)
68 }
69
70 thing, err := client.TestString("thing")
71 if err != nil {
72 t.Fatalf("Unexpected error in TestString() call: ", err)
73 }
74 if thing != "thing" {
75 t.Fatalf("Unexpected TestString() result, expected 'thing' got '%s' ", thing)
76 }
77
78 b, err := client.TestByte(42)
79 if err != nil {
80 t.Fatalf("Unexpected error in TestByte() call: ", err)
81 }
82 if b != 42 {
83 t.Fatalf("Unexpected TestByte() result expected 42, got %d ", b)
84 }
85
86 i32, err := client.TestI32(4242)
87 if err != nil {
88 t.Fatalf("Unexpected error in TestI32() call: ", err)
89 }
90 if i32 != 4242 {
91 t.Fatalf("Unexpected TestI32() result expected 4242, got %d ", i32)
92 }
93
94 i64, err := client.TestI64(424242)
95 if err != nil {
96 t.Fatalf("Unexpected error in TestI64() call: ", err)
97 }
98 if i64 != 424242 {
99 t.Fatalf("Unexpected TestI64() result expected 424242, got %d ", i64)
100 }
101
102 d, err := client.TestDouble(42.42)
103 if err != nil {
104 t.Fatalf("Unexpected error in TestDouble() call: ", err)
105 }
106 if d != 42.42 {
107 t.Fatalf("Unexpected TestDouble() result expected 42.42, got %f ", d)
108 }
109
Jens Geyer8bcfdd92014-12-14 03:14:26 +0100110 // TODO: add TestBinary() call
111
Jens Geyerf4598682014-05-08 23:18:44 +0200112 xs := thrifttest.NewXtruct()
113 xs.StringThing = "thing"
114 xs.ByteThing = 42
115 xs.I32Thing = 4242
116 xs.I64Thing = 424242
117 xsret, err := client.TestStruct(xs)
118 if err != nil {
119 t.Fatalf("Unexpected error in TestStruct() call: ", err)
120 }
121 if *xs != *xsret {
122 t.Fatalf("Unexpected TestStruct() result expected %#v, got %#v ", xs, xsret)
123 }
124
125 x2 := thrifttest.NewXtruct2()
126 x2.StructThing = xs
127 x2ret, err := client.TestNest(x2)
128 if err != nil {
129 t.Fatalf("Unexpected error in TestNest() call: ", err)
130 }
131 if !reflect.DeepEqual(x2, x2ret) {
132 t.Fatalf("Unexpected TestNest() result expected %#v, got %#v ", x2, x2ret)
133 }
134
135 m := map[int32]int32{1: 2, 3: 4, 5: 42}
136 mret, err := client.TestMap(m)
137 if err != nil {
138 t.Fatalf("Unexpected error in TestMap() call: ", err)
139 }
140 if !reflect.DeepEqual(m, mret) {
141 t.Fatalf("Unexpected TestMap() result expected %#v, got %#v ", m, mret)
142 }
143
144 sm := map[string]string{"a": "2", "b": "blah", "some": "thing"}
145 smret, err := client.TestStringMap(sm)
146 if err != nil {
147 t.Fatalf("Unexpected error in TestStringMap() call: ", err)
148 }
149 if !reflect.DeepEqual(sm, smret) {
150 t.Fatalf("Unexpected TestStringMap() result expected %#v, got %#v ", sm, smret)
151 }
152
153 s := map[int32]bool{1: true, 2: true, 42: true}
154 sret, err := client.TestSet(s)
155 if err != nil {
156 t.Fatalf("Unexpected error in TestSet() call: ", err)
157 }
158 if !reflect.DeepEqual(s, sret) {
159 t.Fatalf("Unexpected TestSet() result expected %#v, got %#v ", s, sret)
160 }
161
162 l := []int32{1, 2, 42}
163 lret, err := client.TestList(l)
164 if err != nil {
165 t.Fatalf("Unexpected error in TestList() call: ", err)
166 }
167 if !reflect.DeepEqual(l, lret) {
168 t.Fatalf("Unexpected TestSet() result expected %#v, got %#v ", l, lret)
169 }
170
171 eret, err := client.TestEnum(thrifttest.Numberz_TWO)
172 if err != nil {
173 t.Fatalf("Unexpected error in TestEnum() call: ", err)
174 }
175 if eret != thrifttest.Numberz_TWO {
176 t.Fatalf("Unexpected TestEnum() result expected %#v, got %#v ", thrifttest.Numberz_TWO, eret)
177 }
178
179 tret, err := client.TestTypedef(thrifttest.UserId(42))
180 if err != nil {
181 t.Fatalf("Unexpected error in TestTypedef() call: ", err)
182 }
183 if tret != thrifttest.UserId(42) {
184 t.Fatalf("Unexpected TestTypedef() result expected %#v, got %#v ", thrifttest.UserId(42), tret)
185 }
186
187 mapmap, err := client.TestMapMap(42)
188 if err != nil {
189 t.Fatalf("Unexpected error in TestMapmap() call: ", err)
190 }
191 if !reflect.DeepEqual(mapmap, rmapmap) {
192 t.Fatalf("Unexpected TestMapmap() result expected %#v, got %#v ", rmapmap, mapmap)
193 }
194
Nobuaki Sukegawa2fab3de2015-08-16 15:42:58 +0900195 crazy := thrifttest.NewInsanity()
196 crazy.UserMap = map[thrifttest.Numberz]thrifttest.UserId {
197 thrifttest.Numberz_FIVE: 5,
198 thrifttest.Numberz_EIGHT: 8,
199 }
200 truck1 := thrifttest.NewXtruct()
201 truck1.StringThing = "Goodbye4"
202 truck1.ByteThing = 4;
203 truck1.I32Thing = 4;
204 truck1.I64Thing = 4;
205 truck2 := thrifttest.NewXtruct()
206 truck2.StringThing = "Hello2"
207 truck2.ByteThing = 2;
208 truck2.I32Thing = 2;
209 truck2.I64Thing = 2;
210 crazy.Xtructs = []*thrifttest.Xtruct {
211 truck1,
212 truck2,
213 }
214 insanity, err := client.TestInsanity(crazy)
215 if err != nil {
216 t.Fatalf("Unexpected error in TestInsanity() call: ", err)
217 }
218 if !reflect.DeepEqual(crazy, insanity[1][2]) {
219 t.Fatalf("Unexpected TestInsanity() first result expected %#v, got %#v ",
220 crazy,
221 insanity[1][2])
222 }
223 if !reflect.DeepEqual(crazy, insanity[1][3]) {
224 t.Fatalf("Unexpected TestInsanity() second result expected %#v, got %#v ",
225 crazy,
226 insanity[1][3])
227 }
228 if len(insanity[2][6].UserMap) > 0 || len(insanity[2][6].Xtructs) > 0 {
229 t.Fatalf("Unexpected TestInsanity() non-empty result got %#v ",
230 insanity[2][6])
231 }
232
Jens Geyerf4598682014-05-08 23:18:44 +0200233 xxsret, err := client.TestMulti(42, 4242, 424242, map[int16]string{1: "blah", 2: "thing"}, thrifttest.Numberz_EIGHT, thrifttest.UserId(24))
234 if err != nil {
235 t.Fatalf("Unexpected error in TestMulti() call: ", err)
236 }
237 if !reflect.DeepEqual(xxs, xxsret) {
238 t.Fatalf("Unexpected TestMulti() result expected %#v, got %#v ", xxs, xxsret)
239 }
240
241 err = client.TestException("Xception")
242 if err == nil {
243 t.Fatalf("Expecting exception in TestException() call")
244 }
245 if !reflect.DeepEqual(err, xcept) {
246 t.Fatalf("Unexpected TestException() result expected %#v, got %#v ", xcept, err)
247 }
248
Jens Geyerf4598682014-05-08 23:18:44 +0200249 err = client.TestException("TException")
Nobuaki Sukegawa2fab3de2015-08-16 15:42:58 +0900250 _, ok := err.(thrift.TApplicationException)
251 if err == nil || !ok {
Jens Geyerf4598682014-05-08 23:18:44 +0200252 t.Fatalf("Unexpected TestException() result expected ApplicationError, got %#v ", err)
253 }
254
255 ign, err := client.TestMultiException("Xception", "ignoreme")
256 if ign != nil || err == nil {
257 t.Fatalf("Expecting exception in TestMultiException() call")
258 }
259 if !reflect.DeepEqual(err, &thrifttest.Xception{ErrorCode: 1001, Message: "This is an Xception"}) {
260 t.Fatalf("Unexpected TestMultiException() %#v ", err)
261 }
262
263 ign, err = client.TestMultiException("Xception2", "ignoreme")
264 if ign != nil || err == nil {
265 t.Fatalf("Expecting exception in TestMultiException() call")
266 }
267 expecting := &thrifttest.Xception2{ErrorCode: 2002, StructThing: &thrifttest.Xtruct{StringThing: "This is an Xception2"}}
268
269 if !reflect.DeepEqual(err, expecting) {
270 t.Fatalf("Unexpected TestMultiException() %#v ", err)
271 }
272
273 err = client.TestOneway(2)
274 if err != nil {
275 t.Fatalf("Unexpected error in TestOneway() call: ", err)
276 }
277
278 //Make sure the connection still alive
279 if err = client.TestVoid(); err != nil {
280 t.Fatalf("Unexpected error in TestVoid() call: ", err)
281 }
282}