blob: 2acf6cfc088d84b5f4a8c4afc50ca13ddcbb17d8 [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")
34var transport = flag.String("transport", "buffered", "Transport: buffered, framed, http")
35var 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
110 xs := thrifttest.NewXtruct()
111 xs.StringThing = "thing"
112 xs.ByteThing = 42
113 xs.I32Thing = 4242
114 xs.I64Thing = 424242
115 xsret, err := client.TestStruct(xs)
116 if err != nil {
117 t.Fatalf("Unexpected error in TestStruct() call: ", err)
118 }
119 if *xs != *xsret {
120 t.Fatalf("Unexpected TestStruct() result expected %#v, got %#v ", xs, xsret)
121 }
122
123 x2 := thrifttest.NewXtruct2()
124 x2.StructThing = xs
125 x2ret, err := client.TestNest(x2)
126 if err != nil {
127 t.Fatalf("Unexpected error in TestNest() call: ", err)
128 }
129 if !reflect.DeepEqual(x2, x2ret) {
130 t.Fatalf("Unexpected TestNest() result expected %#v, got %#v ", x2, x2ret)
131 }
132
133 m := map[int32]int32{1: 2, 3: 4, 5: 42}
134 mret, err := client.TestMap(m)
135 if err != nil {
136 t.Fatalf("Unexpected error in TestMap() call: ", err)
137 }
138 if !reflect.DeepEqual(m, mret) {
139 t.Fatalf("Unexpected TestMap() result expected %#v, got %#v ", m, mret)
140 }
141
142 sm := map[string]string{"a": "2", "b": "blah", "some": "thing"}
143 smret, err := client.TestStringMap(sm)
144 if err != nil {
145 t.Fatalf("Unexpected error in TestStringMap() call: ", err)
146 }
147 if !reflect.DeepEqual(sm, smret) {
148 t.Fatalf("Unexpected TestStringMap() result expected %#v, got %#v ", sm, smret)
149 }
150
151 s := map[int32]bool{1: true, 2: true, 42: true}
152 sret, err := client.TestSet(s)
153 if err != nil {
154 t.Fatalf("Unexpected error in TestSet() call: ", err)
155 }
156 if !reflect.DeepEqual(s, sret) {
157 t.Fatalf("Unexpected TestSet() result expected %#v, got %#v ", s, sret)
158 }
159
160 l := []int32{1, 2, 42}
161 lret, err := client.TestList(l)
162 if err != nil {
163 t.Fatalf("Unexpected error in TestList() call: ", err)
164 }
165 if !reflect.DeepEqual(l, lret) {
166 t.Fatalf("Unexpected TestSet() result expected %#v, got %#v ", l, lret)
167 }
168
169 eret, err := client.TestEnum(thrifttest.Numberz_TWO)
170 if err != nil {
171 t.Fatalf("Unexpected error in TestEnum() call: ", err)
172 }
173 if eret != thrifttest.Numberz_TWO {
174 t.Fatalf("Unexpected TestEnum() result expected %#v, got %#v ", thrifttest.Numberz_TWO, eret)
175 }
176
177 tret, err := client.TestTypedef(thrifttest.UserId(42))
178 if err != nil {
179 t.Fatalf("Unexpected error in TestTypedef() call: ", err)
180 }
181 if tret != thrifttest.UserId(42) {
182 t.Fatalf("Unexpected TestTypedef() result expected %#v, got %#v ", thrifttest.UserId(42), tret)
183 }
184
185 mapmap, err := client.TestMapMap(42)
186 if err != nil {
187 t.Fatalf("Unexpected error in TestMapmap() call: ", err)
188 }
189 if !reflect.DeepEqual(mapmap, rmapmap) {
190 t.Fatalf("Unexpected TestMapmap() result expected %#v, got %#v ", rmapmap, mapmap)
191 }
192
193 xxsret, err := client.TestMulti(42, 4242, 424242, map[int16]string{1: "blah", 2: "thing"}, thrifttest.Numberz_EIGHT, thrifttest.UserId(24))
194 if err != nil {
195 t.Fatalf("Unexpected error in TestMulti() call: ", err)
196 }
197 if !reflect.DeepEqual(xxs, xxsret) {
198 t.Fatalf("Unexpected TestMulti() result expected %#v, got %#v ", xxs, xxsret)
199 }
200
201 err = client.TestException("Xception")
202 if err == nil {
203 t.Fatalf("Expecting exception in TestException() call")
204 }
205 if !reflect.DeepEqual(err, xcept) {
206 t.Fatalf("Unexpected TestException() result expected %#v, got %#v ", xcept, err)
207 }
208
209 // TODO: connection is being closed on this
210 err = client.TestException("TException")
211 tex, ok := err.(thrift.TApplicationException)
212 if err == nil || !ok || tex.TypeId() != thrift.INTERNAL_ERROR {
213 t.Fatalf("Unexpected TestException() result expected ApplicationError, got %#v ", err)
214 }
215
216 ign, err := client.TestMultiException("Xception", "ignoreme")
217 if ign != nil || err == nil {
218 t.Fatalf("Expecting exception in TestMultiException() call")
219 }
220 if !reflect.DeepEqual(err, &thrifttest.Xception{ErrorCode: 1001, Message: "This is an Xception"}) {
221 t.Fatalf("Unexpected TestMultiException() %#v ", err)
222 }
223
224 ign, err = client.TestMultiException("Xception2", "ignoreme")
225 if ign != nil || err == nil {
226 t.Fatalf("Expecting exception in TestMultiException() call")
227 }
228 expecting := &thrifttest.Xception2{ErrorCode: 2002, StructThing: &thrifttest.Xtruct{StringThing: "This is an Xception2"}}
229
230 if !reflect.DeepEqual(err, expecting) {
231 t.Fatalf("Unexpected TestMultiException() %#v ", err)
232 }
233
234 err = client.TestOneway(2)
235 if err != nil {
236 t.Fatalf("Unexpected error in TestOneway() call: ", err)
237 }
238
239 //Make sure the connection still alive
240 if err = client.TestVoid(); err != nil {
241 t.Fatalf("Unexpected error in TestVoid() call: ", err)
242 }
243}