Jens Geyer | 5f9bdff | 2014-11-18 21:57:03 +0100 | [diff] [blame] | 1 | /* |
| 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 | |
| 20 | package tests |
| 21 | |
| 22 | import ( |
John Boiles | 5785279 | 2018-01-05 14:37:05 -0800 | [diff] [blame] | 23 | "context" |
Jens Geyer | 5f9bdff | 2014-11-18 21:57:03 +0100 | [diff] [blame] | 24 | "errors" |
Jens Geyer | 5f9bdff | 2014-11-18 21:57:03 +0100 | [diff] [blame] | 25 | "time" |
Yuxuan 'fishy' Wang | b71f11e | 2021-03-22 15:01:00 -0700 | [diff] [blame] | 26 | |
| 27 | "github.com/apache/thrift/lib/go/test/gopath/src/thrifttest" |
| 28 | "github.com/apache/thrift/lib/go/thrift" |
Jens Geyer | 5f9bdff | 2014-11-18 21:57:03 +0100 | [diff] [blame] | 29 | ) |
| 30 | |
| 31 | type SecondServiceHandler struct { |
| 32 | } |
| 33 | |
| 34 | func NewSecondServiceHandler() *SecondServiceHandler { |
| 35 | return &SecondServiceHandler{} |
| 36 | } |
| 37 | |
taozle | c0d384a | 2017-07-17 18:40:42 +0200 | [diff] [blame] | 38 | func (p *SecondServiceHandler) BlahBlah(ctx context.Context) (err error) { |
Jens Geyer | 5f9bdff | 2014-11-18 21:57:03 +0100 | [diff] [blame] | 39 | return nil |
| 40 | } |
| 41 | |
taozle | c0d384a | 2017-07-17 18:40:42 +0200 | [diff] [blame] | 42 | func (p *SecondServiceHandler) SecondtestString(ctx context.Context, thing string) (r string, err error) { |
Jens Geyer | 5f9bdff | 2014-11-18 21:57:03 +0100 | [diff] [blame] | 43 | return thing, nil |
| 44 | } |
| 45 | |
| 46 | type ThriftTestHandler struct { |
| 47 | } |
| 48 | |
| 49 | func NewThriftTestHandler() *ThriftTestHandler { |
| 50 | return &ThriftTestHandler{} |
| 51 | } |
| 52 | |
taozle | c0d384a | 2017-07-17 18:40:42 +0200 | [diff] [blame] | 53 | func (p *ThriftTestHandler) TestVoid(ctx context.Context) (err error) { |
Jens Geyer | 5f9bdff | 2014-11-18 21:57:03 +0100 | [diff] [blame] | 54 | return nil |
| 55 | } |
| 56 | |
taozle | c0d384a | 2017-07-17 18:40:42 +0200 | [diff] [blame] | 57 | func (p *ThriftTestHandler) TestString(ctx context.Context, thing string) (r string, err error) { |
Jens Geyer | 5f9bdff | 2014-11-18 21:57:03 +0100 | [diff] [blame] | 58 | return thing, nil |
| 59 | } |
| 60 | |
taozle | c0d384a | 2017-07-17 18:40:42 +0200 | [diff] [blame] | 61 | func (p *ThriftTestHandler) TestBool(ctx context.Context, thing bool) (r bool, err error) { |
Nobuaki Sukegawa | a649e74 | 2015-09-21 13:53:25 +0900 | [diff] [blame] | 62 | return thing, nil |
| 63 | } |
| 64 | |
taozle | c0d384a | 2017-07-17 18:40:42 +0200 | [diff] [blame] | 65 | func (p *ThriftTestHandler) TestByte(ctx context.Context, thing int8) (r int8, err error) { |
Jens Geyer | 5f9bdff | 2014-11-18 21:57:03 +0100 | [diff] [blame] | 66 | return thing, nil |
| 67 | } |
| 68 | |
taozle | c0d384a | 2017-07-17 18:40:42 +0200 | [diff] [blame] | 69 | func (p *ThriftTestHandler) TestI32(ctx context.Context, thing int32) (r int32, err error) { |
Jens Geyer | 5f9bdff | 2014-11-18 21:57:03 +0100 | [diff] [blame] | 70 | return thing, nil |
| 71 | } |
| 72 | |
taozle | c0d384a | 2017-07-17 18:40:42 +0200 | [diff] [blame] | 73 | func (p *ThriftTestHandler) TestI64(ctx context.Context, thing int64) (r int64, err error) { |
Jens Geyer | 5f9bdff | 2014-11-18 21:57:03 +0100 | [diff] [blame] | 74 | return thing, nil |
| 75 | } |
| 76 | |
taozle | c0d384a | 2017-07-17 18:40:42 +0200 | [diff] [blame] | 77 | func (p *ThriftTestHandler) TestDouble(ctx context.Context, thing float64) (r float64, err error) { |
Jens Geyer | 5f9bdff | 2014-11-18 21:57:03 +0100 | [diff] [blame] | 78 | return thing, nil |
| 79 | } |
| 80 | |
taozle | c0d384a | 2017-07-17 18:40:42 +0200 | [diff] [blame] | 81 | func (p *ThriftTestHandler) TestBinary(ctx context.Context, thing []byte) (r []byte, err error) { |
Jens Geyer | 8bcfdd9 | 2014-12-14 03:14:26 +0100 | [diff] [blame] | 82 | return thing, nil |
| 83 | } |
| 84 | |
taozle | c0d384a | 2017-07-17 18:40:42 +0200 | [diff] [blame] | 85 | func (p *ThriftTestHandler) TestStruct(ctx context.Context, thing *thrifttest.Xtruct) (r *thrifttest.Xtruct, err error) { |
Jens Geyer | 5f9bdff | 2014-11-18 21:57:03 +0100 | [diff] [blame] | 86 | return thing, nil |
| 87 | } |
| 88 | |
taozle | c0d384a | 2017-07-17 18:40:42 +0200 | [diff] [blame] | 89 | func (p *ThriftTestHandler) TestNest(ctx context.Context, thing *thrifttest.Xtruct2) (r *thrifttest.Xtruct2, err error) { |
Jens Geyer | 5f9bdff | 2014-11-18 21:57:03 +0100 | [diff] [blame] | 90 | return thing, nil |
| 91 | } |
| 92 | |
taozle | c0d384a | 2017-07-17 18:40:42 +0200 | [diff] [blame] | 93 | func (p *ThriftTestHandler) TestMap(ctx context.Context, thing map[int32]int32) (r map[int32]int32, err error) { |
Jens Geyer | 5f9bdff | 2014-11-18 21:57:03 +0100 | [diff] [blame] | 94 | return thing, nil |
| 95 | } |
| 96 | |
taozle | c0d384a | 2017-07-17 18:40:42 +0200 | [diff] [blame] | 97 | func (p *ThriftTestHandler) TestStringMap(ctx context.Context, thing map[string]string) (r map[string]string, err error) { |
Jens Geyer | 5f9bdff | 2014-11-18 21:57:03 +0100 | [diff] [blame] | 98 | return thing, nil |
| 99 | } |
| 100 | |
taozle | c0d384a | 2017-07-17 18:40:42 +0200 | [diff] [blame] | 101 | func (p *ThriftTestHandler) TestSet(ctx context.Context, thing []int32) (r []int32, err error) { |
Jens Geyer | 5f9bdff | 2014-11-18 21:57:03 +0100 | [diff] [blame] | 102 | return thing, nil |
| 103 | } |
| 104 | |
taozle | c0d384a | 2017-07-17 18:40:42 +0200 | [diff] [blame] | 105 | func (p *ThriftTestHandler) TestList(ctx context.Context, thing []int32) (r []int32, err error) { |
Jens Geyer | 5f9bdff | 2014-11-18 21:57:03 +0100 | [diff] [blame] | 106 | return thing, nil |
| 107 | } |
| 108 | |
taozle | c0d384a | 2017-07-17 18:40:42 +0200 | [diff] [blame] | 109 | func (p *ThriftTestHandler) TestEnum(ctx context.Context, thing thrifttest.Numberz) (r thrifttest.Numberz, err error) { |
Jens Geyer | 5f9bdff | 2014-11-18 21:57:03 +0100 | [diff] [blame] | 110 | return thing, nil |
| 111 | } |
| 112 | |
taozle | c0d384a | 2017-07-17 18:40:42 +0200 | [diff] [blame] | 113 | func (p *ThriftTestHandler) TestTypedef(ctx context.Context, thing thrifttest.UserId) (r thrifttest.UserId, err error) { |
Jens Geyer | 5f9bdff | 2014-11-18 21:57:03 +0100 | [diff] [blame] | 114 | return thing, nil |
| 115 | } |
| 116 | |
taozle | c0d384a | 2017-07-17 18:40:42 +0200 | [diff] [blame] | 117 | func (p *ThriftTestHandler) TestMapMap(ctx context.Context, hello int32) (r map[int32]map[int32]int32, err error) { |
Jens Geyer | 5f9bdff | 2014-11-18 21:57:03 +0100 | [diff] [blame] | 118 | 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 | |
taozle | c0d384a | 2017-07-17 18:40:42 +0200 | [diff] [blame] | 132 | func (p *ThriftTestHandler) TestInsanity(ctx context.Context, argument *thrifttest.Insanity) (r map[thrifttest.UserId]map[thrifttest.Numberz]*thrifttest.Insanity, err error) { |
Jens Geyer | 5f9bdff | 2014-11-18 21:57:03 +0100 | [diff] [blame] | 133 | 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 | |
taozle | c0d384a | 2017-07-17 18:40:42 +0200 | [diff] [blame] | 167 | func (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 Geyer | 5f9bdff | 2014-11-18 21:57:03 +0100 | [diff] [blame] | 168 | 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 | |
taozle | c0d384a | 2017-07-17 18:40:42 +0200 | [diff] [blame] | 176 | func (p *ThriftTestHandler) TestException(ctx context.Context, arg string) (err error) { |
Jens Geyer | 5f9bdff | 2014-11-18 21:57:03 +0100 | [diff] [blame] | 177 | 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' Wang | d831230 | 2020-12-22 09:53:58 -0800 | [diff] [blame] | 183 | return thrift.WrapTException(errors.New(arg)) |
Jens Geyer | 5f9bdff | 2014-11-18 21:57:03 +0100 | [diff] [blame] | 184 | } else { |
| 185 | return nil |
| 186 | } |
| 187 | } |
| 188 | |
taozle | c0d384a | 2017-07-17 18:40:42 +0200 | [diff] [blame] | 189 | func (p *ThriftTestHandler) TestMultiException(ctx context.Context, arg0 string, arg1 string) (r *thrifttest.Xtruct, err error) { |
Jens Geyer | 5f9bdff | 2014-11-18 21:57:03 +0100 | [diff] [blame] | 190 | 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 | |
taozle | c0d384a | 2017-07-17 18:40:42 +0200 | [diff] [blame] | 208 | func (p *ThriftTestHandler) TestOneway(ctx context.Context, secondsToSleep int32) (err error) { |
Jens Geyer | 5f9bdff | 2014-11-18 21:57:03 +0100 | [diff] [blame] | 209 | time.Sleep(time.Second * time.Duration(secondsToSleep)) |
| 210 | return nil |
| 211 | } |