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__)) |
| 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]) |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 32 | |
| 33 | try: |
| 34 | __import__('tornado') |
| 35 | except ImportError: |
| 36 | print "module `tornado` not found, skipping test" |
| 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 | |
Roger Meier | d52edba | 2014-08-07 17:03:47 +0200 | [diff] [blame] | 75 | def testStruct(self, thing): |
| 76 | return thing |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 77 | |
Roger Meier | d52edba | 2014-08-07 17:03:47 +0200 | [diff] [blame] | 78 | def testException(self, s): |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 79 | if s == 'Xception': |
| 80 | x = Xception() |
| 81 | x.errorCode = 1001 |
| 82 | x.message = s |
| 83 | raise x |
| 84 | elif s == 'throw_undeclared': |
| 85 | raise ValueError("foo") |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 86 | |
Roger Meier | d52edba | 2014-08-07 17:03:47 +0200 | [diff] [blame] | 87 | def testOneway(self, seconds): |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 88 | start = time.time() |
Roger Meier | d52edba | 2014-08-07 17:03:47 +0200 | [diff] [blame] | 89 | |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 90 | def fire_oneway(): |
| 91 | end = time.time() |
| 92 | self.test_instance.stop((start, end, seconds)) |
| 93 | |
Roger Meier | d52edba | 2014-08-07 17:03:47 +0200 | [diff] [blame] | 94 | self.test_instance.io_loop.add_timeout( |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 95 | datetime.timedelta(seconds=seconds), |
| 96 | fire_oneway) |
| 97 | |
Roger Meier | d52edba | 2014-08-07 17:03:47 +0200 | [diff] [blame] | 98 | def testNest(self, thing): |
| 99 | return thing |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 100 | |
Roger Meier | d52edba | 2014-08-07 17:03:47 +0200 | [diff] [blame] | 101 | @gen.coroutine |
| 102 | def testMap(self, thing): |
| 103 | yield gen.moment |
| 104 | raise gen.Return(thing) |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 105 | |
Roger Meier | d52edba | 2014-08-07 17:03:47 +0200 | [diff] [blame] | 106 | def testSet(self, thing): |
| 107 | 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 testList(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 testEnum(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 testTypedef(self, thing): |
| 116 | return thing |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 117 | |
| 118 | |
| 119 | class ThriftTestCase(AsyncTestCase): |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 120 | def setUp(self): |
Roger Meier | d52edba | 2014-08-07 17:03:47 +0200 | [diff] [blame] | 121 | super(ThriftTestCase, self).setUp() |
| 122 | |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 123 | self.port = get_unused_port() |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 124 | |
| 125 | # server |
| 126 | self.handler = TestHandler(self) |
| 127 | self.processor = ThriftTest.Processor(self.handler) |
| 128 | self.pfactory = TBinaryProtocol.TBinaryProtocolFactory() |
| 129 | |
Roger Meier | d52edba | 2014-08-07 17:03:47 +0200 | [diff] [blame] | 130 | 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] | 131 | self.server.bind(self.port) |
| 132 | self.server.start(1) |
| 133 | |
| 134 | # client |
Roger Meier | d52edba | 2014-08-07 17:03:47 +0200 | [diff] [blame] | 135 | transport = TTornado.TTornadoStreamTransport('localhost', self.port, io_loop=self.io_loop) |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 136 | pfactory = TBinaryProtocol.TBinaryProtocolFactory() |
Roger Meier | d52edba | 2014-08-07 17:03:47 +0200 | [diff] [blame] | 137 | self.io_loop.run_sync(transport.open) |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 138 | self.client = ThriftTest.Client(transport, pfactory) |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 139 | |
Roger Meier | d52edba | 2014-08-07 17:03:47 +0200 | [diff] [blame] | 140 | @gen_test |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 141 | def test_void(self): |
Roger Meier | d52edba | 2014-08-07 17:03:47 +0200 | [diff] [blame] | 142 | v = yield self.client.testVoid() |
| 143 | self.assertEqual(v, None) |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 144 | |
Roger Meier | d52edba | 2014-08-07 17:03:47 +0200 | [diff] [blame] | 145 | @gen_test |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 146 | def test_string(self): |
Roger Meier | d52edba | 2014-08-07 17:03:47 +0200 | [diff] [blame] | 147 | v = yield self.client.testString('Python') |
| 148 | self.assertEqual(v, 'Python') |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 149 | |
Roger Meier | d52edba | 2014-08-07 17:03:47 +0200 | [diff] [blame] | 150 | @gen_test |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 151 | def test_byte(self): |
Roger Meier | d52edba | 2014-08-07 17:03:47 +0200 | [diff] [blame] | 152 | v = yield self.client.testByte(63) |
| 153 | self.assertEqual(v, 63) |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 154 | |
Roger Meier | d52edba | 2014-08-07 17:03:47 +0200 | [diff] [blame] | 155 | @gen_test |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 156 | def test_i32(self): |
Roger Meier | d52edba | 2014-08-07 17:03:47 +0200 | [diff] [blame] | 157 | v = yield self.client.testI32(-1) |
| 158 | self.assertEqual(v, -1) |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 159 | |
Roger Meier | d52edba | 2014-08-07 17:03:47 +0200 | [diff] [blame] | 160 | v = yield self.client.testI32(0) |
| 161 | self.assertEqual(v, 0) |
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 | @gen_test |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 164 | def test_i64(self): |
Roger Meier | d52edba | 2014-08-07 17:03:47 +0200 | [diff] [blame] | 165 | v = yield self.client.testI64(-34359738368) |
| 166 | self.assertEqual(v, -34359738368) |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 167 | |
Roger Meier | d52edba | 2014-08-07 17:03:47 +0200 | [diff] [blame] | 168 | @gen_test |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 169 | def test_double(self): |
Roger Meier | d52edba | 2014-08-07 17:03:47 +0200 | [diff] [blame] | 170 | v = yield self.client.testDouble(-5.235098235) |
| 171 | self.assertEqual(v, -5.235098235) |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 172 | |
Roger Meier | d52edba | 2014-08-07 17:03:47 +0200 | [diff] [blame] | 173 | @gen_test |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 174 | def test_struct(self): |
| 175 | x = Xtruct() |
| 176 | x.string_thing = "Zero" |
| 177 | x.byte_thing = 1 |
| 178 | x.i32_thing = -3 |
| 179 | x.i64_thing = -5 |
Roger Meier | d52edba | 2014-08-07 17:03:47 +0200 | [diff] [blame] | 180 | y = yield self.client.testStruct(x) |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 181 | |
Roger Meier | d52edba | 2014-08-07 17:03:47 +0200 | [diff] [blame] | 182 | self.assertEqual(y.string_thing, "Zero") |
| 183 | self.assertEqual(y.byte_thing, 1) |
| 184 | self.assertEqual(y.i32_thing, -3) |
| 185 | self.assertEqual(y.i64_thing, -5) |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 186 | |
| 187 | def test_oneway(self): |
Roger Meier | d52edba | 2014-08-07 17:03:47 +0200 | [diff] [blame] | 188 | self.client.testOneway(0.5) |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 189 | start, end, seconds = self.wait(timeout=1) |
| 190 | self.assertAlmostEquals(seconds, (end - start), places=3) |
| 191 | |
Roger Meier | d52edba | 2014-08-07 17:03:47 +0200 | [diff] [blame] | 192 | @gen_test |
| 193 | def test_map(self): |
| 194 | """ |
| 195 | TestHandler.testMap is a coroutine, this test checks if gen.Return() from a coroutine works. |
| 196 | """ |
| 197 | expected = {1: 1} |
| 198 | res = yield self.client.testMap(expected) |
| 199 | self.assertEqual(res, expected) |
| 200 | |
| 201 | @gen_test |
| 202 | def test_exception(self): |
| 203 | yield self.client.testException('Safe') |
| 204 | |
| 205 | try: |
| 206 | yield self.client.testException('Xception') |
| 207 | except Xception as ex: |
| 208 | self.assertEqual(ex.errorCode, 1001) |
| 209 | self.assertEqual(ex.message, 'Xception') |
| 210 | else: |
| 211 | self.fail("should have gotten exception") |
| 212 | try: |
| 213 | yield self.client.testException('throw_undeclared') |
| 214 | except TTransportException as ex: |
| 215 | pass |
| 216 | else: |
| 217 | self.fail("should have gotten exception") |
| 218 | |
Chris Piro | 20c81ad | 2013-03-07 11:32:48 -0500 | [diff] [blame] | 219 | |
| 220 | def suite(): |
| 221 | suite = unittest.TestSuite() |
| 222 | loader = unittest.TestLoader() |
| 223 | suite.addTest(loader.loadTestsFromTestCase(ThriftTestCase)) |
| 224 | return suite |
| 225 | |
| 226 | |
| 227 | if __name__ == '__main__': |
| 228 | unittest.TestProgram(defaultTest='suite', |
| 229 | testRunner=unittest.TextTestRunner(verbosity=1)) |