Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 1 | #!/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 | |
| 22 | import datetime |
| 23 | import glob |
Roger Meier | d52edba | 2014-08-07 17:03:47 +0200 | [diff] [blame] | 24 | import os |
Nobuaki Sukegawa | 66c3dbf | 2016-02-10 19:37:26 +0900 | [diff] [blame] | 25 | import sys |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 26 | import time |
| 27 | import unittest |
| 28 | |
Roger Meier | d52edba | 2014-08-07 17:03:47 +0200 | [diff] [blame] | 29 | basepath = os.path.abspath(os.path.dirname(__file__)) |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 30 | sys.path.insert(0, basepath + '/gen-py.tornado') |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 31 | sys.path.insert(0, glob.glob(os.path.join(basepath, '../../lib/py/build/lib*'))[0]) |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 32 | |
| 33 | try: |
| 34 | __import__('tornado') |
| 35 | except ImportError: |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 36 | print("module `tornado` not found, skipping test") |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 37 | sys.exit(0) |
| 38 | |
Roger Meier | d52edba | 2014-08-07 17:03:47 +0200 | [diff] [blame] | 39 | from tornado import gen |
| 40 | from tornado.testing import AsyncTestCase, get_unused_port, gen_test |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 41 | |
| 42 | from thrift import TTornado |
Nobuaki Sukegawa | 66c3dbf | 2016-02-10 19:37:26 +0900 | [diff] [blame] | 43 | from thrift.Thrift import TApplicationException |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 44 | from thrift.protocol import TBinaryProtocol |
| 45 | |
| 46 | from ThriftTest import ThriftTest |
Nobuaki Sukegawa | d479e23 | 2016-02-28 11:28:19 +0900 | [diff] [blame] | 47 | from ThriftTest.ttypes import Xception, Xtruct |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 48 | |
| 49 | |
| 50 | class TestHandler(object): |
| 51 | def __init__(self, test_instance): |
| 52 | self.test_instance = test_instance |
| 53 | |
Roger Meier | d52edba | 2014-08-07 17:03:47 +0200 | [diff] [blame] | 54 | def testVoid(self): |
| 55 | pass |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 56 | |
Roger Meier | d52edba | 2014-08-07 17:03:47 +0200 | [diff] [blame] | 57 | def testString(self, s): |
Nobuaki Sukegawa | 66c3dbf | 2016-02-10 19:37:26 +0900 | [diff] [blame] | 58 | if s == 'unexpected_error': |
| 59 | raise Exception(s) |
Roger Meier | d52edba | 2014-08-07 17:03:47 +0200 | [diff] [blame] | 60 | return s |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 61 | |
Roger Meier | d52edba | 2014-08-07 17:03:47 +0200 | [diff] [blame] | 62 | def testByte(self, b): |
| 63 | return b |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 64 | |
Roger Meier | d52edba | 2014-08-07 17:03:47 +0200 | [diff] [blame] | 65 | def testI16(self, i16): |
| 66 | return i16 |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 67 | |
Roger Meier | d52edba | 2014-08-07 17:03:47 +0200 | [diff] [blame] | 68 | def testI32(self, i32): |
| 69 | return i32 |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 70 | |
Roger Meier | d52edba | 2014-08-07 17:03:47 +0200 | [diff] [blame] | 71 | def testI64(self, i64): |
| 72 | return i64 |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 73 | |
Roger Meier | d52edba | 2014-08-07 17:03:47 +0200 | [diff] [blame] | 74 | def testDouble(self, dub): |
| 75 | return dub |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 76 | |
Jens Geyer | 8bcfdd9 | 2014-12-14 03:14:26 +0100 | [diff] [blame] | 77 | def testBinary(self, thing): |
| 78 | return thing |
| 79 | |
Roger Meier | d52edba | 2014-08-07 17:03:47 +0200 | [diff] [blame] | 80 | def testStruct(self, thing): |
| 81 | return thing |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 82 | |
Roger Meier | d52edba | 2014-08-07 17:03:47 +0200 | [diff] [blame] | 83 | def testException(self, s): |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 84 | if s == 'Xception': |
| 85 | x = Xception() |
| 86 | x.errorCode = 1001 |
| 87 | x.message = s |
| 88 | raise x |
| 89 | elif s == 'throw_undeclared': |
Nobuaki Sukegawa | 66c3dbf | 2016-02-10 19:37:26 +0900 | [diff] [blame] | 90 | raise ValueError('testing undeclared exception') |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 91 | |
Roger Meier | d52edba | 2014-08-07 17:03:47 +0200 | [diff] [blame] | 92 | def testOneway(self, seconds): |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 93 | start = time.time() |
Roger Meier | d52edba | 2014-08-07 17:03:47 +0200 | [diff] [blame] | 94 | |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 95 | def fire_oneway(): |
| 96 | end = time.time() |
| 97 | self.test_instance.stop((start, end, seconds)) |
| 98 | |
Roger Meier | d52edba | 2014-08-07 17:03:47 +0200 | [diff] [blame] | 99 | self.test_instance.io_loop.add_timeout( |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 100 | datetime.timedelta(seconds=seconds), |
| 101 | fire_oneway) |
Nobuaki Sukegawa | 66c3dbf | 2016-02-10 19:37:26 +0900 | [diff] [blame] | 102 | raise Exception('testing exception in oneway method') |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 103 | |
Roger Meier | d52edba | 2014-08-07 17:03:47 +0200 | [diff] [blame] | 104 | def testNest(self, thing): |
| 105 | return thing |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 106 | |
Roger Meier | d52edba | 2014-08-07 17:03:47 +0200 | [diff] [blame] | 107 | @gen.coroutine |
| 108 | def testMap(self, thing): |
| 109 | yield gen.moment |
| 110 | raise gen.Return(thing) |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 111 | |
Roger Meier | d52edba | 2014-08-07 17:03:47 +0200 | [diff] [blame] | 112 | def testSet(self, thing): |
| 113 | return thing |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 114 | |
Roger Meier | d52edba | 2014-08-07 17:03:47 +0200 | [diff] [blame] | 115 | def testList(self, thing): |
| 116 | return thing |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 117 | |
Roger Meier | d52edba | 2014-08-07 17:03:47 +0200 | [diff] [blame] | 118 | def testEnum(self, thing): |
| 119 | return thing |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 120 | |
Roger Meier | d52edba | 2014-08-07 17:03:47 +0200 | [diff] [blame] | 121 | def testTypedef(self, thing): |
| 122 | return thing |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 123 | |
| 124 | |
| 125 | class ThriftTestCase(AsyncTestCase): |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 126 | def setUp(self): |
Roger Meier | d52edba | 2014-08-07 17:03:47 +0200 | [diff] [blame] | 127 | super(ThriftTestCase, self).setUp() |
| 128 | |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 129 | self.port = get_unused_port() |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 130 | |
| 131 | # server |
| 132 | self.handler = TestHandler(self) |
| 133 | self.processor = ThriftTest.Processor(self.handler) |
| 134 | self.pfactory = TBinaryProtocol.TBinaryProtocolFactory() |
| 135 | |
Roger Meier | d52edba | 2014-08-07 17:03:47 +0200 | [diff] [blame] | 136 | self.server = TTornado.TTornadoServer(self.processor, self.pfactory, io_loop=self.io_loop) |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 137 | self.server.bind(self.port) |
| 138 | self.server.start(1) |
| 139 | |
| 140 | # client |
Roger Meier | d52edba | 2014-08-07 17:03:47 +0200 | [diff] [blame] | 141 | transport = TTornado.TTornadoStreamTransport('localhost', self.port, io_loop=self.io_loop) |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 142 | pfactory = TBinaryProtocol.TBinaryProtocolFactory() |
Roger Meier | d52edba | 2014-08-07 17:03:47 +0200 | [diff] [blame] | 143 | self.io_loop.run_sync(transport.open) |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 144 | self.client = ThriftTest.Client(transport, pfactory) |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 145 | |
Roger Meier | d52edba | 2014-08-07 17:03:47 +0200 | [diff] [blame] | 146 | @gen_test |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 147 | def test_void(self): |
Roger Meier | d52edba | 2014-08-07 17:03:47 +0200 | [diff] [blame] | 148 | v = yield self.client.testVoid() |
| 149 | self.assertEqual(v, None) |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 150 | |
Roger Meier | d52edba | 2014-08-07 17:03:47 +0200 | [diff] [blame] | 151 | @gen_test |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 152 | def test_string(self): |
Roger Meier | d52edba | 2014-08-07 17:03:47 +0200 | [diff] [blame] | 153 | v = yield self.client.testString('Python') |
| 154 | self.assertEqual(v, 'Python') |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 155 | |
Roger Meier | d52edba | 2014-08-07 17:03:47 +0200 | [diff] [blame] | 156 | @gen_test |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 157 | def test_byte(self): |
Roger Meier | d52edba | 2014-08-07 17:03:47 +0200 | [diff] [blame] | 158 | v = yield self.client.testByte(63) |
| 159 | self.assertEqual(v, 63) |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 160 | |
Roger Meier | d52edba | 2014-08-07 17:03:47 +0200 | [diff] [blame] | 161 | @gen_test |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 162 | def test_i32(self): |
Roger Meier | d52edba | 2014-08-07 17:03:47 +0200 | [diff] [blame] | 163 | v = yield self.client.testI32(-1) |
| 164 | self.assertEqual(v, -1) |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 165 | |
Roger Meier | d52edba | 2014-08-07 17:03:47 +0200 | [diff] [blame] | 166 | v = yield self.client.testI32(0) |
| 167 | self.assertEqual(v, 0) |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 168 | |
Roger Meier | d52edba | 2014-08-07 17:03:47 +0200 | [diff] [blame] | 169 | @gen_test |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 170 | def test_i64(self): |
Roger Meier | d52edba | 2014-08-07 17:03:47 +0200 | [diff] [blame] | 171 | v = yield self.client.testI64(-34359738368) |
| 172 | self.assertEqual(v, -34359738368) |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 173 | |
Roger Meier | d52edba | 2014-08-07 17:03:47 +0200 | [diff] [blame] | 174 | @gen_test |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 175 | def test_double(self): |
Roger Meier | d52edba | 2014-08-07 17:03:47 +0200 | [diff] [blame] | 176 | v = yield self.client.testDouble(-5.235098235) |
| 177 | self.assertEqual(v, -5.235098235) |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 178 | |
Roger Meier | d52edba | 2014-08-07 17:03:47 +0200 | [diff] [blame] | 179 | @gen_test |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 180 | def test_struct(self): |
| 181 | x = Xtruct() |
| 182 | x.string_thing = "Zero" |
| 183 | x.byte_thing = 1 |
| 184 | x.i32_thing = -3 |
| 185 | x.i64_thing = -5 |
Roger Meier | d52edba | 2014-08-07 17:03:47 +0200 | [diff] [blame] | 186 | y = yield self.client.testStruct(x) |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 187 | |
Roger Meier | d52edba | 2014-08-07 17:03:47 +0200 | [diff] [blame] | 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) |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 192 | |
Nobuaki Sukegawa | 66c3dbf | 2016-02-10 19:37:26 +0900 | [diff] [blame] | 193 | @gen_test |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 194 | def test_oneway(self): |
Nobuaki Sukegawa | 66c3dbf | 2016-02-10 19:37:26 +0900 | [diff] [blame] | 195 | self.client.testOneway(1) |
| 196 | v = yield self.client.testI32(-1) |
| 197 | self.assertEqual(v, -1) |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 198 | |
Roger Meier | d52edba | 2014-08-07 17:03:47 +0200 | [diff] [blame] | 199 | @gen_test |
| 200 | def test_map(self): |
| 201 | """ |
| 202 | TestHandler.testMap is a coroutine, this test checks if gen.Return() from a coroutine works. |
| 203 | """ |
| 204 | expected = {1: 1} |
| 205 | res = yield self.client.testMap(expected) |
| 206 | self.assertEqual(res, expected) |
| 207 | |
| 208 | @gen_test |
| 209 | def test_exception(self): |
Roger Meier | d52edba | 2014-08-07 17:03:47 +0200 | [diff] [blame] | 210 | try: |
| 211 | yield self.client.testException('Xception') |
| 212 | except Xception as ex: |
| 213 | self.assertEqual(ex.errorCode, 1001) |
| 214 | self.assertEqual(ex.message, 'Xception') |
| 215 | else: |
| 216 | self.fail("should have gotten exception") |
| 217 | try: |
| 218 | yield self.client.testException('throw_undeclared') |
Nobuaki Sukegawa | 66c3dbf | 2016-02-10 19:37:26 +0900 | [diff] [blame] | 219 | except TApplicationException: |
Roger Meier | d52edba | 2014-08-07 17:03:47 +0200 | [diff] [blame] | 220 | pass |
| 221 | else: |
| 222 | self.fail("should have gotten exception") |
| 223 | |
Nobuaki Sukegawa | 66c3dbf | 2016-02-10 19:37:26 +0900 | [diff] [blame] | 224 | yield self.client.testException('Safe') |
| 225 | |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 226 | |
| 227 | def suite(): |
| 228 | suite = unittest.TestSuite() |
| 229 | loader = unittest.TestLoader() |
| 230 | suite.addTest(loader.loadTestsFromTestCase(ThriftTestCase)) |
| 231 | return suite |
| 232 | |
| 233 | |
| 234 | if __name__ == '__main__': |
| 235 | unittest.TestProgram(defaultTest='suite', |
| 236 | testRunner=unittest.TextTestRunner(verbosity=1)) |