blob: 419a18b443fbc20448e0704cb912b297dd93c2e3 [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"
Jens Geyer5f9bdff2014-11-18 21:57:03 +010025 "time"
Yuxuan 'fishy' Wangb71f11e2021-03-22 15:01:00 -070026
27 "github.com/apache/thrift/lib/go/test/gopath/src/thrifttest"
28 "github.com/apache/thrift/lib/go/thrift"
Jens Geyer5f9bdff2014-11-18 21:57:03 +010029)
30
31type SecondServiceHandler struct {
32}
33
34func NewSecondServiceHandler() *SecondServiceHandler {
35 return &SecondServiceHandler{}
36}
37
taozlec0d384a2017-07-17 18:40:42 +020038func (p *SecondServiceHandler) BlahBlah(ctx context.Context) (err error) {
Jens Geyer5f9bdff2014-11-18 21:57:03 +010039 return nil
40}
41
taozlec0d384a2017-07-17 18:40:42 +020042func (p *SecondServiceHandler) SecondtestString(ctx context.Context, thing string) (r string, err error) {
Jens Geyer5f9bdff2014-11-18 21:57:03 +010043 return thing, nil
44}
45
46type ThriftTestHandler struct {
47}
48
49func NewThriftTestHandler() *ThriftTestHandler {
50 return &ThriftTestHandler{}
51}
52
taozlec0d384a2017-07-17 18:40:42 +020053func (p *ThriftTestHandler) TestVoid(ctx context.Context) (err error) {
Jens Geyer5f9bdff2014-11-18 21:57:03 +010054 return nil
55}
56
taozlec0d384a2017-07-17 18:40:42 +020057func (p *ThriftTestHandler) TestString(ctx context.Context, thing string) (r string, err error) {
Jens Geyer5f9bdff2014-11-18 21:57:03 +010058 return thing, nil
59}
60
taozlec0d384a2017-07-17 18:40:42 +020061func (p *ThriftTestHandler) TestBool(ctx context.Context, thing bool) (r bool, err error) {
Nobuaki Sukegawaa649e742015-09-21 13:53:25 +090062 return thing, nil
63}
64
taozlec0d384a2017-07-17 18:40:42 +020065func (p *ThriftTestHandler) TestByte(ctx context.Context, thing int8) (r int8, err error) {
Jens Geyer5f9bdff2014-11-18 21:57:03 +010066 return thing, nil
67}
68
taozlec0d384a2017-07-17 18:40:42 +020069func (p *ThriftTestHandler) TestI32(ctx context.Context, thing int32) (r int32, err error) {
Jens Geyer5f9bdff2014-11-18 21:57:03 +010070 return thing, nil
71}
72
taozlec0d384a2017-07-17 18:40:42 +020073func (p *ThriftTestHandler) TestI64(ctx context.Context, thing int64) (r int64, err error) {
Jens Geyer5f9bdff2014-11-18 21:57:03 +010074 return thing, nil
75}
76
taozlec0d384a2017-07-17 18:40:42 +020077func (p *ThriftTestHandler) TestDouble(ctx context.Context, thing float64) (r float64, err error) {
Jens Geyer5f9bdff2014-11-18 21:57:03 +010078 return thing, nil
79}
80
taozlec0d384a2017-07-17 18:40:42 +020081func (p *ThriftTestHandler) TestBinary(ctx context.Context, thing []byte) (r []byte, err error) {
Jens Geyer8bcfdd92014-12-14 03:14:26 +010082 return thing, nil
83}
84
taozlec0d384a2017-07-17 18:40:42 +020085func (p *ThriftTestHandler) TestStruct(ctx context.Context, thing *thrifttest.Xtruct) (r *thrifttest.Xtruct, err error) {
Jens Geyer5f9bdff2014-11-18 21:57:03 +010086 return thing, nil
87}
88
taozlec0d384a2017-07-17 18:40:42 +020089func (p *ThriftTestHandler) TestNest(ctx context.Context, thing *thrifttest.Xtruct2) (r *thrifttest.Xtruct2, err error) {
Jens Geyer5f9bdff2014-11-18 21:57:03 +010090 return thing, nil
91}
92
taozlec0d384a2017-07-17 18:40:42 +020093func (p *ThriftTestHandler) TestMap(ctx context.Context, thing map[int32]int32) (r map[int32]int32, err error) {
Jens Geyer5f9bdff2014-11-18 21:57:03 +010094 return thing, nil
95}
96
taozlec0d384a2017-07-17 18:40:42 +020097func (p *ThriftTestHandler) TestStringMap(ctx context.Context, thing map[string]string) (r map[string]string, err error) {
Jens Geyer5f9bdff2014-11-18 21:57:03 +010098 return thing, nil
99}
100
taozlec0d384a2017-07-17 18:40:42 +0200101func (p *ThriftTestHandler) TestSet(ctx context.Context, thing []int32) (r []int32, err error) {
Jens Geyer5f9bdff2014-11-18 21:57:03 +0100102 return thing, nil
103}
104
taozlec0d384a2017-07-17 18:40:42 +0200105func (p *ThriftTestHandler) TestList(ctx context.Context, thing []int32) (r []int32, err error) {
Jens Geyer5f9bdff2014-11-18 21:57:03 +0100106 return thing, nil
107}
108
taozlec0d384a2017-07-17 18:40:42 +0200109func (p *ThriftTestHandler) TestEnum(ctx context.Context, thing thrifttest.Numberz) (r thrifttest.Numberz, err error) {
Jens Geyer5f9bdff2014-11-18 21:57:03 +0100110 return thing, nil
111}
112
taozlec0d384a2017-07-17 18:40:42 +0200113func (p *ThriftTestHandler) TestTypedef(ctx context.Context, thing thrifttest.UserId) (r thrifttest.UserId, err error) {
Jens Geyer5f9bdff2014-11-18 21:57:03 +0100114 return thing, nil
115}
116
taozlec0d384a2017-07-17 18:40:42 +0200117func (p *ThriftTestHandler) TestMapMap(ctx context.Context, hello int32) (r map[int32]map[int32]int32, err error) {
Jens Geyer5f9bdff2014-11-18 21:57:03 +0100118 r = make(map[int32]map[int32]int32)
119 pos := make(map[int32]int32)
120 neg := make(map[int32]int32)
121
122 for i := int32(1); i < 5; i++ {
123 pos[i] = i
124 neg[-i] = -i
125 }
126 r[4] = pos
127 r[-4] = neg
128
129 return r, nil
130}
131
taozlec0d384a2017-07-17 18:40:42 +0200132func (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 +0100133 hello := thrifttest.NewXtruct()
134 hello.StringThing = "Hello2"
135 hello.ByteThing = 2
136 hello.I32Thing = 2
137 hello.I64Thing = 2
138
139 goodbye := thrifttest.NewXtruct()
140 goodbye.StringThing = "Goodbye4"
141 goodbye.ByteThing = 4
142 goodbye.I32Thing = 4
143 goodbye.I64Thing = 4
144
145 crazy := thrifttest.NewInsanity()
146 crazy.UserMap = make(map[thrifttest.Numberz]thrifttest.UserId)
147 crazy.UserMap[thrifttest.Numberz_EIGHT] = 8
148 crazy.UserMap[thrifttest.Numberz_FIVE] = 5
149 crazy.Xtructs = []*thrifttest.Xtruct{goodbye, hello}
150
151 first_map := make(map[thrifttest.Numberz]*thrifttest.Insanity)
152 second_map := make(map[thrifttest.Numberz]*thrifttest.Insanity)
153
154 first_map[thrifttest.Numberz_TWO] = crazy
155 first_map[thrifttest.Numberz_THREE] = crazy
156
157 looney := thrifttest.NewInsanity()
158 second_map[thrifttest.Numberz_SIX] = looney
159
160 var insane = make(map[thrifttest.UserId]map[thrifttest.Numberz]*thrifttest.Insanity)
161 insane[1] = first_map
162 insane[2] = second_map
163
164 return insane, nil
165}
166
taozlec0d384a2017-07-17 18:40:42 +0200167func (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 +0100168 r = thrifttest.NewXtruct()
169 r.StringThing = "Hello2"
170 r.ByteThing = arg0
171 r.I32Thing = arg1
172 r.I64Thing = arg2
173 return r, nil
174}
175
taozlec0d384a2017-07-17 18:40:42 +0200176func (p *ThriftTestHandler) TestException(ctx context.Context, arg string) (err error) {
Jens Geyer5f9bdff2014-11-18 21:57:03 +0100177 if arg == "Xception" {
178 x := thrifttest.NewXception()
179 x.ErrorCode = 1001
180 x.Message = arg
181 return x
182 } else if arg == "TException" {
Yuxuan 'fishy' Wangd8312302020-12-22 09:53:58 -0800183 return thrift.WrapTException(errors.New(arg))
Jens Geyer5f9bdff2014-11-18 21:57:03 +0100184 } else {
185 return nil
186 }
187}
188
taozlec0d384a2017-07-17 18:40:42 +0200189func (p *ThriftTestHandler) TestMultiException(ctx context.Context, arg0 string, arg1 string) (r *thrifttest.Xtruct, err error) {
Jens Geyer5f9bdff2014-11-18 21:57:03 +0100190 if arg0 == "Xception" {
191 x := thrifttest.NewXception()
192 x.ErrorCode = 1001
193 x.Message = "This is an Xception"
194 return nil, x
195 } else if arg0 == "Xception2" {
196 x2 := thrifttest.NewXception2()
197 x2.ErrorCode = 2002
198 x2.StructThing = thrifttest.NewXtruct()
199 x2.StructThing.StringThing = "This is an Xception2"
200 return nil, x2
201 }
202
203 res := thrifttest.NewXtruct()
204 res.StringThing = arg1
205 return res, nil
206}
207
taozlec0d384a2017-07-17 18:40:42 +0200208func (p *ThriftTestHandler) TestOneway(ctx context.Context, secondsToSleep int32) (err error) {
Jens Geyer5f9bdff2014-11-18 21:57:03 +0100209 time.Sleep(time.Second * time.Duration(secondsToSleep))
210 return nil
211}