blob: 5c8915ab3beb3820d43036da0117f703bf077a00 [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 common
21
22import (
Jens Geyer56a03be2015-06-17 22:21:27 +020023 "github.com/golang/mock/gomock"
Jens Geyerf4598682014-05-08 23:18:44 +020024 "errors"
25 "gen/thrifttest"
26 "reflect"
27 "testing"
28 "thrift"
29)
30
31type test_unit struct {
32 host string
33 port int64
34 domain_socket string
35 transport string
36 protocol string
37 ssl bool
38}
39
40var units = []test_unit{
41 {"127.0.0.1", 9090, "", "", "binary", false},
42 {"127.0.0.1", 9091, "", "", "compact", false},
43 {"127.0.0.1", 9092, "", "", "binary", true},
44 {"127.0.0.1", 9093, "", "", "compact", true},
45}
46
47func TestAllConnection(t *testing.T) {
48 certPath = "../../../keys"
49 for _, unit := range units {
50 t.Logf("%#v", unit)
51 doUnit(t, &unit)
52 }
53}
54
55func doUnit(t *testing.T, unit *test_unit) {
56 ctrl := gomock.NewController(t)
57 defer ctrl.Finish()
58 handler := NewMockThriftTest(ctrl)
Roger Meier41ad4342015-03-24 22:30:40 +010059 server, err := StartServer(unit.host, unit.port, unit.domain_socket, unit.transport, unit.protocol, unit.ssl, "../../../keys", handler)
Jens Geyerf4598682014-05-08 23:18:44 +020060 if err != nil {
61 t.Errorf("Unable to start server", err)
62 t.FailNow()
63 }
64 defer server.Stop()
65 client, err := StartClient(unit.host, unit.port, unit.domain_socket, unit.transport, unit.protocol, unit.ssl)
66 if err != nil {
67 t.Errorf("Unable to start client", err)
68 t.FailNow()
69 }
70 defer client.Transport.Close()
71 callEverythingWithMock(t, client, handler)
72}
73
74var rmapmap = map[int32]map[int32]int32{
75 -4: map[int32]int32{-4: -4, -3: -3, -2: -2, -1: -1},
76 4: map[int32]int32{4: 4, 3: 3, 2: 2, 1: 1},
77}
78
79var xxs = &thrifttest.Xtruct{
80 StringThing: "Hello2",
81 ByteThing: 42,
82 I32Thing: 4242,
83 I64Thing: 424242,
84}
85
86var xcept = &thrifttest.Xception{ErrorCode: 1001, Message: "some"}
87
88func callEverythingWithMock(t *testing.T, client *thrifttest.ThriftTestClient, handler *MockThriftTest) {
89 gomock.InOrder(
90 handler.EXPECT().TestVoid(),
91 handler.EXPECT().TestString("thing").Return("thing", nil),
Nobuaki Sukegawaa649e742015-09-21 13:53:25 +090092 handler.EXPECT().TestBool(true).Return(true, nil),
93 handler.EXPECT().TestBool(false).Return(false, nil),
Jens Geyerf4598682014-05-08 23:18:44 +020094 handler.EXPECT().TestByte(int8(42)).Return(int8(42), nil),
95 handler.EXPECT().TestI32(int32(4242)).Return(int32(4242), nil),
96 handler.EXPECT().TestI64(int64(424242)).Return(int64(424242), nil),
Jens Geyer8bcfdd92014-12-14 03:14:26 +010097 // TODO: add TestBinary()
Jens Geyerf4598682014-05-08 23:18:44 +020098 handler.EXPECT().TestDouble(float64(42.42)).Return(float64(42.42), nil),
99 handler.EXPECT().TestStruct(&thrifttest.Xtruct{StringThing: "thing", ByteThing: 42, I32Thing: 4242, I64Thing: 424242}).Return(&thrifttest.Xtruct{StringThing: "thing", ByteThing: 42, I32Thing: 4242, I64Thing: 424242}, nil),
100 handler.EXPECT().TestNest(&thrifttest.Xtruct2{StructThing: &thrifttest.Xtruct{StringThing: "thing", ByteThing: 42, I32Thing: 4242, I64Thing: 424242}}).Return(&thrifttest.Xtruct2{StructThing: &thrifttest.Xtruct{StringThing: "thing", ByteThing: 42, I32Thing: 4242, I64Thing: 424242}}, nil),
101 handler.EXPECT().TestMap(map[int32]int32{1: 2, 3: 4, 5: 42}).Return(map[int32]int32{1: 2, 3: 4, 5: 42}, nil),
102 handler.EXPECT().TestStringMap(map[string]string{"a": "2", "b": "blah", "some": "thing"}).Return(map[string]string{"a": "2", "b": "blah", "some": "thing"}, nil),
103 handler.EXPECT().TestSet(map[int32]bool{1: true, 2: true, 42: true}).Return(map[int32]bool{1: true, 2: true, 42: true}, nil),
104 handler.EXPECT().TestList([]int32{1, 2, 42}).Return([]int32{1, 2, 42}, nil),
105 handler.EXPECT().TestEnum(thrifttest.Numberz_TWO).Return(thrifttest.Numberz_TWO, nil),
106 handler.EXPECT().TestTypedef(thrifttest.UserId(42)).Return(thrifttest.UserId(42), nil),
107 handler.EXPECT().TestMapMap(int32(42)).Return(rmapmap, nil),
Nobuaki Sukegawa2fab3de2015-08-16 15:42:58 +0900108 // TODO: not testing insanity
Jens Geyerf4598682014-05-08 23:18:44 +0200109 handler.EXPECT().TestMulti(int8(42), int32(4242), int64(424242), map[int16]string{1: "blah", 2: "thing"}, thrifttest.Numberz_EIGHT, thrifttest.UserId(24)).Return(xxs, nil),
110 handler.EXPECT().TestException("some").Return(xcept),
111 handler.EXPECT().TestException("TException").Return(errors.New("Just random exception")),
112 handler.EXPECT().TestMultiException("Xception", "ignoreme").Return(nil, &thrifttest.Xception{ErrorCode: 1001, Message: "This is an Xception"}),
113 handler.EXPECT().TestMultiException("Xception2", "ignoreme").Return(nil, &thrifttest.Xception2{ErrorCode: 2002, StructThing: &thrifttest.Xtruct{StringThing: "This is an Xception2"}}),
114 handler.EXPECT().TestOneway(int32(2)).Return(nil),
115 handler.EXPECT().TestVoid(),
116 )
117 var err error
118 if err = client.TestVoid(); err != nil {
119 t.Errorf("Unexpected error in TestVoid() call: ", err)
120 }
121
122 thing, err := client.TestString("thing")
123 if err != nil {
124 t.Errorf("Unexpected error in TestString() call: ", err)
125 }
126 if thing != "thing" {
127 t.Errorf("Unexpected TestString() result, expected 'thing' got '%s' ", thing)
128 }
129
Nobuaki Sukegawaa649e742015-09-21 13:53:25 +0900130 bl, err := client.TestBool(true)
131 if err != nil {
132 t.Errorf("Unexpected error in TestBool() call: ", err)
133 }
134 if !bl {
135 t.Errorf("Unexpected TestBool() result expected true, got %f ", bl)
136 }
137 bl, err = client.TestBool(false)
138 if err != nil {
139 t.Errorf("Unexpected error in TestBool() call: ", err)
140 }
141 if bl {
142 t.Errorf("Unexpected TestBool() result expected false, got %f ", bl)
143 }
144
Jens Geyerf4598682014-05-08 23:18:44 +0200145 b, err := client.TestByte(42)
146 if err != nil {
147 t.Errorf("Unexpected error in TestByte() call: ", err)
148 }
149 if b != 42 {
150 t.Errorf("Unexpected TestByte() result expected 42, got %d ", b)
151 }
152
153 i32, err := client.TestI32(4242)
154 if err != nil {
155 t.Errorf("Unexpected error in TestI32() call: ", err)
156 }
157 if i32 != 4242 {
158 t.Errorf("Unexpected TestI32() result expected 4242, got %d ", i32)
159 }
160
161 i64, err := client.TestI64(424242)
162 if err != nil {
163 t.Errorf("Unexpected error in TestI64() call: ", err)
164 }
165 if i64 != 424242 {
166 t.Errorf("Unexpected TestI64() result expected 424242, got %d ", i64)
167 }
168
169 d, err := client.TestDouble(42.42)
170 if err != nil {
171 t.Errorf("Unexpected error in TestDouble() call: ", err)
172 }
173 if d != 42.42 {
174 t.Errorf("Unexpected TestDouble() result expected 42.42, got %f ", d)
175 }
176
Jens Geyer8bcfdd92014-12-14 03:14:26 +0100177 // TODO: add TestBinary() call
178
Jens Geyerf4598682014-05-08 23:18:44 +0200179 xs := thrifttest.NewXtruct()
180 xs.StringThing = "thing"
181 xs.ByteThing = 42
182 xs.I32Thing = 4242
183 xs.I64Thing = 424242
184 xsret, err := client.TestStruct(xs)
185 if err != nil {
186 t.Errorf("Unexpected error in TestStruct() call: ", err)
187 }
188 if *xs != *xsret {
189 t.Errorf("Unexpected TestStruct() result expected %#v, got %#v ", xs, xsret)
190 }
191
192 x2 := thrifttest.NewXtruct2()
193 x2.StructThing = xs
194 x2ret, err := client.TestNest(x2)
195 if err != nil {
196 t.Errorf("Unexpected error in TestNest() call: ", err)
197 }
198 if !reflect.DeepEqual(x2, x2ret) {
199 t.Errorf("Unexpected TestNest() result expected %#v, got %#v ", x2, x2ret)
200 }
201
202 m := map[int32]int32{1: 2, 3: 4, 5: 42}
203 mret, err := client.TestMap(m)
204 if err != nil {
205 t.Errorf("Unexpected error in TestMap() call: ", err)
206 }
207 if !reflect.DeepEqual(m, mret) {
208 t.Errorf("Unexpected TestMap() result expected %#v, got %#v ", m, mret)
209 }
210
211 sm := map[string]string{"a": "2", "b": "blah", "some": "thing"}
212 smret, err := client.TestStringMap(sm)
213 if err != nil {
214 t.Errorf("Unexpected error in TestStringMap() call: ", err)
215 }
216 if !reflect.DeepEqual(sm, smret) {
217 t.Errorf("Unexpected TestStringMap() result expected %#v, got %#v ", sm, smret)
218 }
219
220 s := map[int32]bool{1: true, 2: true, 42: true}
221 sret, err := client.TestSet(s)
222 if err != nil {
223 t.Errorf("Unexpected error in TestSet() call: ", err)
224 }
225 if !reflect.DeepEqual(s, sret) {
226 t.Errorf("Unexpected TestSet() result expected %#v, got %#v ", s, sret)
227 }
228
229 l := []int32{1, 2, 42}
230 lret, err := client.TestList(l)
231 if err != nil {
232 t.Errorf("Unexpected error in TestList() call: ", err)
233 }
234 if !reflect.DeepEqual(l, lret) {
235 t.Errorf("Unexpected TestSet() result expected %#v, got %#v ", l, lret)
236 }
237
238 eret, err := client.TestEnum(thrifttest.Numberz_TWO)
239 if err != nil {
240 t.Errorf("Unexpected error in TestEnum() call: ", err)
241 }
242 if eret != thrifttest.Numberz_TWO {
243 t.Errorf("Unexpected TestEnum() result expected %#v, got %#v ", thrifttest.Numberz_TWO, eret)
244 }
245
246 tret, err := client.TestTypedef(thrifttest.UserId(42))
247 if err != nil {
248 t.Errorf("Unexpected error in TestTypedef() call: ", err)
249 }
250 if tret != thrifttest.UserId(42) {
251 t.Errorf("Unexpected TestTypedef() result expected %#v, got %#v ", thrifttest.UserId(42), tret)
252 }
253
254 mapmap, err := client.TestMapMap(42)
255 if err != nil {
256 t.Errorf("Unexpected error in TestMapmap() call: ", err)
257 }
258 if !reflect.DeepEqual(mapmap, rmapmap) {
259 t.Errorf("Unexpected TestMapmap() result expected %#v, got %#v ", rmapmap, mapmap)
260 }
261
262 xxsret, err := client.TestMulti(42, 4242, 424242, map[int16]string{1: "blah", 2: "thing"}, thrifttest.Numberz_EIGHT, thrifttest.UserId(24))
263 if err != nil {
264 t.Errorf("Unexpected error in TestMulti() call: ", err)
265 }
266 if !reflect.DeepEqual(xxs, xxsret) {
267 t.Errorf("Unexpected TestMulti() result expected %#v, got %#v ", xxs, xxsret)
268 }
269
270 err = client.TestException("some")
271 if err == nil {
272 t.Errorf("Expecting exception in TestException() call")
273 }
274 if !reflect.DeepEqual(err, xcept) {
275 t.Errorf("Unexpected TestException() result expected %#v, got %#v ", xcept, err)
276 }
277
278 // TODO: connection is being closed on this
279 err = client.TestException("TException")
280 tex, ok := err.(thrift.TApplicationException)
281 if err == nil || !ok || tex.TypeId() != thrift.INTERNAL_ERROR {
282 t.Errorf("Unexpected TestException() result expected ApplicationError, got %#v ", err)
283 }
284
285 ign, err := client.TestMultiException("Xception", "ignoreme")
286 if ign != nil || err == nil {
287 t.Errorf("Expecting exception in TestMultiException() call")
288 }
289 if !reflect.DeepEqual(err, &thrifttest.Xception{ErrorCode: 1001, Message: "This is an Xception"}) {
290 t.Errorf("Unexpected TestMultiException() %#v ", err)
291 }
292
293 ign, err = client.TestMultiException("Xception2", "ignoreme")
294 if ign != nil || err == nil {
295 t.Errorf("Expecting exception in TestMultiException() call")
296 }
297 expecting := &thrifttest.Xception2{ErrorCode: 2002, StructThing: &thrifttest.Xtruct{StringThing: "This is an Xception2"}}
298
299 if !reflect.DeepEqual(err, expecting) {
300 t.Errorf("Unexpected TestMultiException() %#v ", err)
301 }
302
303 err = client.TestOneway(2)
304 if err != nil {
305 t.Errorf("Unexpected error in TestOneway() call: ", err)
306 }
307
308 //Make sure the connection still alive
309 if err = client.TestVoid(); err != nil {
310 t.Errorf("Unexpected error in TestVoid() call: ", err)
311 }
312}