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 |
| 24 | import sys |
Roger Meier | d52edba | 2014-08-07 17:03:47 +0200 | [diff] [blame] | 25 | import os |
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 |
| 43 | from thrift.protocol import TBinaryProtocol |
Roger Meier | d52edba | 2014-08-07 17:03:47 +0200 | [diff] [blame] | 44 | from thrift.transport.TTransport import TTransportException |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 45 | |
| 46 | from ThriftTest import ThriftTest |
| 47 | from ThriftTest.ttypes import * |
| 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): |
| 58 | return s |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 59 | |
Roger Meier | d52edba | 2014-08-07 17:03:47 +0200 | [diff] [blame] | 60 | def testByte(self, b): |
| 61 | return b |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 62 | |
Roger Meier | d52edba | 2014-08-07 17:03:47 +0200 | [diff] [blame] | 63 | def testI16(self, i16): |
| 64 | return i16 |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 65 | |
Roger Meier | d52edba | 2014-08-07 17:03:47 +0200 | [diff] [blame] | 66 | def testI32(self, i32): |
| 67 | return i32 |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 68 | |
Roger Meier | d52edba | 2014-08-07 17:03:47 +0200 | [diff] [blame] | 69 | def testI64(self, i64): |
| 70 | return i64 |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 71 | |
Roger Meier | d52edba | 2014-08-07 17:03:47 +0200 | [diff] [blame] | 72 | def testDouble(self, dub): |
| 73 | return dub |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 74 | |
Jens Geyer | 8bcfdd9 | 2014-12-14 03:14:26 +0100 | [diff] [blame] | 75 | def testBinary(self, thing): |
| 76 | return thing |
| 77 | |
Roger Meier | d52edba | 2014-08-07 17:03:47 +0200 | [diff] [blame] | 78 | def testStruct(self, thing): |
| 79 | return thing |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 80 | |
Roger Meier | d52edba | 2014-08-07 17:03:47 +0200 | [diff] [blame] | 81 | def testException(self, s): |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 82 | if s == 'Xception': |
| 83 | x = Xception() |
| 84 | x.errorCode = 1001 |
| 85 | x.message = s |
| 86 | raise x |
| 87 | elif s == 'throw_undeclared': |
| 88 | raise ValueError("foo") |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 89 | |
Roger Meier | d52edba | 2014-08-07 17:03:47 +0200 | [diff] [blame] | 90 | def testOneway(self, seconds): |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 91 | start = time.time() |
Roger Meier | d52edba | 2014-08-07 17:03:47 +0200 | [diff] [blame] | 92 | |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 93 | def fire_oneway(): |
| 94 | end = time.time() |
| 95 | self.test_instance.stop((start, end, seconds)) |
| 96 | |
Roger Meier | d52edba | 2014-08-07 17:03:47 +0200 | [diff] [blame] | 97 | self.test_instance.io_loop.add_timeout( |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 98 | datetime.timedelta(seconds=seconds), |
| 99 | fire_oneway) |
| 100 | |
Roger Meier | d52edba | 2014-08-07 17:03:47 +0200 | [diff] [blame] | 101 | def testNest(self, thing): |
| 102 | return thing |
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 | @gen.coroutine |
| 105 | def testMap(self, thing): |
| 106 | yield gen.moment |
| 107 | raise gen.Return(thing) |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 108 | |
Roger Meier | d52edba | 2014-08-07 17:03:47 +0200 | [diff] [blame] | 109 | def testSet(self, thing): |
| 110 | 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 testList(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 testEnum(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 testTypedef(self, thing): |
| 119 | return thing |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 120 | |
| 121 | |
| 122 | class ThriftTestCase(AsyncTestCase): |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 123 | def setUp(self): |
Roger Meier | d52edba | 2014-08-07 17:03:47 +0200 | [diff] [blame] | 124 | super(ThriftTestCase, self).setUp() |
| 125 | |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 126 | self.port = get_unused_port() |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 127 | |
| 128 | # server |
| 129 | self.handler = TestHandler(self) |
| 130 | self.processor = ThriftTest.Processor(self.handler) |
| 131 | self.pfactory = TBinaryProtocol.TBinaryProtocolFactory() |
| 132 | |
Roger Meier | d52edba | 2014-08-07 17:03:47 +0200 | [diff] [blame] | 133 | 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] | 134 | self.server.bind(self.port) |
| 135 | self.server.start(1) |
| 136 | |
| 137 | # client |
Roger Meier | d52edba | 2014-08-07 17:03:47 +0200 | [diff] [blame] | 138 | transport = TTornado.TTornadoStreamTransport('localhost', self.port, io_loop=self.io_loop) |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 139 | pfactory = TBinaryProtocol.TBinaryProtocolFactory() |
Roger Meier | d52edba | 2014-08-07 17:03:47 +0200 | [diff] [blame] | 140 | self.io_loop.run_sync(transport.open) |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 141 | self.client = ThriftTest.Client(transport, pfactory) |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 142 | |
Roger Meier | d52edba | 2014-08-07 17:03:47 +0200 | [diff] [blame] | 143 | @gen_test |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 144 | def test_void(self): |
Roger Meier | d52edba | 2014-08-07 17:03:47 +0200 | [diff] [blame] | 145 | v = yield self.client.testVoid() |
| 146 | self.assertEqual(v, None) |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 147 | |
Roger Meier | d52edba | 2014-08-07 17:03:47 +0200 | [diff] [blame] | 148 | @gen_test |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 149 | def test_string(self): |
Roger Meier | d52edba | 2014-08-07 17:03:47 +0200 | [diff] [blame] | 150 | v = yield self.client.testString('Python') |
| 151 | self.assertEqual(v, 'Python') |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 152 | |
Roger Meier | d52edba | 2014-08-07 17:03:47 +0200 | [diff] [blame] | 153 | @gen_test |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 154 | def test_byte(self): |
Roger Meier | d52edba | 2014-08-07 17:03:47 +0200 | [diff] [blame] | 155 | v = yield self.client.testByte(63) |
| 156 | self.assertEqual(v, 63) |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 157 | |
Roger Meier | d52edba | 2014-08-07 17:03:47 +0200 | [diff] [blame] | 158 | @gen_test |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 159 | def test_i32(self): |
Roger Meier | d52edba | 2014-08-07 17:03:47 +0200 | [diff] [blame] | 160 | v = yield self.client.testI32(-1) |
| 161 | self.assertEqual(v, -1) |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 162 | |
Roger Meier | d52edba | 2014-08-07 17:03:47 +0200 | [diff] [blame] | 163 | v = yield self.client.testI32(0) |
| 164 | self.assertEqual(v, 0) |
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 | @gen_test |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 167 | def test_i64(self): |
Roger Meier | d52edba | 2014-08-07 17:03:47 +0200 | [diff] [blame] | 168 | v = yield self.client.testI64(-34359738368) |
| 169 | self.assertEqual(v, -34359738368) |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 170 | |
Roger Meier | d52edba | 2014-08-07 17:03:47 +0200 | [diff] [blame] | 171 | @gen_test |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 172 | def test_double(self): |
Roger Meier | d52edba | 2014-08-07 17:03:47 +0200 | [diff] [blame] | 173 | v = yield self.client.testDouble(-5.235098235) |
| 174 | self.assertEqual(v, -5.235098235) |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 175 | |
Roger Meier | d52edba | 2014-08-07 17:03:47 +0200 | [diff] [blame] | 176 | @gen_test |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 177 | def test_struct(self): |
| 178 | x = Xtruct() |
| 179 | x.string_thing = "Zero" |
| 180 | x.byte_thing = 1 |
| 181 | x.i32_thing = -3 |
| 182 | x.i64_thing = -5 |
Roger Meier | d52edba | 2014-08-07 17:03:47 +0200 | [diff] [blame] | 183 | y = yield self.client.testStruct(x) |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 184 | |
Roger Meier | d52edba | 2014-08-07 17:03:47 +0200 | [diff] [blame] | 185 | self.assertEqual(y.string_thing, "Zero") |
| 186 | self.assertEqual(y.byte_thing, 1) |
| 187 | self.assertEqual(y.i32_thing, -3) |
| 188 | self.assertEqual(y.i64_thing, -5) |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 189 | |
| 190 | def test_oneway(self): |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 191 | self.client.testOneway(0) |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 192 | start, end, seconds = self.wait(timeout=1) |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 193 | self.assertAlmostEqual(seconds, (end - start), places=3) |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 194 | |
Roger Meier | d52edba | 2014-08-07 17:03:47 +0200 | [diff] [blame] | 195 | @gen_test |
| 196 | def test_map(self): |
| 197 | """ |
| 198 | TestHandler.testMap is a coroutine, this test checks if gen.Return() from a coroutine works. |
| 199 | """ |
| 200 | expected = {1: 1} |
| 201 | res = yield self.client.testMap(expected) |
| 202 | self.assertEqual(res, expected) |
| 203 | |
| 204 | @gen_test |
| 205 | def test_exception(self): |
| 206 | yield self.client.testException('Safe') |
| 207 | |
| 208 | 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') |
| 217 | except TTransportException as ex: |
| 218 | pass |
| 219 | else: |
| 220 | self.fail("should have gotten exception") |
| 221 | |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 222 | |
| 223 | def suite(): |
| 224 | suite = unittest.TestSuite() |
| 225 | loader = unittest.TestLoader() |
| 226 | suite.addTest(loader.loadTestsFromTestCase(ThriftTestCase)) |
| 227 | return suite |
| 228 | |
| 229 | |
| 230 | if __name__ == '__main__': |
| 231 | unittest.TestProgram(defaultTest='suite', |
| 232 | testRunner=unittest.TextTestRunner(verbosity=1)) |