blob: f04ba0404473985ad9602318bad74bb8f3e0706b [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
24import sys
25import time
26import unittest
27
28sys.path.insert(0, './gen-py.tornado')
29sys.path.insert(0, glob.glob('../../lib/py/build/lib.*')[0])
30
31try:
32 __import__('tornado')
33except ImportError:
34 print "module `tornado` not found, skipping test"
35 sys.exit(0)
36
37from tornado import gen, ioloop, stack_context
38from tornado.testing import AsyncTestCase, get_unused_port
39
40from thrift import TTornado
41from thrift.protocol import TBinaryProtocol
42
43from ThriftTest import ThriftTest
44from ThriftTest.ttypes import *
45
46
47class TestHandler(object):
48 def __init__(self, test_instance):
49 self.test_instance = test_instance
50
51 def testVoid(self, callback):
52 callback()
53
54 def testString(self, s, callback):
55 callback(s)
56
57 def testByte(self, b, callback):
58 callback(b)
59
60 def testI16(self, i16, callback):
61 callback(i16)
62
63 def testI32(self, i32, callback):
64 callback(i32)
65
66 def testI64(self, i64, callback):
67 callback(i64)
68
69 def testDouble(self, dub, callback):
70 callback(dub)
71
72 def testStruct(self, thing, callback):
73 callback(thing)
74
75 def testException(self, s, callback):
76 if s == 'Xception':
77 x = Xception()
78 x.errorCode = 1001
79 x.message = s
80 raise x
81 elif s == 'throw_undeclared':
82 raise ValueError("foo")
83 callback()
84
85 def testOneway(self, seconds, callback=None):
86 start = time.time()
87 def fire_oneway():
88 end = time.time()
89 self.test_instance.stop((start, end, seconds))
90
91 ioloop.IOLoop.instance().add_timeout(
92 datetime.timedelta(seconds=seconds),
93 fire_oneway)
94
95 if callback:
96 callback()
97
98 def testNest(self, thing, callback):
99 callback(thing)
100
101 def testMap(self, thing, callback):
102 callback(thing)
103
104 def testSet(self, thing, callback):
105 callback(thing)
106
107 def testList(self, thing, callback):
108 callback(thing)
109
110 def testEnum(self, thing, callback):
111 callback(thing)
112
113 def testTypedef(self, thing, callback):
114 callback(thing)
115
116
117class ThriftTestCase(AsyncTestCase):
118 def get_new_ioloop(self):
119 return ioloop.IOLoop.instance()
120
121 def setUp(self):
122 self.port = get_unused_port()
123 self.io_loop = self.get_new_ioloop()
124
125 # server
126 self.handler = TestHandler(self)
127 self.processor = ThriftTest.Processor(self.handler)
128 self.pfactory = TBinaryProtocol.TBinaryProtocolFactory()
129
130 self.server = TTornado.TTornadoServer(self.processor, self.pfactory)
131 self.server.bind(self.port)
132 self.server.start(1)
133
134 # client
135 transport = TTornado.TTornadoStreamTransport('localhost', self.port)
136 pfactory = TBinaryProtocol.TBinaryProtocolFactory()
137 self.client = ThriftTest.Client(transport, pfactory)
138 transport.open(callback=self.stop)
139 self.wait(timeout=1)
140
141 def test_void(self):
142 self.client.testVoid(callback=self.stop)
143 v = self.wait(timeout=1)
144 self.assertEquals(v, None)
145
146 def test_string(self):
147 self.client.testString('Python', callback=self.stop)
148 v = self.wait(timeout=1)
149 self.assertEquals(v, 'Python')
150
151 def test_byte(self):
152 self.client.testByte(63, callback=self.stop)
153 v = self.wait(timeout=1)
154 self.assertEquals(v, 63)
155
156 def test_i32(self):
157 self.client.testI32(-1, callback=self.stop)
158 v = self.wait(timeout=1)
159 self.assertEquals(v, -1)
160
161 self.client.testI32(0, callback=self.stop)
162 v = self.wait(timeout=1)
163 self.assertEquals(v, 0)
164
165 def test_i64(self):
166 self.client.testI64(-34359738368, callback=self.stop)
167 v = self.wait(timeout=1)
168 self.assertEquals(v, -34359738368)
169
170 def test_double(self):
171 self.client.testDouble(-5.235098235, callback=self.stop)
172 v = self.wait(timeout=1)
173 self.assertEquals(v, -5.235098235)
174
175 def test_struct(self):
176 x = Xtruct()
177 x.string_thing = "Zero"
178 x.byte_thing = 1
179 x.i32_thing = -3
180 x.i64_thing = -5
181 self.client.testStruct(x, callback=self.stop)
182
183 y = self.wait(timeout=1)
184 self.assertEquals(y.string_thing, "Zero")
185 self.assertEquals(y.byte_thing, 1)
186 self.assertEquals(y.i32_thing, -3)
187 self.assertEquals(y.i64_thing, -5)
188
189 def test_exception(self):
190 self.client.testException('Safe', callback=self.stop)
191 v = self.wait(timeout=1)
192
193 self.client.testException('Xception', callback=self.stop)
194 ex = self.wait(timeout=1)
195 if type(ex) == Xception:
196 self.assertEquals(ex.errorCode, 1001)
197 self.assertEquals(ex.message, 'Xception')
198 else:
199 self.fail("should have gotten exception")
200
201 def test_oneway(self):
202 def return_from_send():
203 self.stop('done with send')
204 self.client.testOneway(0.5, callback=return_from_send)
205 self.assertEquals(self.wait(timeout=1), 'done with send')
206
207 start, end, seconds = self.wait(timeout=1)
208 self.assertAlmostEquals(seconds, (end - start), places=3)
209
210
211def suite():
212 suite = unittest.TestSuite()
213 loader = unittest.TestLoader()
214 suite.addTest(loader.loadTestsFromTestCase(ThriftTestCase))
215 return suite
216
217
218if __name__ == '__main__':
219 unittest.TestProgram(defaultTest='suite',
220 testRunner=unittest.TextTestRunner(verbosity=1))