blob: 31b9ee23ecaeb79d7be4a92917453eca10379fa9 [file] [log] [blame]
Jens Geyer5f9bdff2014-11-18 21:57:03 +01001/*
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 tests
21
22import (
John Boiles57852792018-01-05 14:37:05 -080023 "context"
Jens Geyer5f9bdff2014-11-18 21:57:03 +010024 "errors"
25 "thrift"
26 "thrifttest"
27 "time"
28)
29
30type SecondServiceHandler struct {
31}
32
33func NewSecondServiceHandler() *SecondServiceHandler {
34 return &SecondServiceHandler{}
35}
36
taozlec0d384a2017-07-17 18:40:42 +020037func (p *SecondServiceHandler) BlahBlah(ctx context.Context) (err error) {
Jens Geyer5f9bdff2014-11-18 21:57:03 +010038 return nil
39}
40
taozlec0d384a2017-07-17 18:40:42 +020041func (p *SecondServiceHandler) SecondtestString(ctx context.Context, thing string) (r string, err error) {
Jens Geyer5f9bdff2014-11-18 21:57:03 +010042 return thing, nil
43}
44
45type ThriftTestHandler struct {
46}
47
48func NewThriftTestHandler() *ThriftTestHandler {
49 return &ThriftTestHandler{}
50}
51
taozlec0d384a2017-07-17 18:40:42 +020052func (p *ThriftTestHandler) TestVoid(ctx context.Context) (err error) {
Jens Geyer5f9bdff2014-11-18 21:57:03 +010053 return nil
54}
55
taozlec0d384a2017-07-17 18:40:42 +020056func (p *ThriftTestHandler) TestString(ctx context.Context, thing string) (r string, err error) {
Jens Geyer5f9bdff2014-11-18 21:57:03 +010057 return thing, nil
58}
59
taozlec0d384a2017-07-17 18:40:42 +020060func (p *ThriftTestHandler) TestBool(ctx context.Context, thing bool) (r bool, err error) {
Nobuaki Sukegawaa649e742015-09-21 13:53:25 +090061 return thing, nil
62}
63
taozlec0d384a2017-07-17 18:40:42 +020064func (p *ThriftTestHandler) TestByte(ctx context.Context, thing int8) (r int8, err error) {
Jens Geyer5f9bdff2014-11-18 21:57:03 +010065 return thing, nil
66}
67
taozlec0d384a2017-07-17 18:40:42 +020068func (p *ThriftTestHandler) TestI32(ctx context.Context, thing int32) (r int32, err error) {
Jens Geyer5f9bdff2014-11-18 21:57:03 +010069 return thing, nil
70}
71
taozlec0d384a2017-07-17 18:40:42 +020072func (p *ThriftTestHandler) TestI64(ctx context.Context, thing int64) (r int64, err error) {
Jens Geyer5f9bdff2014-11-18 21:57:03 +010073 return thing, nil
74}
75
taozlec0d384a2017-07-17 18:40:42 +020076func (p *ThriftTestHandler) TestDouble(ctx context.Context, thing float64) (r float64, err error) {
Jens Geyer5f9bdff2014-11-18 21:57:03 +010077 return thing, nil
78}
79
taozlec0d384a2017-07-17 18:40:42 +020080func (p *ThriftTestHandler) TestBinary(ctx context.Context, thing []byte) (r []byte, err error) {
Jens Geyer8bcfdd92014-12-14 03:14:26 +010081 return thing, nil
82}
83
taozlec0d384a2017-07-17 18:40:42 +020084func (p *ThriftTestHandler) TestStruct(ctx context.Context, thing *thrifttest.Xtruct) (r *thrifttest.Xtruct, err error) {
Jens Geyer5f9bdff2014-11-18 21:57:03 +010085 return thing, nil
86}
87
taozlec0d384a2017-07-17 18:40:42 +020088func (p *ThriftTestHandler) TestNest(ctx context.Context, thing *thrifttest.Xtruct2) (r *thrifttest.Xtruct2, err error) {
Jens Geyer5f9bdff2014-11-18 21:57:03 +010089 return thing, nil
90}
91
taozlec0d384a2017-07-17 18:40:42 +020092func (p *ThriftTestHandler) TestMap(ctx context.Context, thing map[int32]int32) (r map[int32]int32, err error) {
Jens Geyer5f9bdff2014-11-18 21:57:03 +010093 return thing, nil
94}
95
taozlec0d384a2017-07-17 18:40:42 +020096func (p *ThriftTestHandler) TestStringMap(ctx context.Context, thing map[string]string) (r map[string]string, err error) {
Jens Geyer5f9bdff2014-11-18 21:57:03 +010097 return thing, nil
98}
99
taozlec0d384a2017-07-17 18:40:42 +0200100func (p *ThriftTestHandler) TestSet(ctx context.Context, thing []int32) (r []int32, err error) {
Jens Geyer5f9bdff2014-11-18 21:57:03 +0100101 return thing, nil
102}
103
taozlec0d384a2017-07-17 18:40:42 +0200104func (p *ThriftTestHandler) TestList(ctx context.Context, thing []int32) (r []int32, err error) {
Jens Geyer5f9bdff2014-11-18 21:57:03 +0100105 return thing, nil
106}
107
taozlec0d384a2017-07-17 18:40:42 +0200108func (p *ThriftTestHandler) TestEnum(ctx context.Context, thing thrifttest.Numberz) (r thrifttest.Numberz, err error) {
Jens Geyer5f9bdff2014-11-18 21:57:03 +0100109 return thing, nil
110}
111
taozlec0d384a2017-07-17 18:40:42 +0200112func (p *ThriftTestHandler) TestTypedef(ctx context.Context, thing thrifttest.UserId) (r thrifttest.UserId, err error) {
Jens Geyer5f9bdff2014-11-18 21:57:03 +0100113 return thing, nil
114}
115
taozlec0d384a2017-07-17 18:40:42 +0200116func (p *ThriftTestHandler) TestMapMap(ctx context.Context, hello int32) (r map[int32]map[int32]int32, err error) {
Jens Geyer5f9bdff2014-11-18 21:57:03 +0100117 r = make(map[int32]map[int32]int32)
118 pos := make(map[int32]int32)
119 neg := make(map[int32]int32)
120
121 for i := int32(1); i < 5; i++ {
122 pos[i] = i
123 neg[-i] = -i
124 }
125 r[4] = pos
126 r[-4] = neg
127
128 return r, nil
129}
130
taozlec0d384a2017-07-17 18:40:42 +0200131func (p *ThriftTestHandler) TestInsanity(ctx context.Context, argument *thrifttest.Insanity) (r map[thrifttest.UserId]map[thrifttest.Numberz]*thrifttest.Insanity, err error) {
Jens Geyer5f9bdff2014-11-18 21:57:03 +0100132 hello := thrifttest.NewXtruct()
133 hello.StringThing = "Hello2"
134 hello.ByteThing = 2
135 hello.I32Thing = 2
136 hello.I64Thing = 2
137
138 goodbye := thrifttest.NewXtruct()
139 goodbye.StringThing = "Goodbye4"
140 goodbye.ByteThing = 4
141 goodbye.I32Thing = 4
142 goodbye.I64Thing = 4
143
144 crazy := thrifttest.NewInsanity()
145 crazy.UserMap = make(map[thrifttest.Numberz]thrifttest.UserId)
146 crazy.UserMap[thrifttest.Numberz_EIGHT] = 8
147 crazy.UserMap[thrifttest.Numberz_FIVE] = 5
148 crazy.Xtructs = []*thrifttest.Xtruct{goodbye, hello}
149
150 first_map := make(map[thrifttest.Numberz]*thrifttest.Insanity)
151 second_map := make(map[thrifttest.Numberz]*thrifttest.Insanity)
152
153 first_map[thrifttest.Numberz_TWO] = crazy
154 first_map[thrifttest.Numberz_THREE] = crazy
155
156 looney := thrifttest.NewInsanity()
157 second_map[thrifttest.Numberz_SIX] = looney
158
159 var insane = make(map[thrifttest.UserId]map[thrifttest.Numberz]*thrifttest.Insanity)
160 insane[1] = first_map
161 insane[2] = second_map
162
163 return insane, nil
164}
165
taozlec0d384a2017-07-17 18:40:42 +0200166func (p *ThriftTestHandler) TestMulti(ctx context.Context, arg0 int8, arg1 int32, arg2 int64, arg3 map[int16]string, arg4 thrifttest.Numberz, arg5 thrifttest.UserId) (r *thrifttest.Xtruct, err error) {
Jens Geyer5f9bdff2014-11-18 21:57:03 +0100167 r = thrifttest.NewXtruct()
168 r.StringThing = "Hello2"
169 r.ByteThing = arg0
170 r.I32Thing = arg1
171 r.I64Thing = arg2
172 return r, nil
173}
174
taozlec0d384a2017-07-17 18:40:42 +0200175func (p *ThriftTestHandler) TestException(ctx context.Context, arg string) (err error) {
Jens Geyer5f9bdff2014-11-18 21:57:03 +0100176 if arg == "Xception" {
177 x := thrifttest.NewXception()
178 x.ErrorCode = 1001
179 x.Message = arg
180 return x
181 } else if arg == "TException" {
182 return thrift.TException(errors.New(arg))
183 } else {
184 return nil
185 }
186}
187
taozlec0d384a2017-07-17 18:40:42 +0200188func (p *ThriftTestHandler) TestMultiException(ctx context.Context, arg0 string, arg1 string) (r *thrifttest.Xtruct, err error) {
Jens Geyer5f9bdff2014-11-18 21:57:03 +0100189 if arg0 == "Xception" {
190 x := thrifttest.NewXception()
191 x.ErrorCode = 1001
192 x.Message = "This is an Xception"
193 return nil, x
194 } else if arg0 == "Xception2" {
195 x2 := thrifttest.NewXception2()
196 x2.ErrorCode = 2002
197 x2.StructThing = thrifttest.NewXtruct()
198 x2.StructThing.StringThing = "This is an Xception2"
199 return nil, x2
200 }
201
202 res := thrifttest.NewXtruct()
203 res.StringThing = arg1
204 return res, nil
205}
206
taozlec0d384a2017-07-17 18:40:42 +0200207func (p *ThriftTestHandler) TestOneway(ctx context.Context, secondsToSleep int32) (err error) {
Jens Geyer5f9bdff2014-11-18 21:57:03 +0100208 time.Sleep(time.Second * time.Duration(secondsToSleep))
209 return nil
210}