blob: 0080ead63dca9a3d9fc13b05b8a2d44c002d0ffa [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),
92 handler.EXPECT().TestByte(int8(42)).Return(int8(42), nil),
93 handler.EXPECT().TestI32(int32(4242)).Return(int32(4242), nil),
94 handler.EXPECT().TestI64(int64(424242)).Return(int64(424242), nil),
Jens Geyer8bcfdd92014-12-14 03:14:26 +010095 // TODO: add TestBinary()
Jens Geyerf4598682014-05-08 23:18:44 +020096 handler.EXPECT().TestDouble(float64(42.42)).Return(float64(42.42), nil),
97 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),
98 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),
99 handler.EXPECT().TestMap(map[int32]int32{1: 2, 3: 4, 5: 42}).Return(map[int32]int32{1: 2, 3: 4, 5: 42}, nil),
100 handler.EXPECT().TestStringMap(map[string]string{"a": "2", "b": "blah", "some": "thing"}).Return(map[string]string{"a": "2", "b": "blah", "some": "thing"}, nil),
101 handler.EXPECT().TestSet(map[int32]bool{1: true, 2: true, 42: true}).Return(map[int32]bool{1: true, 2: true, 42: true}, nil),
102 handler.EXPECT().TestList([]int32{1, 2, 42}).Return([]int32{1, 2, 42}, nil),
103 handler.EXPECT().TestEnum(thrifttest.Numberz_TWO).Return(thrifttest.Numberz_TWO, nil),
104 handler.EXPECT().TestTypedef(thrifttest.UserId(42)).Return(thrifttest.UserId(42), nil),
105 handler.EXPECT().TestMapMap(int32(42)).Return(rmapmap, nil),
106 //not testing insanity
107 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),
108 handler.EXPECT().TestException("some").Return(xcept),
109 handler.EXPECT().TestException("TException").Return(errors.New("Just random exception")),
110 handler.EXPECT().TestMultiException("Xception", "ignoreme").Return(nil, &thrifttest.Xception{ErrorCode: 1001, Message: "This is an Xception"}),
111 handler.EXPECT().TestMultiException("Xception2", "ignoreme").Return(nil, &thrifttest.Xception2{ErrorCode: 2002, StructThing: &thrifttest.Xtruct{StringThing: "This is an Xception2"}}),
112 handler.EXPECT().TestOneway(int32(2)).Return(nil),
113 handler.EXPECT().TestVoid(),
114 )
115 var err error
116 if err = client.TestVoid(); err != nil {
117 t.Errorf("Unexpected error in TestVoid() call: ", err)
118 }
119
120 thing, err := client.TestString("thing")
121 if err != nil {
122 t.Errorf("Unexpected error in TestString() call: ", err)
123 }
124 if thing != "thing" {
125 t.Errorf("Unexpected TestString() result, expected 'thing' got '%s' ", thing)
126 }
127
128 b, err := client.TestByte(42)
129 if err != nil {
130 t.Errorf("Unexpected error in TestByte() call: ", err)
131 }
132 if b != 42 {
133 t.Errorf("Unexpected TestByte() result expected 42, got %d ", b)
134 }
135
136 i32, err := client.TestI32(4242)
137 if err != nil {
138 t.Errorf("Unexpected error in TestI32() call: ", err)
139 }
140 if i32 != 4242 {
141 t.Errorf("Unexpected TestI32() result expected 4242, got %d ", i32)
142 }
143
144 i64, err := client.TestI64(424242)
145 if err != nil {
146 t.Errorf("Unexpected error in TestI64() call: ", err)
147 }
148 if i64 != 424242 {
149 t.Errorf("Unexpected TestI64() result expected 424242, got %d ", i64)
150 }
151
152 d, err := client.TestDouble(42.42)
153 if err != nil {
154 t.Errorf("Unexpected error in TestDouble() call: ", err)
155 }
156 if d != 42.42 {
157 t.Errorf("Unexpected TestDouble() result expected 42.42, got %f ", d)
158 }
159
Jens Geyer8bcfdd92014-12-14 03:14:26 +0100160 // TODO: add TestBinary() call
161
Jens Geyerf4598682014-05-08 23:18:44 +0200162 xs := thrifttest.NewXtruct()
163 xs.StringThing = "thing"
164 xs.ByteThing = 42
165 xs.I32Thing = 4242
166 xs.I64Thing = 424242
167 xsret, err := client.TestStruct(xs)
168 if err != nil {
169 t.Errorf("Unexpected error in TestStruct() call: ", err)
170 }
171 if *xs != *xsret {
172 t.Errorf("Unexpected TestStruct() result expected %#v, got %#v ", xs, xsret)
173 }
174
175 x2 := thrifttest.NewXtruct2()
176 x2.StructThing = xs
177 x2ret, err := client.TestNest(x2)
178 if err != nil {
179 t.Errorf("Unexpected error in TestNest() call: ", err)
180 }
181 if !reflect.DeepEqual(x2, x2ret) {
182 t.Errorf("Unexpected TestNest() result expected %#v, got %#v ", x2, x2ret)
183 }
184
185 m := map[int32]int32{1: 2, 3: 4, 5: 42}
186 mret, err := client.TestMap(m)
187 if err != nil {
188 t.Errorf("Unexpected error in TestMap() call: ", err)
189 }
190 if !reflect.DeepEqual(m, mret) {
191 t.Errorf("Unexpected TestMap() result expected %#v, got %#v ", m, mret)
192 }
193
194 sm := map[string]string{"a": "2", "b": "blah", "some": "thing"}
195 smret, err := client.TestStringMap(sm)
196 if err != nil {
197 t.Errorf("Unexpected error in TestStringMap() call: ", err)
198 }
199 if !reflect.DeepEqual(sm, smret) {
200 t.Errorf("Unexpected TestStringMap() result expected %#v, got %#v ", sm, smret)
201 }
202
203 s := map[int32]bool{1: true, 2: true, 42: true}
204 sret, err := client.TestSet(s)
205 if err != nil {
206 t.Errorf("Unexpected error in TestSet() call: ", err)
207 }
208 if !reflect.DeepEqual(s, sret) {
209 t.Errorf("Unexpected TestSet() result expected %#v, got %#v ", s, sret)
210 }
211
212 l := []int32{1, 2, 42}
213 lret, err := client.TestList(l)
214 if err != nil {
215 t.Errorf("Unexpected error in TestList() call: ", err)
216 }
217 if !reflect.DeepEqual(l, lret) {
218 t.Errorf("Unexpected TestSet() result expected %#v, got %#v ", l, lret)
219 }
220
221 eret, err := client.TestEnum(thrifttest.Numberz_TWO)
222 if err != nil {
223 t.Errorf("Unexpected error in TestEnum() call: ", err)
224 }
225 if eret != thrifttest.Numberz_TWO {
226 t.Errorf("Unexpected TestEnum() result expected %#v, got %#v ", thrifttest.Numberz_TWO, eret)
227 }
228
229 tret, err := client.TestTypedef(thrifttest.UserId(42))
230 if err != nil {
231 t.Errorf("Unexpected error in TestTypedef() call: ", err)
232 }
233 if tret != thrifttest.UserId(42) {
234 t.Errorf("Unexpected TestTypedef() result expected %#v, got %#v ", thrifttest.UserId(42), tret)
235 }
236
237 mapmap, err := client.TestMapMap(42)
238 if err != nil {
239 t.Errorf("Unexpected error in TestMapmap() call: ", err)
240 }
241 if !reflect.DeepEqual(mapmap, rmapmap) {
242 t.Errorf("Unexpected TestMapmap() result expected %#v, got %#v ", rmapmap, mapmap)
243 }
244
245 xxsret, err := client.TestMulti(42, 4242, 424242, map[int16]string{1: "blah", 2: "thing"}, thrifttest.Numberz_EIGHT, thrifttest.UserId(24))
246 if err != nil {
247 t.Errorf("Unexpected error in TestMulti() call: ", err)
248 }
249 if !reflect.DeepEqual(xxs, xxsret) {
250 t.Errorf("Unexpected TestMulti() result expected %#v, got %#v ", xxs, xxsret)
251 }
252
253 err = client.TestException("some")
254 if err == nil {
255 t.Errorf("Expecting exception in TestException() call")
256 }
257 if !reflect.DeepEqual(err, xcept) {
258 t.Errorf("Unexpected TestException() result expected %#v, got %#v ", xcept, err)
259 }
260
261 // TODO: connection is being closed on this
262 err = client.TestException("TException")
263 tex, ok := err.(thrift.TApplicationException)
264 if err == nil || !ok || tex.TypeId() != thrift.INTERNAL_ERROR {
265 t.Errorf("Unexpected TestException() result expected ApplicationError, got %#v ", err)
266 }
267
268 ign, err := client.TestMultiException("Xception", "ignoreme")
269 if ign != nil || err == nil {
270 t.Errorf("Expecting exception in TestMultiException() call")
271 }
272 if !reflect.DeepEqual(err, &thrifttest.Xception{ErrorCode: 1001, Message: "This is an Xception"}) {
273 t.Errorf("Unexpected TestMultiException() %#v ", err)
274 }
275
276 ign, err = client.TestMultiException("Xception2", "ignoreme")
277 if ign != nil || err == nil {
278 t.Errorf("Expecting exception in TestMultiException() call")
279 }
280 expecting := &thrifttest.Xception2{ErrorCode: 2002, StructThing: &thrifttest.Xtruct{StringThing: "This is an Xception2"}}
281
282 if !reflect.DeepEqual(err, expecting) {
283 t.Errorf("Unexpected TestMultiException() %#v ", err)
284 }
285
286 err = client.TestOneway(2)
287 if err != nil {
288 t.Errorf("Unexpected error in TestOneway() call: ", err)
289 }
290
291 //Make sure the connection still alive
292 if err = client.TestVoid(); err != nil {
293 t.Errorf("Unexpected error in TestVoid() call: ", err)
294 }
295}