blob: 26fa7afb8003b6291658d0c9bfd3a75f17d666d3 [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{
42 {"127.0.0.1", 9090, "", "", "binary", false},
43 {"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 {
65 return
66 }
67 go server.AcceptLoop()
68 server.Serve()
Jens Geyerf4598682014-05-08 23:18:44 +020069 if err != nil {
70 t.Errorf("Unable to start server", err)
71 t.FailNow()
72 }
73 defer server.Stop()
74 client, err := StartClient(unit.host, unit.port, unit.domain_socket, unit.transport, unit.protocol, unit.ssl)
75 if err != nil {
76 t.Errorf("Unable to start client", err)
77 t.FailNow()
78 }
79 defer client.Transport.Close()
80 callEverythingWithMock(t, client, handler)
81}
82
83var rmapmap = map[int32]map[int32]int32{
84 -4: map[int32]int32{-4: -4, -3: -3, -2: -2, -1: -1},
85 4: map[int32]int32{4: 4, 3: 3, 2: 2, 1: 1},
86}
87
88var xxs = &thrifttest.Xtruct{
89 StringThing: "Hello2",
90 ByteThing: 42,
91 I32Thing: 4242,
92 I64Thing: 424242,
93}
94
95var xcept = &thrifttest.Xception{ErrorCode: 1001, Message: "some"}
96
97func callEverythingWithMock(t *testing.T, client *thrifttest.ThriftTestClient, handler *MockThriftTest) {
98 gomock.InOrder(
99 handler.EXPECT().TestVoid(),
100 handler.EXPECT().TestString("thing").Return("thing", nil),
Nobuaki Sukegawaa649e742015-09-21 13:53:25 +0900101 handler.EXPECT().TestBool(true).Return(true, nil),
102 handler.EXPECT().TestBool(false).Return(false, nil),
Jens Geyerf4598682014-05-08 23:18:44 +0200103 handler.EXPECT().TestByte(int8(42)).Return(int8(42), nil),
104 handler.EXPECT().TestI32(int32(4242)).Return(int32(4242), nil),
105 handler.EXPECT().TestI64(int64(424242)).Return(int64(424242), nil),
Jens Geyer8bcfdd92014-12-14 03:14:26 +0100106 // TODO: add TestBinary()
Jens Geyerf4598682014-05-08 23:18:44 +0200107 handler.EXPECT().TestDouble(float64(42.42)).Return(float64(42.42), nil),
108 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),
109 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),
110 handler.EXPECT().TestMap(map[int32]int32{1: 2, 3: 4, 5: 42}).Return(map[int32]int32{1: 2, 3: 4, 5: 42}, nil),
111 handler.EXPECT().TestStringMap(map[string]string{"a": "2", "b": "blah", "some": "thing"}).Return(map[string]string{"a": "2", "b": "blah", "some": "thing"}, nil),
112 handler.EXPECT().TestSet(map[int32]bool{1: true, 2: true, 42: true}).Return(map[int32]bool{1: true, 2: true, 42: true}, nil),
113 handler.EXPECT().TestList([]int32{1, 2, 42}).Return([]int32{1, 2, 42}, nil),
114 handler.EXPECT().TestEnum(thrifttest.Numberz_TWO).Return(thrifttest.Numberz_TWO, nil),
115 handler.EXPECT().TestTypedef(thrifttest.UserId(42)).Return(thrifttest.UserId(42), nil),
116 handler.EXPECT().TestMapMap(int32(42)).Return(rmapmap, nil),
Nobuaki Sukegawa2fab3de2015-08-16 15:42:58 +0900117 // TODO: not testing insanity
Jens Geyerf4598682014-05-08 23:18:44 +0200118 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),
119 handler.EXPECT().TestException("some").Return(xcept),
120 handler.EXPECT().TestException("TException").Return(errors.New("Just random exception")),
121 handler.EXPECT().TestMultiException("Xception", "ignoreme").Return(nil, &thrifttest.Xception{ErrorCode: 1001, Message: "This is an Xception"}),
122 handler.EXPECT().TestMultiException("Xception2", "ignoreme").Return(nil, &thrifttest.Xception2{ErrorCode: 2002, StructThing: &thrifttest.Xtruct{StringThing: "This is an Xception2"}}),
123 handler.EXPECT().TestOneway(int32(2)).Return(nil),
124 handler.EXPECT().TestVoid(),
125 )
126 var err error
127 if err = client.TestVoid(); err != nil {
128 t.Errorf("Unexpected error in TestVoid() call: ", err)
129 }
130
131 thing, err := client.TestString("thing")
132 if err != nil {
133 t.Errorf("Unexpected error in TestString() call: ", err)
134 }
135 if thing != "thing" {
136 t.Errorf("Unexpected TestString() result, expected 'thing' got '%s' ", thing)
137 }
138
Nobuaki Sukegawaa649e742015-09-21 13:53:25 +0900139 bl, err := client.TestBool(true)
140 if err != nil {
141 t.Errorf("Unexpected error in TestBool() call: ", err)
142 }
143 if !bl {
144 t.Errorf("Unexpected TestBool() result expected true, got %f ", bl)
145 }
146 bl, err = client.TestBool(false)
147 if err != nil {
148 t.Errorf("Unexpected error in TestBool() call: ", err)
149 }
150 if bl {
151 t.Errorf("Unexpected TestBool() result expected false, got %f ", bl)
152 }
153
Jens Geyerf4598682014-05-08 23:18:44 +0200154 b, err := client.TestByte(42)
155 if err != nil {
156 t.Errorf("Unexpected error in TestByte() call: ", err)
157 }
158 if b != 42 {
159 t.Errorf("Unexpected TestByte() result expected 42, got %d ", b)
160 }
161
162 i32, err := client.TestI32(4242)
163 if err != nil {
164 t.Errorf("Unexpected error in TestI32() call: ", err)
165 }
166 if i32 != 4242 {
167 t.Errorf("Unexpected TestI32() result expected 4242, got %d ", i32)
168 }
169
170 i64, err := client.TestI64(424242)
171 if err != nil {
172 t.Errorf("Unexpected error in TestI64() call: ", err)
173 }
174 if i64 != 424242 {
175 t.Errorf("Unexpected TestI64() result expected 424242, got %d ", i64)
176 }
177
178 d, err := client.TestDouble(42.42)
179 if err != nil {
180 t.Errorf("Unexpected error in TestDouble() call: ", err)
181 }
182 if d != 42.42 {
183 t.Errorf("Unexpected TestDouble() result expected 42.42, got %f ", d)
184 }
185
Jens Geyer8bcfdd92014-12-14 03:14:26 +0100186 // TODO: add TestBinary() call
claudemirof8ca0552016-01-10 23:31:30 -0200187
Jens Geyerf4598682014-05-08 23:18:44 +0200188 xs := thrifttest.NewXtruct()
189 xs.StringThing = "thing"
190 xs.ByteThing = 42
191 xs.I32Thing = 4242
192 xs.I64Thing = 424242
193 xsret, err := client.TestStruct(xs)
194 if err != nil {
195 t.Errorf("Unexpected error in TestStruct() call: ", err)
196 }
197 if *xs != *xsret {
198 t.Errorf("Unexpected TestStruct() result expected %#v, got %#v ", xs, xsret)
199 }
200
201 x2 := thrifttest.NewXtruct2()
202 x2.StructThing = xs
203 x2ret, err := client.TestNest(x2)
204 if err != nil {
205 t.Errorf("Unexpected error in TestNest() call: ", err)
206 }
207 if !reflect.DeepEqual(x2, x2ret) {
208 t.Errorf("Unexpected TestNest() result expected %#v, got %#v ", x2, x2ret)
209 }
210
211 m := map[int32]int32{1: 2, 3: 4, 5: 42}
212 mret, err := client.TestMap(m)
213 if err != nil {
214 t.Errorf("Unexpected error in TestMap() call: ", err)
215 }
216 if !reflect.DeepEqual(m, mret) {
217 t.Errorf("Unexpected TestMap() result expected %#v, got %#v ", m, mret)
218 }
219
220 sm := map[string]string{"a": "2", "b": "blah", "some": "thing"}
221 smret, err := client.TestStringMap(sm)
222 if err != nil {
223 t.Errorf("Unexpected error in TestStringMap() call: ", err)
224 }
225 if !reflect.DeepEqual(sm, smret) {
226 t.Errorf("Unexpected TestStringMap() result expected %#v, got %#v ", sm, smret)
227 }
228
229 s := map[int32]bool{1: true, 2: true, 42: true}
230 sret, err := client.TestSet(s)
231 if err != nil {
232 t.Errorf("Unexpected error in TestSet() call: ", err)
233 }
234 if !reflect.DeepEqual(s, sret) {
235 t.Errorf("Unexpected TestSet() result expected %#v, got %#v ", s, sret)
236 }
237
238 l := []int32{1, 2, 42}
239 lret, err := client.TestList(l)
240 if err != nil {
241 t.Errorf("Unexpected error in TestList() call: ", err)
242 }
243 if !reflect.DeepEqual(l, lret) {
244 t.Errorf("Unexpected TestSet() result expected %#v, got %#v ", l, lret)
245 }
246
247 eret, err := client.TestEnum(thrifttest.Numberz_TWO)
248 if err != nil {
249 t.Errorf("Unexpected error in TestEnum() call: ", err)
250 }
251 if eret != thrifttest.Numberz_TWO {
252 t.Errorf("Unexpected TestEnum() result expected %#v, got %#v ", thrifttest.Numberz_TWO, eret)
253 }
254
255 tret, err := client.TestTypedef(thrifttest.UserId(42))
256 if err != nil {
257 t.Errorf("Unexpected error in TestTypedef() call: ", err)
258 }
259 if tret != thrifttest.UserId(42) {
260 t.Errorf("Unexpected TestTypedef() result expected %#v, got %#v ", thrifttest.UserId(42), tret)
261 }
262
263 mapmap, err := client.TestMapMap(42)
264 if err != nil {
265 t.Errorf("Unexpected error in TestMapmap() call: ", err)
266 }
267 if !reflect.DeepEqual(mapmap, rmapmap) {
268 t.Errorf("Unexpected TestMapmap() result expected %#v, got %#v ", rmapmap, mapmap)
269 }
270
271 xxsret, err := client.TestMulti(42, 4242, 424242, map[int16]string{1: "blah", 2: "thing"}, thrifttest.Numberz_EIGHT, thrifttest.UserId(24))
272 if err != nil {
273 t.Errorf("Unexpected error in TestMulti() call: ", err)
274 }
275 if !reflect.DeepEqual(xxs, xxsret) {
276 t.Errorf("Unexpected TestMulti() result expected %#v, got %#v ", xxs, xxsret)
277 }
278
279 err = client.TestException("some")
280 if err == nil {
281 t.Errorf("Expecting exception in TestException() call")
282 }
283 if !reflect.DeepEqual(err, xcept) {
284 t.Errorf("Unexpected TestException() result expected %#v, got %#v ", xcept, err)
285 }
286
287 // TODO: connection is being closed on this
288 err = client.TestException("TException")
289 tex, ok := err.(thrift.TApplicationException)
290 if err == nil || !ok || tex.TypeId() != thrift.INTERNAL_ERROR {
291 t.Errorf("Unexpected TestException() result expected ApplicationError, got %#v ", err)
292 }
293
294 ign, err := client.TestMultiException("Xception", "ignoreme")
295 if ign != nil || err == nil {
296 t.Errorf("Expecting exception in TestMultiException() call")
297 }
298 if !reflect.DeepEqual(err, &thrifttest.Xception{ErrorCode: 1001, Message: "This is an Xception"}) {
299 t.Errorf("Unexpected TestMultiException() %#v ", err)
300 }
301
302 ign, err = client.TestMultiException("Xception2", "ignoreme")
303 if ign != nil || err == nil {
304 t.Errorf("Expecting exception in TestMultiException() call")
305 }
306 expecting := &thrifttest.Xception2{ErrorCode: 2002, StructThing: &thrifttest.Xtruct{StringThing: "This is an Xception2"}}
307
308 if !reflect.DeepEqual(err, expecting) {
309 t.Errorf("Unexpected TestMultiException() %#v ", err)
310 }
311
312 err = client.TestOneway(2)
313 if err != nil {
314 t.Errorf("Unexpected error in TestOneway() call: ", err)
315 }
316
317 //Make sure the connection still alive
318 if err = client.TestVoid(); err != nil {
319 t.Errorf("Unexpected error in TestVoid() call: ", err)
320 }
321}