blob: 1bbd7de4049702b5cfec03095ed6d89c85d72cd2 [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 (
23 "errors"
24 "thrift"
25 "thrifttest"
26 "time"
27)
28
29type SecondServiceHandler struct {
30}
31
32func NewSecondServiceHandler() *SecondServiceHandler {
33 return &SecondServiceHandler{}
34}
35
36func (p *SecondServiceHandler) BlahBlah() (err error) {
37 return nil
38}
39
40func (p *SecondServiceHandler) SecondtestString(thing string) (r string, err error) {
41 return thing, nil
42}
43
44type ThriftTestHandler struct {
45}
46
47func NewThriftTestHandler() *ThriftTestHandler {
48 return &ThriftTestHandler{}
49}
50
51func (p *ThriftTestHandler) TestVoid() (err error) {
52 return nil
53}
54
55func (p *ThriftTestHandler) TestString(thing string) (r string, err error) {
56 return thing, nil
57}
58
59func (p *ThriftTestHandler) TestByte(thing int8) (r int8, err error) {
60 return thing, nil
61}
62
63func (p *ThriftTestHandler) TestI32(thing int32) (r int32, err error) {
64 return thing, nil
65}
66
67func (p *ThriftTestHandler) TestI64(thing int64) (r int64, err error) {
68 return thing, nil
69}
70
71func (p *ThriftTestHandler) TestDouble(thing float64) (r float64, err error) {
72 return thing, nil
73}
74
75func (p *ThriftTestHandler) TestStruct(thing *thrifttest.Xtruct) (r *thrifttest.Xtruct, err error) {
76 return thing, nil
77}
78
79func (p *ThriftTestHandler) TestNest(thing *thrifttest.Xtruct2) (r *thrifttest.Xtruct2, err error) {
80 return thing, nil
81}
82
83func (p *ThriftTestHandler) TestMap(thing map[int32]int32) (r map[int32]int32, err error) {
84 return thing, nil
85}
86
87func (p *ThriftTestHandler) TestStringMap(thing map[string]string) (r map[string]string, err error) {
88 return thing, nil
89}
90
91func (p *ThriftTestHandler) TestSet(thing map[int32]bool) (r map[int32]bool, err error) {
92 return thing, nil
93}
94
95func (p *ThriftTestHandler) TestList(thing []int32) (r []int32, err error) {
96 return thing, nil
97}
98
99func (p *ThriftTestHandler) TestEnum(thing thrifttest.Numberz) (r thrifttest.Numberz, err error) {
100 return thing, nil
101}
102
103func (p *ThriftTestHandler) TestTypedef(thing thrifttest.UserId) (r thrifttest.UserId, err error) {
104 return thing, nil
105}
106
107func (p *ThriftTestHandler) TestMapMap(hello int32) (r map[int32]map[int32]int32, err error) {
108 r = make(map[int32]map[int32]int32)
109 pos := make(map[int32]int32)
110 neg := make(map[int32]int32)
111
112 for i := int32(1); i < 5; i++ {
113 pos[i] = i
114 neg[-i] = -i
115 }
116 r[4] = pos
117 r[-4] = neg
118
119 return r, nil
120}
121
122func (p *ThriftTestHandler) TestInsanity(argument *thrifttest.Insanity) (r map[thrifttest.UserId]map[thrifttest.Numberz]*thrifttest.Insanity, err error) {
123 hello := thrifttest.NewXtruct()
124 hello.StringThing = "Hello2"
125 hello.ByteThing = 2
126 hello.I32Thing = 2
127 hello.I64Thing = 2
128
129 goodbye := thrifttest.NewXtruct()
130 goodbye.StringThing = "Goodbye4"
131 goodbye.ByteThing = 4
132 goodbye.I32Thing = 4
133 goodbye.I64Thing = 4
134
135 crazy := thrifttest.NewInsanity()
136 crazy.UserMap = make(map[thrifttest.Numberz]thrifttest.UserId)
137 crazy.UserMap[thrifttest.Numberz_EIGHT] = 8
138 crazy.UserMap[thrifttest.Numberz_FIVE] = 5
139 crazy.Xtructs = []*thrifttest.Xtruct{goodbye, hello}
140
141 first_map := make(map[thrifttest.Numberz]*thrifttest.Insanity)
142 second_map := make(map[thrifttest.Numberz]*thrifttest.Insanity)
143
144 first_map[thrifttest.Numberz_TWO] = crazy
145 first_map[thrifttest.Numberz_THREE] = crazy
146
147 looney := thrifttest.NewInsanity()
148 second_map[thrifttest.Numberz_SIX] = looney
149
150 var insane = make(map[thrifttest.UserId]map[thrifttest.Numberz]*thrifttest.Insanity)
151 insane[1] = first_map
152 insane[2] = second_map
153
154 return insane, nil
155}
156
157func (p *ThriftTestHandler) TestMulti(arg0 int8, arg1 int32, arg2 int64, arg3 map[int16]string, arg4 thrifttest.Numberz, arg5 thrifttest.UserId) (r *thrifttest.Xtruct, err error) {
158 r = thrifttest.NewXtruct()
159 r.StringThing = "Hello2"
160 r.ByteThing = arg0
161 r.I32Thing = arg1
162 r.I64Thing = arg2
163 return r, nil
164}
165
166func (p *ThriftTestHandler) TestException(arg string) (err error) {
167 if arg == "Xception" {
168 x := thrifttest.NewXception()
169 x.ErrorCode = 1001
170 x.Message = arg
171 return x
172 } else if arg == "TException" {
173 return thrift.TException(errors.New(arg))
174 } else {
175 return nil
176 }
177}
178
179func (p *ThriftTestHandler) TestMultiException(arg0 string, arg1 string) (r *thrifttest.Xtruct, err error) {
180 if arg0 == "Xception" {
181 x := thrifttest.NewXception()
182 x.ErrorCode = 1001
183 x.Message = "This is an Xception"
184 return nil, x
185 } else if arg0 == "Xception2" {
186 x2 := thrifttest.NewXception2()
187 x2.ErrorCode = 2002
188 x2.StructThing = thrifttest.NewXtruct()
189 x2.StructThing.StringThing = "This is an Xception2"
190 return nil, x2
191 }
192
193 res := thrifttest.NewXtruct()
194 res.StringThing = arg1
195 return res, nil
196}
197
198func (p *ThriftTestHandler) TestOneway(secondsToSleep int32) (err error) {
199 time.Sleep(time.Second * time.Duration(secondsToSleep))
200 return nil
201}