blob: ecd021f3b241359ad42249a765c3c909f6c5536c [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(
taozlec0d384a2017-07-17 18:40:42 +020095 handler.EXPECT().TestVoid(gomock.Any()),
96 handler.EXPECT().TestString(gomock.Any(), "thing").Return("thing", nil),
97 handler.EXPECT().TestBool(gomock.Any(), true).Return(true, nil),
98 handler.EXPECT().TestBool(gomock.Any(), false).Return(false, nil),
99 handler.EXPECT().TestByte(gomock.Any(), int8(42)).Return(int8(42), nil),
100 handler.EXPECT().TestI32(gomock.Any(), int32(4242)).Return(int32(4242), nil),
101 handler.EXPECT().TestI64(gomock.Any(), int64(424242)).Return(int64(424242), nil),
Jens Geyer8bcfdd92014-12-14 03:14:26 +0100102 // TODO: add TestBinary()
taozlec0d384a2017-07-17 18:40:42 +0200103 handler.EXPECT().TestDouble(gomock.Any(), float64(42.42)).Return(float64(42.42), nil),
104 handler.EXPECT().TestStruct(gomock.Any(), &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(gomock.Any(), &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(gomock.Any(), map[int32]int32{1: 2, 3: 4, 5: 42}).Return(map[int32]int32{1: 2, 3: 4, 5: 42}, nil),
107 handler.EXPECT().TestStringMap(gomock.Any(), map[string]string{"a": "2", "b": "blah", "some": "thing"}).Return(map[string]string{"a": "2", "b": "blah", "some": "thing"}, nil),
108 handler.EXPECT().TestSet(gomock.Any(), []int32{1, 2, 42}).Return([]int32{1, 2, 42}, nil),
109 handler.EXPECT().TestList(gomock.Any(), []int32{1, 2, 42}).Return([]int32{1, 2, 42}, nil),
110 handler.EXPECT().TestEnum(gomock.Any(), thrifttest.Numberz_TWO).Return(thrifttest.Numberz_TWO, nil),
111 handler.EXPECT().TestTypedef(gomock.Any(), thrifttest.UserId(42)).Return(thrifttest.UserId(42), nil),
112 handler.EXPECT().TestMapMap(gomock.Any(), int32(42)).Return(rmapmap, nil),
Nobuaki Sukegawa2fab3de2015-08-16 15:42:58 +0900113 // TODO: not testing insanity
taozlec0d384a2017-07-17 18:40:42 +0200114 handler.EXPECT().TestMulti(gomock.Any(), 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(gomock.Any(), "some").Return(xcept),
116 handler.EXPECT().TestException(gomock.Any(), "TException").Return(errors.New("Just random exception")),
117 handler.EXPECT().TestMultiException(gomock.Any(), "Xception", "ignoreme").Return(nil, &thrifttest.Xception{ErrorCode: 1001, Message: "This is an Xception"}),
118 handler.EXPECT().TestMultiException(gomock.Any(), "Xception2", "ignoreme").Return(nil, &thrifttest.Xception2{ErrorCode: 2002, StructThing: &thrifttest.Xtruct{StringThing: "This is an Xception2"}}),
119 handler.EXPECT().TestOneway(gomock.Any(), int32(2)).Return(nil),
120 handler.EXPECT().TestVoid(gomock.Any()),
Jens Geyerf4598682014-05-08 23:18:44 +0200121 )
122 var err error
taozle5c302e02017-07-23 15:21:44 +0200123 if err = client.TestVoid(defaultCtx); err != nil {
Jens Geyerf4598682014-05-08 23:18:44 +0200124 t.Errorf("Unexpected error in TestVoid() call: ", err)
125 }
126
taozle5c302e02017-07-23 15:21:44 +0200127 thing, err := client.TestString(defaultCtx, "thing")
Jens Geyerf4598682014-05-08 23:18:44 +0200128 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
taozle5c302e02017-07-23 15:21:44 +0200135 bl, err := client.TestBool(defaultCtx, true)
Nobuaki Sukegawaa649e742015-09-21 13:53:25 +0900136 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 }
taozle5c302e02017-07-23 15:21:44 +0200142 bl, err = client.TestBool(defaultCtx, false)
Nobuaki Sukegawaa649e742015-09-21 13:53:25 +0900143 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
taozle5c302e02017-07-23 15:21:44 +0200150 b, err := client.TestByte(defaultCtx, 42)
Jens Geyerf4598682014-05-08 23:18:44 +0200151 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
taozle5c302e02017-07-23 15:21:44 +0200158 i32, err := client.TestI32(defaultCtx, 4242)
Jens Geyerf4598682014-05-08 23:18:44 +0200159 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
taozle5c302e02017-07-23 15:21:44 +0200166 i64, err := client.TestI64(defaultCtx, 424242)
Jens Geyerf4598682014-05-08 23:18:44 +0200167 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
taozle5c302e02017-07-23 15:21:44 +0200174 d, err := client.TestDouble(defaultCtx, 42.42)
Jens Geyerf4598682014-05-08 23:18:44 +0200175 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
taozle5c302e02017-07-23 15:21:44 +0200189 xsret, err := client.TestStruct(defaultCtx, xs)
Jens Geyerf4598682014-05-08 23:18:44 +0200190 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
taozle5c302e02017-07-23 15:21:44 +0200199 x2ret, err := client.TestNest(defaultCtx, x2)
Jens Geyerf4598682014-05-08 23:18:44 +0200200 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}
taozle5c302e02017-07-23 15:21:44 +0200208 mret, err := client.TestMap(defaultCtx, m)
Jens Geyerf4598682014-05-08 23:18:44 +0200209 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"}
taozle5c302e02017-07-23 15:21:44 +0200217 smret, err := client.TestStringMap(defaultCtx, sm)
Jens Geyerf4598682014-05-08 23:18:44 +0200218 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
D. Can Celasun43fb34d2017-01-15 10:53:19 +0100225 s := []int32{1, 2, 42}
taozle5c302e02017-07-23 15:21:44 +0200226 sret, err := client.TestSet(defaultCtx, s)
Jens Geyerf4598682014-05-08 23:18:44 +0200227 if err != nil {
228 t.Errorf("Unexpected error in TestSet() call: ", err)
229 }
D. Can Celasun43fb34d2017-01-15 10:53:19 +0100230 // Sets can be in any order, but Go slices are ordered, so reflect.DeepEqual won't work.
231 stemp := map[int32]struct{}{}
232 for _, val := range s {
233 stemp[val] = struct{}{}
234 }
235 for _, val := range sret {
236 if _, ok := stemp[val]; !ok {
237 t.Fatalf("Unexpected TestSet() result expected %#v, got %#v ", s, sret)
238 }
Jens Geyerf4598682014-05-08 23:18:44 +0200239 }
240
241 l := []int32{1, 2, 42}
taozle5c302e02017-07-23 15:21:44 +0200242 lret, err := client.TestList(defaultCtx, l)
Jens Geyerf4598682014-05-08 23:18:44 +0200243 if err != nil {
244 t.Errorf("Unexpected error in TestList() call: ", err)
245 }
246 if !reflect.DeepEqual(l, lret) {
D. Can Celasun43fb34d2017-01-15 10:53:19 +0100247 t.Errorf("Unexpected TestList() result expected %#v, got %#v ", l, lret)
Jens Geyerf4598682014-05-08 23:18:44 +0200248 }
249
taozle5c302e02017-07-23 15:21:44 +0200250 eret, err := client.TestEnum(defaultCtx, thrifttest.Numberz_TWO)
Jens Geyerf4598682014-05-08 23:18:44 +0200251 if err != nil {
252 t.Errorf("Unexpected error in TestEnum() call: ", err)
253 }
254 if eret != thrifttest.Numberz_TWO {
255 t.Errorf("Unexpected TestEnum() result expected %#v, got %#v ", thrifttest.Numberz_TWO, eret)
256 }
257
taozle5c302e02017-07-23 15:21:44 +0200258 tret, err := client.TestTypedef(defaultCtx, thrifttest.UserId(42))
Jens Geyerf4598682014-05-08 23:18:44 +0200259 if err != nil {
260 t.Errorf("Unexpected error in TestTypedef() call: ", err)
261 }
262 if tret != thrifttest.UserId(42) {
263 t.Errorf("Unexpected TestTypedef() result expected %#v, got %#v ", thrifttest.UserId(42), tret)
264 }
265
taozle5c302e02017-07-23 15:21:44 +0200266 mapmap, err := client.TestMapMap(defaultCtx, 42)
Jens Geyerf4598682014-05-08 23:18:44 +0200267 if err != nil {
268 t.Errorf("Unexpected error in TestMapmap() call: ", err)
269 }
270 if !reflect.DeepEqual(mapmap, rmapmap) {
271 t.Errorf("Unexpected TestMapmap() result expected %#v, got %#v ", rmapmap, mapmap)
272 }
273
taozle5c302e02017-07-23 15:21:44 +0200274 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 +0200275 if err != nil {
276 t.Errorf("Unexpected error in TestMulti() call: ", err)
277 }
278 if !reflect.DeepEqual(xxs, xxsret) {
279 t.Errorf("Unexpected TestMulti() result expected %#v, got %#v ", xxs, xxsret)
280 }
281
taozle5c302e02017-07-23 15:21:44 +0200282 err = client.TestException(defaultCtx, "some")
Jens Geyerf4598682014-05-08 23:18:44 +0200283 if err == nil {
284 t.Errorf("Expecting exception in TestException() call")
285 }
286 if !reflect.DeepEqual(err, xcept) {
287 t.Errorf("Unexpected TestException() result expected %#v, got %#v ", xcept, err)
288 }
289
290 // TODO: connection is being closed on this
taozle5c302e02017-07-23 15:21:44 +0200291 err = client.TestException(defaultCtx, "TException")
Jens Geyerf4598682014-05-08 23:18:44 +0200292 tex, ok := err.(thrift.TApplicationException)
293 if err == nil || !ok || tex.TypeId() != thrift.INTERNAL_ERROR {
294 t.Errorf("Unexpected TestException() result expected ApplicationError, got %#v ", err)
295 }
296
taozle5c302e02017-07-23 15:21:44 +0200297 ign, err := client.TestMultiException(defaultCtx, "Xception", "ignoreme")
Jens Geyerf4598682014-05-08 23:18:44 +0200298 if ign != nil || err == nil {
299 t.Errorf("Expecting exception in TestMultiException() call")
300 }
301 if !reflect.DeepEqual(err, &thrifttest.Xception{ErrorCode: 1001, Message: "This is an Xception"}) {
302 t.Errorf("Unexpected TestMultiException() %#v ", err)
303 }
304
taozle5c302e02017-07-23 15:21:44 +0200305 ign, err = client.TestMultiException(defaultCtx, "Xception2", "ignoreme")
Jens Geyerf4598682014-05-08 23:18:44 +0200306 if ign != nil || err == nil {
307 t.Errorf("Expecting exception in TestMultiException() call")
308 }
309 expecting := &thrifttest.Xception2{ErrorCode: 2002, StructThing: &thrifttest.Xtruct{StringThing: "This is an Xception2"}}
310
311 if !reflect.DeepEqual(err, expecting) {
312 t.Errorf("Unexpected TestMultiException() %#v ", err)
313 }
314
taozle5c302e02017-07-23 15:21:44 +0200315 err = client.TestOneway(defaultCtx, 2)
Jens Geyerf4598682014-05-08 23:18:44 +0200316 if err != nil {
317 t.Errorf("Unexpected error in TestOneway() call: ", err)
318 }
319
320 //Make sure the connection still alive
taozle5c302e02017-07-23 15:21:44 +0200321 if err = client.TestVoid(defaultCtx); err != nil {
Jens Geyerf4598682014-05-08 23:18:44 +0200322 t.Errorf("Unexpected error in TestVoid() call: ", err)
323 }
324}