blob: fef09f0b71ac86400842d01d60a692739e80572e [file] [log] [blame]
Chris Piro20c81ad2013-03-07 11:32:48 -05001#!/usr/bin/env python
2
3#
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
11#
12# http://www.apache.org/licenses/LICENSE-2.0
13#
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
19# under the License.
20#
21
22import datetime
23import glob
Roger Meierd52edba2014-08-07 17:03:47 +020024import os
Nobuaki Sukegawa66c3dbf2016-02-10 19:37:26 +090025import sys
Chris Piro20c81ad2013-03-07 11:32:48 -050026import time
27import unittest
28
Roger Meierd52edba2014-08-07 17:03:47 +020029basepath = os.path.abspath(os.path.dirname(__file__))
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090030sys.path.insert(0, basepath + '/gen-py.tornado')
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +090031sys.path.insert(0, glob.glob(os.path.join(basepath, '../../lib/py/build/lib*'))[0])
Chris Piro20c81ad2013-03-07 11:32:48 -050032
33try:
34 __import__('tornado')
35except ImportError:
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +090036 print("module `tornado` not found, skipping test")
Chris Piro20c81ad2013-03-07 11:32:48 -050037 sys.exit(0)
38
Roger Meierd52edba2014-08-07 17:03:47 +020039from tornado import gen
Gregg Donovan62ec9292026-01-29 16:51:37 -050040from tornado.testing import AsyncTestCase, bind_unused_port, gen_test
Chris Piro20c81ad2013-03-07 11:32:48 -050041
42from thrift import TTornado
Nobuaki Sukegawa66c3dbf2016-02-10 19:37:26 +090043from thrift.Thrift import TApplicationException
Chris Piro20c81ad2013-03-07 11:32:48 -050044from thrift.protocol import TBinaryProtocol
45
46from ThriftTest import ThriftTest
Nobuaki Sukegawad479e232016-02-28 11:28:19 +090047from ThriftTest.ttypes import Xception, Xtruct
Chris Piro20c81ad2013-03-07 11:32:48 -050048
49
50class TestHandler(object):
51 def __init__(self, test_instance):
52 self.test_instance = test_instance
53
Roger Meierd52edba2014-08-07 17:03:47 +020054 def testVoid(self):
55 pass
Chris Piro20c81ad2013-03-07 11:32:48 -050056
Roger Meierd52edba2014-08-07 17:03:47 +020057 def testString(self, s):
Nobuaki Sukegawa66c3dbf2016-02-10 19:37:26 +090058 if s == 'unexpected_error':
59 raise Exception(s)
Roger Meierd52edba2014-08-07 17:03:47 +020060 return s
Chris Piro20c81ad2013-03-07 11:32:48 -050061
Roger Meierd52edba2014-08-07 17:03:47 +020062 def testByte(self, b):
63 return b
Chris Piro20c81ad2013-03-07 11:32:48 -050064
Roger Meierd52edba2014-08-07 17:03:47 +020065 def testI16(self, i16):
66 return i16
Chris Piro20c81ad2013-03-07 11:32:48 -050067
Roger Meierd52edba2014-08-07 17:03:47 +020068 def testI32(self, i32):
69 return i32
Chris Piro20c81ad2013-03-07 11:32:48 -050070
Roger Meierd52edba2014-08-07 17:03:47 +020071 def testI64(self, i64):
72 return i64
Chris Piro20c81ad2013-03-07 11:32:48 -050073
Roger Meierd52edba2014-08-07 17:03:47 +020074 def testDouble(self, dub):
75 return dub
Chris Piro20c81ad2013-03-07 11:32:48 -050076
Jens Geyer8bcfdd92014-12-14 03:14:26 +010077 def testBinary(self, thing):
78 return thing
79
Roger Meierd52edba2014-08-07 17:03:47 +020080 def testStruct(self, thing):
81 return thing
Chris Piro20c81ad2013-03-07 11:32:48 -050082
Roger Meierd52edba2014-08-07 17:03:47 +020083 def testException(self, s):
Chris Piro20c81ad2013-03-07 11:32:48 -050084 if s == 'Xception':
Elvis Pranskevichus9c439622019-12-11 16:47:52 -050085 raise Xception(1001, s)
Chris Piro20c81ad2013-03-07 11:32:48 -050086 elif s == 'throw_undeclared':
Nobuaki Sukegawa66c3dbf2016-02-10 19:37:26 +090087 raise ValueError('testing undeclared exception')
Chris Piro20c81ad2013-03-07 11:32:48 -050088
Roger Meierd52edba2014-08-07 17:03:47 +020089 def testOneway(self, seconds):
Chris Piro20c81ad2013-03-07 11:32:48 -050090 start = time.time()
Roger Meierd52edba2014-08-07 17:03:47 +020091
Chris Piro20c81ad2013-03-07 11:32:48 -050092 def fire_oneway():
93 end = time.time()
94 self.test_instance.stop((start, end, seconds))
95
Roger Meierd52edba2014-08-07 17:03:47 +020096 self.test_instance.io_loop.add_timeout(
Chris Piro20c81ad2013-03-07 11:32:48 -050097 datetime.timedelta(seconds=seconds),
98 fire_oneway)
Nobuaki Sukegawa66c3dbf2016-02-10 19:37:26 +090099 raise Exception('testing exception in oneway method')
Chris Piro20c81ad2013-03-07 11:32:48 -0500100
Roger Meierd52edba2014-08-07 17:03:47 +0200101 def testNest(self, thing):
102 return thing
Chris Piro20c81ad2013-03-07 11:32:48 -0500103
Roger Meierd52edba2014-08-07 17:03:47 +0200104 @gen.coroutine
105 def testMap(self, thing):
106 yield gen.moment
107 raise gen.Return(thing)
Chris Piro20c81ad2013-03-07 11:32:48 -0500108
Roger Meierd52edba2014-08-07 17:03:47 +0200109 def testSet(self, thing):
110 return thing
Chris Piro20c81ad2013-03-07 11:32:48 -0500111
Roger Meierd52edba2014-08-07 17:03:47 +0200112 def testList(self, thing):
113 return thing
Chris Piro20c81ad2013-03-07 11:32:48 -0500114
Roger Meierd52edba2014-08-07 17:03:47 +0200115 def testEnum(self, thing):
116 return thing
Chris Piro20c81ad2013-03-07 11:32:48 -0500117
Roger Meierd52edba2014-08-07 17:03:47 +0200118 def testTypedef(self, thing):
119 return thing
Chris Piro20c81ad2013-03-07 11:32:48 -0500120
121
122class ThriftTestCase(AsyncTestCase):
Chris Piro20c81ad2013-03-07 11:32:48 -0500123 def setUp(self):
Roger Meierd52edba2014-08-07 17:03:47 +0200124 super(ThriftTestCase, self).setUp()
125
Gregg Donovan62ec9292026-01-29 16:51:37 -0500126 sock, self.port = bind_unused_port()
127 sock.close()
Chris Piro20c81ad2013-03-07 11:32:48 -0500128
129 # server
130 self.handler = TestHandler(self)
131 self.processor = ThriftTest.Processor(self.handler)
132 self.pfactory = TBinaryProtocol.TBinaryProtocolFactory()
133
Roger Meierd52edba2014-08-07 17:03:47 +0200134 self.server = TTornado.TTornadoServer(self.processor, self.pfactory, io_loop=self.io_loop)
Chris Piro20c81ad2013-03-07 11:32:48 -0500135 self.server.bind(self.port)
136 self.server.start(1)
137
138 # client
Roger Meierd52edba2014-08-07 17:03:47 +0200139 transport = TTornado.TTornadoStreamTransport('localhost', self.port, io_loop=self.io_loop)
Chris Piro20c81ad2013-03-07 11:32:48 -0500140 pfactory = TBinaryProtocol.TBinaryProtocolFactory()
Roger Meierd52edba2014-08-07 17:03:47 +0200141 self.io_loop.run_sync(transport.open)
Chris Piro20c81ad2013-03-07 11:32:48 -0500142 self.client = ThriftTest.Client(transport, pfactory)
Chris Piro20c81ad2013-03-07 11:32:48 -0500143
Roger Meierd52edba2014-08-07 17:03:47 +0200144 @gen_test
Chris Piro20c81ad2013-03-07 11:32:48 -0500145 def test_void(self):
Roger Meierd52edba2014-08-07 17:03:47 +0200146 v = yield self.client.testVoid()
147 self.assertEqual(v, None)
Chris Piro20c81ad2013-03-07 11:32:48 -0500148
Roger Meierd52edba2014-08-07 17:03:47 +0200149 @gen_test
Chris Piro20c81ad2013-03-07 11:32:48 -0500150 def test_string(self):
Roger Meierd52edba2014-08-07 17:03:47 +0200151 v = yield self.client.testString('Python')
152 self.assertEqual(v, 'Python')
Chris Piro20c81ad2013-03-07 11:32:48 -0500153
Roger Meierd52edba2014-08-07 17:03:47 +0200154 @gen_test
Chris Piro20c81ad2013-03-07 11:32:48 -0500155 def test_byte(self):
Roger Meierd52edba2014-08-07 17:03:47 +0200156 v = yield self.client.testByte(63)
157 self.assertEqual(v, 63)
Chris Piro20c81ad2013-03-07 11:32:48 -0500158
Roger Meierd52edba2014-08-07 17:03:47 +0200159 @gen_test
Chris Piro20c81ad2013-03-07 11:32:48 -0500160 def test_i32(self):
Roger Meierd52edba2014-08-07 17:03:47 +0200161 v = yield self.client.testI32(-1)
162 self.assertEqual(v, -1)
Chris Piro20c81ad2013-03-07 11:32:48 -0500163
Roger Meierd52edba2014-08-07 17:03:47 +0200164 v = yield self.client.testI32(0)
165 self.assertEqual(v, 0)
Chris Piro20c81ad2013-03-07 11:32:48 -0500166
Roger Meierd52edba2014-08-07 17:03:47 +0200167 @gen_test
Chris Piro20c81ad2013-03-07 11:32:48 -0500168 def test_i64(self):
Roger Meierd52edba2014-08-07 17:03:47 +0200169 v = yield self.client.testI64(-34359738368)
170 self.assertEqual(v, -34359738368)
Chris Piro20c81ad2013-03-07 11:32:48 -0500171
Roger Meierd52edba2014-08-07 17:03:47 +0200172 @gen_test
Chris Piro20c81ad2013-03-07 11:32:48 -0500173 def test_double(self):
Roger Meierd52edba2014-08-07 17:03:47 +0200174 v = yield self.client.testDouble(-5.235098235)
175 self.assertEqual(v, -5.235098235)
Chris Piro20c81ad2013-03-07 11:32:48 -0500176
Roger Meierd52edba2014-08-07 17:03:47 +0200177 @gen_test
Chris Piro20c81ad2013-03-07 11:32:48 -0500178 def test_struct(self):
179 x = Xtruct()
180 x.string_thing = "Zero"
181 x.byte_thing = 1
182 x.i32_thing = -3
183 x.i64_thing = -5
Roger Meierd52edba2014-08-07 17:03:47 +0200184 y = yield self.client.testStruct(x)
Chris Piro20c81ad2013-03-07 11:32:48 -0500185
Roger Meierd52edba2014-08-07 17:03:47 +0200186 self.assertEqual(y.string_thing, "Zero")
187 self.assertEqual(y.byte_thing, 1)
188 self.assertEqual(y.i32_thing, -3)
189 self.assertEqual(y.i64_thing, -5)
Chris Piro20c81ad2013-03-07 11:32:48 -0500190
Nobuaki Sukegawa66c3dbf2016-02-10 19:37:26 +0900191 @gen_test
Chris Piro20c81ad2013-03-07 11:32:48 -0500192 def test_oneway(self):
Nobuaki Sukegawa66c3dbf2016-02-10 19:37:26 +0900193 self.client.testOneway(1)
194 v = yield self.client.testI32(-1)
195 self.assertEqual(v, -1)
Chris Piro20c81ad2013-03-07 11:32:48 -0500196
Roger Meierd52edba2014-08-07 17:03:47 +0200197 @gen_test
198 def test_map(self):
199 """
200 TestHandler.testMap is a coroutine, this test checks if gen.Return() from a coroutine works.
201 """
202 expected = {1: 1}
203 res = yield self.client.testMap(expected)
204 self.assertEqual(res, expected)
205
206 @gen_test
207 def test_exception(self):
Roger Meierd52edba2014-08-07 17:03:47 +0200208 try:
209 yield self.client.testException('Xception')
210 except Xception as ex:
211 self.assertEqual(ex.errorCode, 1001)
212 self.assertEqual(ex.message, 'Xception')
213 else:
214 self.fail("should have gotten exception")
215 try:
216 yield self.client.testException('throw_undeclared')
Nobuaki Sukegawa66c3dbf2016-02-10 19:37:26 +0900217 except TApplicationException:
Roger Meierd52edba2014-08-07 17:03:47 +0200218 pass
219 else:
220 self.fail("should have gotten exception")
221
Nobuaki Sukegawa66c3dbf2016-02-10 19:37:26 +0900222 yield self.client.testException('Safe')
223
Chris Piro20c81ad2013-03-07 11:32:48 -0500224
225def suite():
226 suite = unittest.TestSuite()
227 loader = unittest.TestLoader()
228 suite.addTest(loader.loadTestsFromTestCase(ThriftTestCase))
229 return suite
230
231
232if __name__ == '__main__':
233 unittest.TestProgram(defaultTest='suite',
234 testRunner=unittest.TextTestRunner(verbosity=1))