]>
git.proxmox.com Git - ceph.git/blob - ceph/src/jaegertracing/thrift/test/py.tornado/test_suite.py
4 # Licensed to the Apache Software Foundation (ASF) under one
5 # or more contributor license agreements. See the NOTICE file
6 # distributed with this work for additional information
7 # regarding copyright ownership. The ASF licenses this file
8 # to you under the Apache License, Version 2.0 (the
9 # "License"); you may not use this file except in compliance
10 # with the License. You may obtain a copy of the License at
12 # http://www.apache.org/licenses/LICENSE-2.0
14 # Unless required by applicable law or agreed to in writing,
15 # software distributed under the License is distributed on an
16 # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 # KIND, either express or implied. See the License for the
18 # specific language governing permissions and limitations
29 basepath
= os
.path
.abspath(os
.path
.dirname(__file__
))
30 sys
.path
.insert(0, basepath
+ '/gen-py.tornado')
31 sys
.path
.insert(0, glob
.glob(os
.path
.join(basepath
, '../../lib/py/build/lib*'))[0])
36 print("module `tornado` not found, skipping test")
39 from tornado
import gen
40 from tornado
.testing
import AsyncTestCase
, get_unused_port
, gen_test
42 from thrift
import TTornado
43 from thrift
.Thrift
import TApplicationException
44 from thrift
.protocol
import TBinaryProtocol
46 from ThriftTest
import ThriftTest
47 from ThriftTest
.ttypes
import Xception
, Xtruct
50 class TestHandler(object):
51 def __init__(self
, test_instance
):
52 self
.test_instance
= test_instance
57 def testString(self
, s
):
58 if s
== 'unexpected_error':
62 def testByte(self
, b
):
65 def testI16(self
, i16
):
68 def testI32(self
, i32
):
71 def testI64(self
, i64
):
74 def testDouble(self
, dub
):
77 def testBinary(self
, thing
):
80 def testStruct(self
, thing
):
83 def testException(self
, s
):
89 elif s
== 'throw_undeclared':
90 raise ValueError('testing undeclared exception')
92 def testOneway(self
, seconds
):
97 self
.test_instance
.stop((start
, end
, seconds
))
99 self
.test_instance
.io_loop
.add_timeout(
100 datetime
.timedelta(seconds
=seconds
),
102 raise Exception('testing exception in oneway method')
104 def testNest(self
, thing
):
108 def testMap(self
, thing
):
110 raise gen
.Return(thing
)
112 def testSet(self
, thing
):
115 def testList(self
, thing
):
118 def testEnum(self
, thing
):
121 def testTypedef(self
, thing
):
125 class ThriftTestCase(AsyncTestCase
):
127 super(ThriftTestCase
, self
).setUp()
129 self
.port
= get_unused_port()
132 self
.handler
= TestHandler(self
)
133 self
.processor
= ThriftTest
.Processor(self
.handler
)
134 self
.pfactory
= TBinaryProtocol
.TBinaryProtocolFactory()
136 self
.server
= TTornado
.TTornadoServer(self
.processor
, self
.pfactory
, io_loop
=self
.io_loop
)
137 self
.server
.bind(self
.port
)
141 transport
= TTornado
.TTornadoStreamTransport('localhost', self
.port
, io_loop
=self
.io_loop
)
142 pfactory
= TBinaryProtocol
.TBinaryProtocolFactory()
143 self
.io_loop
.run_sync(transport
.open)
144 self
.client
= ThriftTest
.Client(transport
, pfactory
)
148 v
= yield self
.client
.testVoid()
149 self
.assertEqual(v
, None)
152 def test_string(self
):
153 v
= yield self
.client
.testString('Python')
154 self
.assertEqual(v
, 'Python')
158 v
= yield self
.client
.testByte(63)
159 self
.assertEqual(v
, 63)
163 v
= yield self
.client
.testI32(-1)
164 self
.assertEqual(v
, -1)
166 v
= yield self
.client
.testI32(0)
167 self
.assertEqual(v
, 0)
171 v
= yield self
.client
.testI64(-34359738368)
172 self
.assertEqual(v
, -34359738368)
175 def test_double(self
):
176 v
= yield self
.client
.testDouble(-5.235098235)
177 self
.assertEqual(v
, -5.235098235)
180 def test_struct(self
):
182 x
.string_thing
= "Zero"
186 y
= yield self
.client
.testStruct(x
)
188 self
.assertEqual(y
.string_thing
, "Zero")
189 self
.assertEqual(y
.byte_thing
, 1)
190 self
.assertEqual(y
.i32_thing
, -3)
191 self
.assertEqual(y
.i64_thing
, -5)
194 def test_oneway(self
):
195 self
.client
.testOneway(1)
196 v
= yield self
.client
.testI32(-1)
197 self
.assertEqual(v
, -1)
202 TestHandler.testMap is a coroutine, this test checks if gen.Return() from a coroutine works.
205 res
= yield self
.client
.testMap(expected
)
206 self
.assertEqual(res
, expected
)
209 def test_exception(self
):
211 yield self
.client
.testException('Xception')
212 except Xception
as ex
:
213 self
.assertEqual(ex
.errorCode
, 1001)
214 self
.assertEqual(ex
.message
, 'Xception')
216 self
.fail("should have gotten exception")
218 yield self
.client
.testException('throw_undeclared')
219 except TApplicationException
:
222 self
.fail("should have gotten exception")
224 yield self
.client
.testException('Safe')
228 suite
= unittest
.TestSuite()
229 loader
= unittest
.TestLoader()
230 suite
.addTest(loader
.loadTestsFromTestCase(ThriftTestCase
))
234 if __name__
== '__main__':
235 unittest
.TestProgram(defaultTest
='suite',
236 testRunner
=unittest
.TextTestRunner(verbosity
=1))