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
10 * http://www.apache.org/licenses/LICENSE-2.0
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
31 "github.com/golang/mock/gomock"
34 type test_unit struct {
43 var units = []test_unit{
44 {"127.0.0.1", 9095, "", "", "binary", false},
45 {"127.0.0.1", 9091, "", "", "compact", false},
46 {"127.0.0.1", 9092, "", "", "binary", true},
47 {"127.0.0.1", 9093, "", "", "compact", true},
50 func TestAllConnection(t *testing.T) {
51 certPath = "../../../keys"
52 wg := &sync.WaitGroup{}
54 for _, unit := range units {
55 go func(u test_unit) {
63 func doUnit(t *testing.T, unit *test_unit) {
64 ctrl := gomock.NewController(t)
66 handler := NewMockThriftTest(ctrl)
68 processor, serverTransport, transportFactory, protocolFactory, err := GetServerParams(unit.host, unit.port, unit.domain_socket, unit.transport, unit.protocol, unit.ssl, "../../../keys", handler)
70 server := thrift.NewTSimpleServer4(processor, serverTransport, transportFactory, protocolFactory)
71 if err = server.Listen(); err != nil {
72 t.Errorf("Unable to start server: %v", err)
75 go server.AcceptLoop()
77 client, trans, err := StartClient(unit.host, unit.port, unit.domain_socket, unit.transport, unit.protocol, unit.ssl)
79 t.Errorf("Unable to start client: %v", err)
83 callEverythingWithMock(t, client, handler)
86 var rmapmap = map[int32]map[int32]int32{
87 -4: map[int32]int32{-4: -4, -3: -3, -2: -2, -1: -1},
88 4: map[int32]int32{4: 4, 3: 3, 2: 2, 1: 1},
91 var xxs = &thrifttest.Xtruct{
92 StringThing: "Hello2",
98 var xcept = &thrifttest.Xception{ErrorCode: 1001, Message: "some"}
99 var defaultCtx = context.Background()
101 func callEverythingWithMock(t *testing.T, client *thrifttest.ThriftTestClient, handler *MockThriftTest) {
103 handler.EXPECT().TestVoid(gomock.Any()),
104 handler.EXPECT().TestString(gomock.Any(), "thing").Return("thing", nil),
105 handler.EXPECT().TestBool(gomock.Any(), true).Return(true, nil),
106 handler.EXPECT().TestBool(gomock.Any(), false).Return(false, nil),
107 handler.EXPECT().TestByte(gomock.Any(), int8(42)).Return(int8(42), nil),
108 handler.EXPECT().TestI32(gomock.Any(), int32(4242)).Return(int32(4242), nil),
109 handler.EXPECT().TestI64(gomock.Any(), int64(424242)).Return(int64(424242), nil),
110 // TODO: add TestBinary()
111 handler.EXPECT().TestDouble(gomock.Any(), float64(42.42)).Return(float64(42.42), nil),
112 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),
113 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),
114 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),
115 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),
116 handler.EXPECT().TestSet(gomock.Any(), []int32{1, 2, 42}).Return([]int32{1, 2, 42}, nil),
117 handler.EXPECT().TestList(gomock.Any(), []int32{1, 2, 42}).Return([]int32{1, 2, 42}, nil),
118 handler.EXPECT().TestEnum(gomock.Any(), thrifttest.Numberz_TWO).Return(thrifttest.Numberz_TWO, nil),
119 handler.EXPECT().TestTypedef(gomock.Any(), thrifttest.UserId(42)).Return(thrifttest.UserId(42), nil),
120 handler.EXPECT().TestMapMap(gomock.Any(), int32(42)).Return(rmapmap, nil),
121 // TODO: not testing insanity
122 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),
123 handler.EXPECT().TestException(gomock.Any(), "some").Return(xcept),
124 handler.EXPECT().TestException(gomock.Any(), "TException").Return(errors.New("Just random exception")),
125 handler.EXPECT().TestMultiException(gomock.Any(), "Xception", "ignoreme").Return(nil, &thrifttest.Xception{ErrorCode: 1001, Message: "This is an Xception"}),
126 handler.EXPECT().TestMultiException(gomock.Any(), "Xception2", "ignoreme").Return(nil, &thrifttest.Xception2{ErrorCode: 2002, StructThing: &thrifttest.Xtruct{StringThing: "This is an Xception2"}}),
127 handler.EXPECT().TestOneway(gomock.Any(), int32(2)).Return(nil),
128 handler.EXPECT().TestVoid(gomock.Any()),
131 if err = client.TestVoid(defaultCtx); err != nil {
132 t.Errorf("Unexpected error in TestVoid() call: %s", err)
135 thing, err := client.TestString(defaultCtx, "thing")
137 t.Errorf("Unexpected error in TestString() call: %s", err)
139 if thing != "thing" {
140 t.Errorf("Unexpected TestString() result, expected 'thing' got '%s' ", thing)
143 bl, err := client.TestBool(defaultCtx, true)
145 t.Errorf("Unexpected error in TestBool() call: %s", err)
148 t.Errorf("Unexpected TestBool() result expected true, got %v ", bl)
150 bl, err = client.TestBool(defaultCtx, false)
152 t.Errorf("Unexpected error in TestBool() call: %s", err)
155 t.Errorf("Unexpected TestBool() result expected false, got %v ", bl)
158 b, err := client.TestByte(defaultCtx, 42)
160 t.Errorf("Unexpected error in TestByte() call: %s", err)
163 t.Errorf("Unexpected TestByte() result expected 42, got %d ", b)
166 i32, err := client.TestI32(defaultCtx, 4242)
168 t.Errorf("Unexpected error in TestI32() call: %s", err)
171 t.Errorf("Unexpected TestI32() result expected 4242, got %d ", i32)
174 i64, err := client.TestI64(defaultCtx, 424242)
176 t.Errorf("Unexpected error in TestI64() call: %s", err)
179 t.Errorf("Unexpected TestI64() result expected 424242, got %d ", i64)
182 d, err := client.TestDouble(defaultCtx, 42.42)
184 t.Errorf("Unexpected error in TestDouble() call: %s", err)
187 t.Errorf("Unexpected TestDouble() result expected 42.42, got %f ", d)
190 // TODO: add TestBinary() call
192 xs := thrifttest.NewXtruct()
193 xs.StringThing = "thing"
197 xsret, err := client.TestStruct(defaultCtx, xs)
199 t.Errorf("Unexpected error in TestStruct() call: %s", err)
202 t.Errorf("Unexpected TestStruct() result expected %#v, got %#v ", xs, xsret)
205 x2 := thrifttest.NewXtruct2()
207 x2ret, err := client.TestNest(defaultCtx, x2)
209 t.Errorf("Unexpected error in TestNest() call: %s", err)
211 if !reflect.DeepEqual(x2, x2ret) {
212 t.Errorf("Unexpected TestNest() result expected %#v, got %#v ", x2, x2ret)
215 m := map[int32]int32{1: 2, 3: 4, 5: 42}
216 mret, err := client.TestMap(defaultCtx, m)
218 t.Errorf("Unexpected error in TestMap() call: %s", err)
220 if !reflect.DeepEqual(m, mret) {
221 t.Errorf("Unexpected TestMap() result expected %#v, got %#v ", m, mret)
224 sm := map[string]string{"a": "2", "b": "blah", "some": "thing"}
225 smret, err := client.TestStringMap(defaultCtx, sm)
227 t.Errorf("Unexpected error in TestStringMap() call: %s", err)
229 if !reflect.DeepEqual(sm, smret) {
230 t.Errorf("Unexpected TestStringMap() result expected %#v, got %#v ", sm, smret)
233 s := []int32{1, 2, 42}
234 sret, err := client.TestSet(defaultCtx, s)
236 t.Errorf("Unexpected error in TestSet() call: %s", err)
238 // Sets can be in any order, but Go slices are ordered, so reflect.DeepEqual won't work.
239 stemp := map[int32]struct{}{}
240 for _, val := range s {
241 stemp[val] = struct{}{}
243 for _, val := range sret {
244 if _, ok := stemp[val]; !ok {
245 t.Fatalf("Unexpected TestSet() result expected %#v, got %#v ", s, sret)
249 l := []int32{1, 2, 42}
250 lret, err := client.TestList(defaultCtx, l)
252 t.Errorf("Unexpected error in TestList() call: %s", err)
254 if !reflect.DeepEqual(l, lret) {
255 t.Errorf("Unexpected TestList() result expected %#v, got %#v ", l, lret)
258 eret, err := client.TestEnum(defaultCtx, thrifttest.Numberz_TWO)
260 t.Errorf("Unexpected error in TestEnum() call: %s", err)
262 if eret != thrifttest.Numberz_TWO {
263 t.Errorf("Unexpected TestEnum() result expected %#v, got %#v ", thrifttest.Numberz_TWO, eret)
266 tret, err := client.TestTypedef(defaultCtx, thrifttest.UserId(42))
268 t.Errorf("Unexpected error in TestTypedef() call: %s", err)
270 if tret != thrifttest.UserId(42) {
271 t.Errorf("Unexpected TestTypedef() result expected %#v, got %#v ", thrifttest.UserId(42), tret)
274 mapmap, err := client.TestMapMap(defaultCtx, 42)
276 t.Errorf("Unexpected error in TestMapmap() call: %s", err)
278 if !reflect.DeepEqual(mapmap, rmapmap) {
279 t.Errorf("Unexpected TestMapmap() result expected %#v, got %#v ", rmapmap, mapmap)
282 xxsret, err := client.TestMulti(defaultCtx, 42, 4242, 424242, map[int16]string{1: "blah", 2: "thing"}, thrifttest.Numberz_EIGHT, thrifttest.UserId(24))
284 t.Errorf("Unexpected error in TestMulti() call: %s", err)
286 if !reflect.DeepEqual(xxs, xxsret) {
287 t.Errorf("Unexpected TestMulti() result expected %#v, got %#v ", xxs, xxsret)
290 err = client.TestException(defaultCtx, "some")
292 t.Errorf("Expecting exception in TestException() call")
294 if !reflect.DeepEqual(err, xcept) {
295 t.Errorf("Unexpected TestException() result expected %#v, got %#v ", xcept, err)
298 // TODO: connection is being closed on this
299 err = client.TestException(defaultCtx, "TException")
301 t.Error("expected exception got nil")
302 } else if tex, ok := err.(thrift.TApplicationException); !ok {
303 t.Errorf("Unexpected TestException() result expected ApplicationError, got %T ", err)
304 } else if tex.TypeId() != thrift.INTERNAL_ERROR {
305 t.Errorf("expected internal_error got %v", tex.TypeId())
308 ign, err := client.TestMultiException(defaultCtx, "Xception", "ignoreme")
309 if ign != nil || err == nil {
310 t.Errorf("Expecting exception in TestMultiException() call")
312 if !reflect.DeepEqual(err, &thrifttest.Xception{ErrorCode: 1001, Message: "This is an Xception"}) {
313 t.Errorf("Unexpected TestMultiException() %#v ", err)
316 ign, err = client.TestMultiException(defaultCtx, "Xception2", "ignoreme")
317 if ign != nil || err == nil {
318 t.Errorf("Expecting exception in TestMultiException() call")
320 expecting := &thrifttest.Xception2{ErrorCode: 2002, StructThing: &thrifttest.Xtruct{StringThing: "This is an Xception2"}}
322 if !reflect.DeepEqual(err, expecting) {
323 t.Errorf("Unexpected TestMultiException() %#v ", err)
326 err = client.TestOneway(defaultCtx, 2)
328 t.Errorf("Unexpected error in TestOneway() call: %s", err)
331 //Make sure the connection still alive
332 if err = client.TestVoid(defaultCtx); err != nil {
333 t.Errorf("Unexpected error in TestVoid() call: %s", err)