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" |
| 25 | "thrift" |
| 26 | "thrifttest" |
| 27 | "time" |
| 28 | ) |
| 29 | |
| 30 | type SecondServiceHandler struct { |
| 31 | } |
| 32 | |
| 33 | func NewSecondServiceHandler() *SecondServiceHandler { |
| 34 | return &SecondServiceHandler{} |
| 35 | } |
| 36 | |
taozle | c0d384a | 2017-07-17 18:40:42 +0200 | [diff] [blame] | 37 | func (p *SecondServiceHandler) BlahBlah(ctx context.Context) (err error) { |
Jens Geyer | 5f9bdff | 2014-11-18 21:57:03 +0100 | [diff] [blame] | 38 | return nil |
| 39 | } |
| 40 | |
taozle | c0d384a | 2017-07-17 18:40:42 +0200 | [diff] [blame] | 41 | 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] | 42 | return thing, nil |
| 43 | } |
| 44 | |
| 45 | type ThriftTestHandler struct { |
| 46 | } |
| 47 | |
| 48 | func NewThriftTestHandler() *ThriftTestHandler { |
| 49 | return &ThriftTestHandler{} |
| 50 | } |
| 51 | |
taozle | c0d384a | 2017-07-17 18:40:42 +0200 | [diff] [blame] | 52 | func (p *ThriftTestHandler) TestVoid(ctx context.Context) (err error) { |
Jens Geyer | 5f9bdff | 2014-11-18 21:57:03 +0100 | [diff] [blame] | 53 | return nil |
| 54 | } |
| 55 | |
taozle | c0d384a | 2017-07-17 18:40:42 +0200 | [diff] [blame] | 56 | 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] | 57 | return thing, nil |
| 58 | } |
| 59 | |
taozle | c0d384a | 2017-07-17 18:40:42 +0200 | [diff] [blame] | 60 | 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] | 61 | return thing, nil |
| 62 | } |
| 63 | |
taozle | c0d384a | 2017-07-17 18:40:42 +0200 | [diff] [blame] | 64 | 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] | 65 | return thing, nil |
| 66 | } |
| 67 | |
taozle | c0d384a | 2017-07-17 18:40:42 +0200 | [diff] [blame] | 68 | 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] | 69 | return thing, nil |
| 70 | } |
| 71 | |
taozle | c0d384a | 2017-07-17 18:40:42 +0200 | [diff] [blame] | 72 | 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] | 73 | return thing, nil |
| 74 | } |
| 75 | |
taozle | c0d384a | 2017-07-17 18:40:42 +0200 | [diff] [blame] | 76 | 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] | 77 | return thing, nil |
| 78 | } |
| 79 | |
taozle | c0d384a | 2017-07-17 18:40:42 +0200 | [diff] [blame] | 80 | 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] | 81 | return thing, nil |
| 82 | } |
| 83 | |
taozle | c0d384a | 2017-07-17 18:40:42 +0200 | [diff] [blame] | 84 | 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] | 85 | return thing, nil |
| 86 | } |
| 87 | |
taozle | c0d384a | 2017-07-17 18:40:42 +0200 | [diff] [blame] | 88 | 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] | 89 | return thing, nil |
| 90 | } |
| 91 | |
taozle | c0d384a | 2017-07-17 18:40:42 +0200 | [diff] [blame] | 92 | 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] | 93 | return thing, nil |
| 94 | } |
| 95 | |
taozle | c0d384a | 2017-07-17 18:40:42 +0200 | [diff] [blame] | 96 | 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] | 97 | return thing, nil |
| 98 | } |
| 99 | |
taozle | c0d384a | 2017-07-17 18:40:42 +0200 | [diff] [blame] | 100 | 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] | 101 | return thing, nil |
| 102 | } |
| 103 | |
taozle | c0d384a | 2017-07-17 18:40:42 +0200 | [diff] [blame] | 104 | 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] | 105 | return thing, nil |
| 106 | } |
| 107 | |
taozle | c0d384a | 2017-07-17 18:40:42 +0200 | [diff] [blame] | 108 | 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] | 109 | return thing, nil |
| 110 | } |
| 111 | |
taozle | c0d384a | 2017-07-17 18:40:42 +0200 | [diff] [blame] | 112 | 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] | 113 | return thing, nil |
| 114 | } |
| 115 | |
taozle | c0d384a | 2017-07-17 18:40:42 +0200 | [diff] [blame] | 116 | 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] | 117 | 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 | |
taozle | c0d384a | 2017-07-17 18:40:42 +0200 | [diff] [blame] | 131 | 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] | 132 | 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 | |
taozle | c0d384a | 2017-07-17 18:40:42 +0200 | [diff] [blame] | 166 | 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] | 167 | 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 | |
taozle | c0d384a | 2017-07-17 18:40:42 +0200 | [diff] [blame] | 175 | func (p *ThriftTestHandler) TestException(ctx context.Context, arg string) (err error) { |
Jens Geyer | 5f9bdff | 2014-11-18 21:57:03 +0100 | [diff] [blame] | 176 | 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 | |
taozle | c0d384a | 2017-07-17 18:40:42 +0200 | [diff] [blame] | 188 | 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] | 189 | 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 | |
taozle | c0d384a | 2017-07-17 18:40:42 +0200 | [diff] [blame] | 207 | func (p *ThriftTestHandler) TestOneway(ctx context.Context, secondsToSleep int32) (err error) { |
Jens Geyer | 5f9bdff | 2014-11-18 21:57:03 +0100 | [diff] [blame] | 208 | time.Sleep(time.Second * time.Duration(secondsToSleep)) |
| 209 | return nil |
| 210 | } |