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