blob: c4cfd44f30316beb502563f333c48ad4dc2c2ea9 [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"
D. Can Celasun4f77ab82017-09-21 15:21:00 +020026 "sync"
Jens Geyerf4598682014-05-08 23:18:44 +020027 "testing"
28 "thrift"
claudemirof8ca0552016-01-10 23:31:30 -020029
30 "github.com/golang/mock/gomock"
Jens Geyerf4598682014-05-08 23:18:44 +020031)
32
33type test_unit struct {
34 host string
35 port int64
36 domain_socket string
37 transport string
38 protocol string
39 ssl bool
40}
41
42var units = []test_unit{
Nobuaki Sukegawafd02a302016-08-16 14:06:48 +090043 {"127.0.0.1", 9095, "", "", "binary", false},
Jens Geyerf4598682014-05-08 23:18:44 +020044 {"127.0.0.1", 9091, "", "", "compact", false},
45 {"127.0.0.1", 9092, "", "", "binary", true},
46 {"127.0.0.1", 9093, "", "", "compact", true},
47}
48
49func TestAllConnection(t *testing.T) {
50 certPath = "../../../keys"
D. Can Celasun4f77ab82017-09-21 15:21:00 +020051 wg := &sync.WaitGroup{}
52 wg.Add(len(units))
Jens Geyerf4598682014-05-08 23:18:44 +020053 for _, unit := range units {
D. Can Celasun4f77ab82017-09-21 15:21:00 +020054 go func(u test_unit) {
55 defer wg.Done()
56 doUnit(t, &u)
57 }(unit)
Jens Geyerf4598682014-05-08 23:18:44 +020058 }
D. Can Celasun4f77ab82017-09-21 15:21:00 +020059 wg.Wait()
Jens Geyerf4598682014-05-08 23:18:44 +020060}
61
62func doUnit(t *testing.T, unit *test_unit) {
63 ctrl := gomock.NewController(t)
64 defer ctrl.Finish()
65 handler := NewMockThriftTest(ctrl)
claudemirof8ca0552016-01-10 23:31:30 -020066
67 processor, serverTransport, transportFactory, protocolFactory, err := GetServerParams(unit.host, unit.port, unit.domain_socket, unit.transport, unit.protocol, unit.ssl, "../../../keys", handler)
68
69 server := thrift.NewTSimpleServer4(processor, serverTransport, transportFactory, protocolFactory)
70 if err = server.Listen(); err != nil {
D. Can Celasun4f77ab82017-09-21 15:21:00 +020071 t.Errorf("Unable to start server: %v", err)
72 return
Jens Geyerf4598682014-05-08 23:18:44 +020073 }
John Siroisf7d49792016-02-03 17:12:19 -070074 go server.AcceptLoop()
Jens Geyerf4598682014-05-08 23:18:44 +020075 defer server.Stop()
D. Can Celasun4f77ab82017-09-21 15:21:00 +020076 client, trans, err := StartClient(unit.host, unit.port, unit.domain_socket, unit.transport, unit.protocol, unit.ssl)
Jens Geyerf4598682014-05-08 23:18:44 +020077 if err != nil {
D. Can Celasun4f77ab82017-09-21 15:21:00 +020078 t.Errorf("Unable to start client: %v", err)
79 return
Jens Geyerf4598682014-05-08 23:18:44 +020080 }
D. Can Celasun4f77ab82017-09-21 15:21:00 +020081 defer trans.Close()
Jens Geyerf4598682014-05-08 23:18:44 +020082 callEverythingWithMock(t, client, handler)
83}
84
85var rmapmap = map[int32]map[int32]int32{
86 -4: map[int32]int32{-4: -4, -3: -3, -2: -2, -1: -1},
87 4: map[int32]int32{4: 4, 3: 3, 2: 2, 1: 1},
88}
89
90var xxs = &thrifttest.Xtruct{
91 StringThing: "Hello2",
92 ByteThing: 42,
93 I32Thing: 4242,
94 I64Thing: 424242,
95}
96
97var xcept = &thrifttest.Xception{ErrorCode: 1001, Message: "some"}
98
99func callEverythingWithMock(t *testing.T, client *thrifttest.ThriftTestClient, handler *MockThriftTest) {
100 gomock.InOrder(
taozlec0d384a2017-07-17 18:40:42 +0200101 handler.EXPECT().TestVoid(gomock.Any()),
102 handler.EXPECT().TestString(gomock.Any(), "thing").Return("thing", nil),
103 handler.EXPECT().TestBool(gomock.Any(), true).Return(true, nil),
104 handler.EXPECT().TestBool(gomock.Any(), false).Return(false, nil),
105 handler.EXPECT().TestByte(gomock.Any(), int8(42)).Return(int8(42), nil),
106 handler.EXPECT().TestI32(gomock.Any(), int32(4242)).Return(int32(4242), nil),
107 handler.EXPECT().TestI64(gomock.Any(), int64(424242)).Return(int64(424242), nil),
Jens Geyer8bcfdd92014-12-14 03:14:26 +0100108 // TODO: add TestBinary()
taozlec0d384a2017-07-17 18:40:42 +0200109 handler.EXPECT().TestDouble(gomock.Any(), float64(42.42)).Return(float64(42.42), nil),
110 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),
111 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),
112 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),
113 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),
114 handler.EXPECT().TestSet(gomock.Any(), []int32{1, 2, 42}).Return([]int32{1, 2, 42}, nil),
115 handler.EXPECT().TestList(gomock.Any(), []int32{1, 2, 42}).Return([]int32{1, 2, 42}, nil),
116 handler.EXPECT().TestEnum(gomock.Any(), thrifttest.Numberz_TWO).Return(thrifttest.Numberz_TWO, nil),
117 handler.EXPECT().TestTypedef(gomock.Any(), thrifttest.UserId(42)).Return(thrifttest.UserId(42), nil),
118 handler.EXPECT().TestMapMap(gomock.Any(), int32(42)).Return(rmapmap, nil),
Nobuaki Sukegawa2fab3de2015-08-16 15:42:58 +0900119 // TODO: not testing insanity
taozlec0d384a2017-07-17 18:40:42 +0200120 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),
121 handler.EXPECT().TestException(gomock.Any(), "some").Return(xcept),
122 handler.EXPECT().TestException(gomock.Any(), "TException").Return(errors.New("Just random exception")),
123 handler.EXPECT().TestMultiException(gomock.Any(), "Xception", "ignoreme").Return(nil, &thrifttest.Xception{ErrorCode: 1001, Message: "This is an Xception"}),
124 handler.EXPECT().TestMultiException(gomock.Any(), "Xception2", "ignoreme").Return(nil, &thrifttest.Xception2{ErrorCode: 2002, StructThing: &thrifttest.Xtruct{StringThing: "This is an Xception2"}}),
125 handler.EXPECT().TestOneway(gomock.Any(), int32(2)).Return(nil),
126 handler.EXPECT().TestVoid(gomock.Any()),
Jens Geyerf4598682014-05-08 23:18:44 +0200127 )
128 var err error
taozle5c302e02017-07-23 15:21:44 +0200129 if err = client.TestVoid(defaultCtx); err != nil {
Jens Geyerf4598682014-05-08 23:18:44 +0200130 t.Errorf("Unexpected error in TestVoid() call: ", err)
131 }
132
taozle5c302e02017-07-23 15:21:44 +0200133 thing, err := client.TestString(defaultCtx, "thing")
Jens Geyerf4598682014-05-08 23:18:44 +0200134 if err != nil {
135 t.Errorf("Unexpected error in TestString() call: ", err)
136 }
137 if thing != "thing" {
138 t.Errorf("Unexpected TestString() result, expected 'thing' got '%s' ", thing)
139 }
140
taozle5c302e02017-07-23 15:21:44 +0200141 bl, err := client.TestBool(defaultCtx, true)
Nobuaki Sukegawaa649e742015-09-21 13:53:25 +0900142 if err != nil {
143 t.Errorf("Unexpected error in TestBool() call: ", err)
144 }
145 if !bl {
146 t.Errorf("Unexpected TestBool() result expected true, got %f ", bl)
147 }
taozle5c302e02017-07-23 15:21:44 +0200148 bl, err = client.TestBool(defaultCtx, false)
Nobuaki Sukegawaa649e742015-09-21 13:53:25 +0900149 if err != nil {
150 t.Errorf("Unexpected error in TestBool() call: ", err)
151 }
152 if bl {
153 t.Errorf("Unexpected TestBool() result expected false, got %f ", bl)
154 }
155
taozle5c302e02017-07-23 15:21:44 +0200156 b, err := client.TestByte(defaultCtx, 42)
Jens Geyerf4598682014-05-08 23:18:44 +0200157 if err != nil {
158 t.Errorf("Unexpected error in TestByte() call: ", err)
159 }
160 if b != 42 {
161 t.Errorf("Unexpected TestByte() result expected 42, got %d ", b)
162 }
163
taozle5c302e02017-07-23 15:21:44 +0200164 i32, err := client.TestI32(defaultCtx, 4242)
Jens Geyerf4598682014-05-08 23:18:44 +0200165 if err != nil {
166 t.Errorf("Unexpected error in TestI32() call: ", err)
167 }
168 if i32 != 4242 {
169 t.Errorf("Unexpected TestI32() result expected 4242, got %d ", i32)
170 }
171
taozle5c302e02017-07-23 15:21:44 +0200172 i64, err := client.TestI64(defaultCtx, 424242)
Jens Geyerf4598682014-05-08 23:18:44 +0200173 if err != nil {
174 t.Errorf("Unexpected error in TestI64() call: ", err)
175 }
176 if i64 != 424242 {
177 t.Errorf("Unexpected TestI64() result expected 424242, got %d ", i64)
178 }
179
taozle5c302e02017-07-23 15:21:44 +0200180 d, err := client.TestDouble(defaultCtx, 42.42)
Jens Geyerf4598682014-05-08 23:18:44 +0200181 if err != nil {
182 t.Errorf("Unexpected error in TestDouble() call: ", err)
183 }
184 if d != 42.42 {
185 t.Errorf("Unexpected TestDouble() result expected 42.42, got %f ", d)
186 }
187
Jens Geyer8bcfdd92014-12-14 03:14:26 +0100188 // TODO: add TestBinary() call
claudemirof8ca0552016-01-10 23:31:30 -0200189
Jens Geyerf4598682014-05-08 23:18:44 +0200190 xs := thrifttest.NewXtruct()
191 xs.StringThing = "thing"
192 xs.ByteThing = 42
193 xs.I32Thing = 4242
194 xs.I64Thing = 424242
taozle5c302e02017-07-23 15:21:44 +0200195 xsret, err := client.TestStruct(defaultCtx, xs)
Jens Geyerf4598682014-05-08 23:18:44 +0200196 if err != nil {
197 t.Errorf("Unexpected error in TestStruct() call: ", err)
198 }
199 if *xs != *xsret {
200 t.Errorf("Unexpected TestStruct() result expected %#v, got %#v ", xs, xsret)
201 }
202
203 x2 := thrifttest.NewXtruct2()
204 x2.StructThing = xs
taozle5c302e02017-07-23 15:21:44 +0200205 x2ret, err := client.TestNest(defaultCtx, x2)
Jens Geyerf4598682014-05-08 23:18:44 +0200206 if err != nil {
207 t.Errorf("Unexpected error in TestNest() call: ", err)
208 }
209 if !reflect.DeepEqual(x2, x2ret) {
210 t.Errorf("Unexpected TestNest() result expected %#v, got %#v ", x2, x2ret)
211 }
212
213 m := map[int32]int32{1: 2, 3: 4, 5: 42}
taozle5c302e02017-07-23 15:21:44 +0200214 mret, err := client.TestMap(defaultCtx, m)
Jens Geyerf4598682014-05-08 23:18:44 +0200215 if err != nil {
216 t.Errorf("Unexpected error in TestMap() call: ", err)
217 }
218 if !reflect.DeepEqual(m, mret) {
219 t.Errorf("Unexpected TestMap() result expected %#v, got %#v ", m, mret)
220 }
221
222 sm := map[string]string{"a": "2", "b": "blah", "some": "thing"}
taozle5c302e02017-07-23 15:21:44 +0200223 smret, err := client.TestStringMap(defaultCtx, sm)
Jens Geyerf4598682014-05-08 23:18:44 +0200224 if err != nil {
225 t.Errorf("Unexpected error in TestStringMap() call: ", err)
226 }
227 if !reflect.DeepEqual(sm, smret) {
228 t.Errorf("Unexpected TestStringMap() result expected %#v, got %#v ", sm, smret)
229 }
230
D. Can Celasun43fb34d2017-01-15 10:53:19 +0100231 s := []int32{1, 2, 42}
taozle5c302e02017-07-23 15:21:44 +0200232 sret, err := client.TestSet(defaultCtx, s)
Jens Geyerf4598682014-05-08 23:18:44 +0200233 if err != nil {
234 t.Errorf("Unexpected error in TestSet() call: ", err)
235 }
D. Can Celasun43fb34d2017-01-15 10:53:19 +0100236 // Sets can be in any order, but Go slices are ordered, so reflect.DeepEqual won't work.
237 stemp := map[int32]struct{}{}
238 for _, val := range s {
239 stemp[val] = struct{}{}
240 }
241 for _, val := range sret {
242 if _, ok := stemp[val]; !ok {
243 t.Fatalf("Unexpected TestSet() result expected %#v, got %#v ", s, sret)
244 }
Jens Geyerf4598682014-05-08 23:18:44 +0200245 }
246
247 l := []int32{1, 2, 42}
taozle5c302e02017-07-23 15:21:44 +0200248 lret, err := client.TestList(defaultCtx, l)
Jens Geyerf4598682014-05-08 23:18:44 +0200249 if err != nil {
250 t.Errorf("Unexpected error in TestList() call: ", err)
251 }
252 if !reflect.DeepEqual(l, lret) {
D. Can Celasun43fb34d2017-01-15 10:53:19 +0100253 t.Errorf("Unexpected TestList() result expected %#v, got %#v ", l, lret)
Jens Geyerf4598682014-05-08 23:18:44 +0200254 }
255
taozle5c302e02017-07-23 15:21:44 +0200256 eret, err := client.TestEnum(defaultCtx, thrifttest.Numberz_TWO)
Jens Geyerf4598682014-05-08 23:18:44 +0200257 if err != nil {
258 t.Errorf("Unexpected error in TestEnum() call: ", err)
259 }
260 if eret != thrifttest.Numberz_TWO {
261 t.Errorf("Unexpected TestEnum() result expected %#v, got %#v ", thrifttest.Numberz_TWO, eret)
262 }
263
taozle5c302e02017-07-23 15:21:44 +0200264 tret, err := client.TestTypedef(defaultCtx, thrifttest.UserId(42))
Jens Geyerf4598682014-05-08 23:18:44 +0200265 if err != nil {
266 t.Errorf("Unexpected error in TestTypedef() call: ", err)
267 }
268 if tret != thrifttest.UserId(42) {
269 t.Errorf("Unexpected TestTypedef() result expected %#v, got %#v ", thrifttest.UserId(42), tret)
270 }
271
taozle5c302e02017-07-23 15:21:44 +0200272 mapmap, err := client.TestMapMap(defaultCtx, 42)
Jens Geyerf4598682014-05-08 23:18:44 +0200273 if err != nil {
274 t.Errorf("Unexpected error in TestMapmap() call: ", err)
275 }
276 if !reflect.DeepEqual(mapmap, rmapmap) {
277 t.Errorf("Unexpected TestMapmap() result expected %#v, got %#v ", rmapmap, mapmap)
278 }
279
taozle5c302e02017-07-23 15:21:44 +0200280 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 +0200281 if err != nil {
D. Can Celasun4f77ab82017-09-21 15:21:00 +0200282 t.Errorf("Unexpected error in TestMulti() call: %v", err)
Jens Geyerf4598682014-05-08 23:18:44 +0200283 }
284 if !reflect.DeepEqual(xxs, xxsret) {
285 t.Errorf("Unexpected TestMulti() result expected %#v, got %#v ", xxs, xxsret)
286 }
287
taozle5c302e02017-07-23 15:21:44 +0200288 err = client.TestException(defaultCtx, "some")
Jens Geyerf4598682014-05-08 23:18:44 +0200289 if err == nil {
290 t.Errorf("Expecting exception in TestException() call")
291 }
292 if !reflect.DeepEqual(err, xcept) {
293 t.Errorf("Unexpected TestException() result expected %#v, got %#v ", xcept, err)
294 }
295
296 // TODO: connection is being closed on this
taozle5c302e02017-07-23 15:21:44 +0200297 err = client.TestException(defaultCtx, "TException")
D. Can Celasun4f77ab82017-09-21 15:21:00 +0200298 if err == nil {
299 t.Error("expected exception got nil")
300 } else if tex, ok := err.(thrift.TApplicationException); !ok {
301 t.Errorf("Unexpected TestException() result expected ApplicationError, got %T ", err)
302 } else if tex.TypeId() != thrift.INTERNAL_ERROR {
303 t.Errorf("expected internal_error got %v", tex.TypeId())
Jens Geyerf4598682014-05-08 23:18:44 +0200304 }
305
taozle5c302e02017-07-23 15:21:44 +0200306 ign, err := client.TestMultiException(defaultCtx, "Xception", "ignoreme")
Jens Geyerf4598682014-05-08 23:18:44 +0200307 if ign != nil || err == nil {
308 t.Errorf("Expecting exception in TestMultiException() call")
309 }
310 if !reflect.DeepEqual(err, &thrifttest.Xception{ErrorCode: 1001, Message: "This is an Xception"}) {
311 t.Errorf("Unexpected TestMultiException() %#v ", err)
312 }
313
taozle5c302e02017-07-23 15:21:44 +0200314 ign, err = client.TestMultiException(defaultCtx, "Xception2", "ignoreme")
Jens Geyerf4598682014-05-08 23:18:44 +0200315 if ign != nil || err == nil {
316 t.Errorf("Expecting exception in TestMultiException() call")
317 }
318 expecting := &thrifttest.Xception2{ErrorCode: 2002, StructThing: &thrifttest.Xtruct{StringThing: "This is an Xception2"}}
319
320 if !reflect.DeepEqual(err, expecting) {
321 t.Errorf("Unexpected TestMultiException() %#v ", err)
322 }
323
taozle5c302e02017-07-23 15:21:44 +0200324 err = client.TestOneway(defaultCtx, 2)
Jens Geyerf4598682014-05-08 23:18:44 +0200325 if err != nil {
326 t.Errorf("Unexpected error in TestOneway() call: ", err)
327 }
328
329 //Make sure the connection still alive
taozle5c302e02017-07-23 15:21:44 +0200330 if err = client.TestVoid(defaultCtx); err != nil {
Jens Geyerf4598682014-05-08 23:18:44 +0200331 t.Errorf("Unexpected error in TestVoid() call: ", err)
332 }
333}