blob: 886de44d26adae90dea67c643d60bfd8f51e0db3 [file] [log] [blame]
jfarrell6cdbd982015-06-23 21:58:50 -04001#!/usr/bin/env python
2
Roger Meier50e43492010-10-08 17:46:06 +00003#
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
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090022import sys
23import os
24import glob
25import time
jfarrell6cdbd982015-06-23 21:58:50 -040026basepath = os.path.abspath(os.path.dirname(__file__))
27sys.path.insert(0, os.path.join(basepath, 'gen-py.twisted'))
28sys.path.insert(0, glob.glob(os.path.join(basepath, '../../lib/py/build/lib.*'))[0])
Roger Meier50e43492010-10-08 17:46:06 +000029
30from ThriftTest import ThriftTest
James Broadhead79c3f4a2014-08-09 18:21:44 +010031from ThriftTest.ttypes import Xception, Xtruct
Roger Meier50e43492010-10-08 17:46:06 +000032from thrift.transport import TTwisted
33from thrift.protocol import TBinaryProtocol
34
35from twisted.trial import unittest
36from twisted.internet import defer, reactor
37from twisted.internet.protocol import ClientCreator
38
James E. King, IIId7142b72017-09-01 13:00:36 -070039from zope.interface import implementer
Roger Meier50e43492010-10-08 17:46:06 +000040
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090041
James E. King, IIId7142b72017-09-01 13:00:36 -070042@implementer(ThriftTest.Iface)
Roger Meier50e43492010-10-08 17:46:06 +000043class TestHandler:
Roger Meier50e43492010-10-08 17:46:06 +000044 def __init__(self):
45 self.onewaysQueue = defer.DeferredQueue()
46
47 def testVoid(self):
48 pass
James Broadhead79c3f4a2014-08-09 18:21:44 +010049
Roger Meier50e43492010-10-08 17:46:06 +000050 def testString(self, s):
51 return s
James Broadhead79c3f4a2014-08-09 18:21:44 +010052
Roger Meier50e43492010-10-08 17:46:06 +000053 def testByte(self, b):
54 return b
James Broadhead79c3f4a2014-08-09 18:21:44 +010055
Roger Meier50e43492010-10-08 17:46:06 +000056 def testI16(self, i16):
57 return i16
James Broadhead79c3f4a2014-08-09 18:21:44 +010058
Roger Meier50e43492010-10-08 17:46:06 +000059 def testI32(self, i32):
60 return i32
James Broadhead79c3f4a2014-08-09 18:21:44 +010061
Roger Meier50e43492010-10-08 17:46:06 +000062 def testI64(self, i64):
63 return i64
James Broadhead79c3f4a2014-08-09 18:21:44 +010064
Roger Meier50e43492010-10-08 17:46:06 +000065 def testDouble(self, dub):
66 return dub
James Broadhead79c3f4a2014-08-09 18:21:44 +010067
Jens Geyer8bcfdd92014-12-14 03:14:26 +010068 def testBinary(self, thing):
69 return thing
70
Roger Meier50e43492010-10-08 17:46:06 +000071 def testStruct(self, thing):
72 return thing
James Broadhead79c3f4a2014-08-09 18:21:44 +010073
Roger Meier50e43492010-10-08 17:46:06 +000074 def testException(self, s):
75 if s == 'Xception':
76 x = Xception()
77 x.errorCode = 1001
78 x.message = s
79 raise x
80 elif s == "throw_undeclared":
81 raise ValueError("foo")
James Broadhead79c3f4a2014-08-09 18:21:44 +010082
Roger Meier50e43492010-10-08 17:46:06 +000083 def testOneway(self, seconds):
84 def fireOneway(t):
85 self.onewaysQueue.put((t, time.time(), seconds))
86 reactor.callLater(seconds, fireOneway, time.time())
James Broadhead79c3f4a2014-08-09 18:21:44 +010087
Roger Meier50e43492010-10-08 17:46:06 +000088 def testNest(self, thing):
89 return thing
James Broadhead79c3f4a2014-08-09 18:21:44 +010090
Roger Meier50e43492010-10-08 17:46:06 +000091 def testMap(self, thing):
92 return thing
James Broadhead79c3f4a2014-08-09 18:21:44 +010093
Roger Meier50e43492010-10-08 17:46:06 +000094 def testSet(self, thing):
95 return thing
James Broadhead79c3f4a2014-08-09 18:21:44 +010096
Roger Meier50e43492010-10-08 17:46:06 +000097 def testList(self, thing):
98 return thing
James Broadhead79c3f4a2014-08-09 18:21:44 +010099
Roger Meier50e43492010-10-08 17:46:06 +0000100 def testEnum(self, thing):
101 return thing
James Broadhead79c3f4a2014-08-09 18:21:44 +0100102
Roger Meier50e43492010-10-08 17:46:06 +0000103 def testTypedef(self, thing):
104 return thing
105
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900106
Roger Meier50e43492010-10-08 17:46:06 +0000107class ThriftTestCase(unittest.TestCase):
108
109 @defer.inlineCallbacks
110 def setUp(self):
111 self.handler = TestHandler()
112 self.processor = ThriftTest.Processor(self.handler)
113 self.pfactory = TBinaryProtocol.TBinaryProtocolFactory()
114
Nobuaki Sukegawad479e232016-02-28 11:28:19 +0900115 self.server = reactor.listenTCP(
116 0, TTwisted.ThriftServerFactory(self.processor, self.pfactory), interface="127.0.0.1")
Roger Meier50e43492010-10-08 17:46:06 +0000117
118 self.portNo = self.server.getHost().port
119
120 self.txclient = yield ClientCreator(reactor,
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900121 TTwisted.ThriftClientProtocol,
122 ThriftTest.Client,
123 self.pfactory).connectTCP("127.0.0.1", self.portNo)
Roger Meier50e43492010-10-08 17:46:06 +0000124 self.client = self.txclient.client
125
126 @defer.inlineCallbacks
127 def tearDown(self):
128 yield self.server.stopListening()
129 self.txclient.transport.loseConnection()
130
131 @defer.inlineCallbacks
132 def testVoid(self):
133 self.assertEquals((yield self.client.testVoid()), None)
134
135 @defer.inlineCallbacks
136 def testString(self):
137 self.assertEquals((yield self.client.testString('Python')), 'Python')
James Broadhead79c3f4a2014-08-09 18:21:44 +0100138
Roger Meier50e43492010-10-08 17:46:06 +0000139 @defer.inlineCallbacks
140 def testByte(self):
141 self.assertEquals((yield self.client.testByte(63)), 63)
142
143 @defer.inlineCallbacks
144 def testI32(self):
145 self.assertEquals((yield self.client.testI32(-1)), -1)
146 self.assertEquals((yield self.client.testI32(0)), 0)
147
148 @defer.inlineCallbacks
149 def testI64(self):
150 self.assertEquals((yield self.client.testI64(-34359738368)), -34359738368)
151
152 @defer.inlineCallbacks
153 def testDouble(self):
154 self.assertEquals((yield self.client.testDouble(-5.235098235)), -5.235098235)
155
jfarrell6cdbd982015-06-23 21:58:50 -0400156 # TODO: def testBinary(self) ...
157
James Broadhead79c3f4a2014-08-09 18:21:44 +0100158 @defer.inlineCallbacks
Roger Meier50e43492010-10-08 17:46:06 +0000159 def testStruct(self):
160 x = Xtruct()
161 x.string_thing = "Zero"
162 x.byte_thing = 1
163 x.i32_thing = -3
164 x.i64_thing = -5
165 y = yield self.client.testStruct(x)
James Broadhead79c3f4a2014-08-09 18:21:44 +0100166
Roger Meier50e43492010-10-08 17:46:06 +0000167 self.assertEquals(y.string_thing, "Zero")
168 self.assertEquals(y.byte_thing, 1)
169 self.assertEquals(y.i32_thing, -3)
170 self.assertEquals(y.i64_thing, -5)
James Broadhead79c3f4a2014-08-09 18:21:44 +0100171
Roger Meier50e43492010-10-08 17:46:06 +0000172 @defer.inlineCallbacks
173 def testException(self):
174 yield self.client.testException('Safe')
175 try:
176 yield self.client.testException('Xception')
177 self.fail("should have gotten exception")
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900178 except Xception as x:
Roger Meier50e43492010-10-08 17:46:06 +0000179 self.assertEquals(x.errorCode, 1001)
180 self.assertEquals(x.message, 'Xception')
181
182 try:
183 yield self.client.testException("throw_undeclared")
184 self.fail("should have thrown exception")
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900185 except Exception: # type is undefined
Roger Meier50e43492010-10-08 17:46:06 +0000186 pass
James Broadhead79c3f4a2014-08-09 18:21:44 +0100187
Roger Meier50e43492010-10-08 17:46:06 +0000188 @defer.inlineCallbacks
189 def testOneway(self):
James Broadhead79c3f4a2014-08-09 18:21:44 +0100190 yield self.client.testOneway(1)
Roger Meier50e43492010-10-08 17:46:06 +0000191 start, end, seconds = yield self.handler.onewaysQueue.get()
James Broadhead79c3f4a2014-08-09 18:21:44 +0100192 self.assertAlmostEquals(seconds, (end - start), places=1)