blob: 02eb7f14ff893ddf9d1329703429150f7b48ded6 [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 glob
Nobuaki Sukegawa6f826402016-02-10 19:42:19 +090023import os
24import sys
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090025import time
Nobuaki Sukegawa6f826402016-02-10 19:42:19 +090026
jfarrell6cdbd982015-06-23 21:58:50 -040027basepath = os.path.abspath(os.path.dirname(__file__))
28sys.path.insert(0, os.path.join(basepath, 'gen-py.twisted'))
29sys.path.insert(0, glob.glob(os.path.join(basepath, '../../lib/py/build/lib.*'))[0])
Roger Meier50e43492010-10-08 17:46:06 +000030
Nobuaki Sukegawa6f826402016-02-10 19:42:19 +090031from thrift.Thrift import TApplicationException
32
Roger Meier50e43492010-10-08 17:46:06 +000033from ThriftTest import ThriftTest
James Broadhead79c3f4a2014-08-09 18:21:44 +010034from ThriftTest.ttypes import Xception, Xtruct
Roger Meier50e43492010-10-08 17:46:06 +000035from thrift.transport import TTwisted
36from thrift.protocol import TBinaryProtocol
37
38from twisted.trial import unittest
39from twisted.internet import defer, reactor
40from twisted.internet.protocol import ClientCreator
41
James E. King, IIId7142b72017-09-01 13:00:36 -070042from zope.interface import implementer
Roger Meier50e43492010-10-08 17:46:06 +000043
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090044
James E. King, IIId7142b72017-09-01 13:00:36 -070045@implementer(ThriftTest.Iface)
Roger Meier50e43492010-10-08 17:46:06 +000046class TestHandler:
Roger Meier50e43492010-10-08 17:46:06 +000047 def __init__(self):
48 self.onewaysQueue = defer.DeferredQueue()
49
50 def testVoid(self):
51 pass
James Broadhead79c3f4a2014-08-09 18:21:44 +010052
Roger Meier50e43492010-10-08 17:46:06 +000053 def testString(self, s):
54 return s
James Broadhead79c3f4a2014-08-09 18:21:44 +010055
Roger Meier50e43492010-10-08 17:46:06 +000056 def testByte(self, b):
57 return b
James Broadhead79c3f4a2014-08-09 18:21:44 +010058
Roger Meier50e43492010-10-08 17:46:06 +000059 def testI16(self, i16):
60 return i16
James Broadhead79c3f4a2014-08-09 18:21:44 +010061
Roger Meier50e43492010-10-08 17:46:06 +000062 def testI32(self, i32):
63 return i32
James Broadhead79c3f4a2014-08-09 18:21:44 +010064
Roger Meier50e43492010-10-08 17:46:06 +000065 def testI64(self, i64):
66 return i64
James Broadhead79c3f4a2014-08-09 18:21:44 +010067
Roger Meier50e43492010-10-08 17:46:06 +000068 def testDouble(self, dub):
69 return dub
James Broadhead79c3f4a2014-08-09 18:21:44 +010070
Jens Geyer8bcfdd92014-12-14 03:14:26 +010071 def testBinary(self, thing):
72 return thing
73
Roger Meier50e43492010-10-08 17:46:06 +000074 def testStruct(self, thing):
75 return thing
James Broadhead79c3f4a2014-08-09 18:21:44 +010076
Roger Meier50e43492010-10-08 17:46:06 +000077 def testException(self, s):
78 if s == 'Xception':
79 x = Xception()
80 x.errorCode = 1001
81 x.message = s
82 raise x
83 elif s == "throw_undeclared":
84 raise ValueError("foo")
James Broadhead79c3f4a2014-08-09 18:21:44 +010085
Roger Meier50e43492010-10-08 17:46:06 +000086 def testOneway(self, seconds):
87 def fireOneway(t):
88 self.onewaysQueue.put((t, time.time(), seconds))
89 reactor.callLater(seconds, fireOneway, time.time())
Nobuaki Sukegawa6f826402016-02-10 19:42:19 +090090 raise Exception('')
James Broadhead79c3f4a2014-08-09 18:21:44 +010091
Roger Meier50e43492010-10-08 17:46:06 +000092 def testNest(self, thing):
93 return thing
James Broadhead79c3f4a2014-08-09 18:21:44 +010094
Roger Meier50e43492010-10-08 17:46:06 +000095 def testMap(self, thing):
96 return thing
James Broadhead79c3f4a2014-08-09 18:21:44 +010097
Roger Meier50e43492010-10-08 17:46:06 +000098 def testSet(self, thing):
99 return thing
James Broadhead79c3f4a2014-08-09 18:21:44 +0100100
Roger Meier50e43492010-10-08 17:46:06 +0000101 def testList(self, thing):
102 return thing
James Broadhead79c3f4a2014-08-09 18:21:44 +0100103
Roger Meier50e43492010-10-08 17:46:06 +0000104 def testEnum(self, thing):
105 return thing
James Broadhead79c3f4a2014-08-09 18:21:44 +0100106
Roger Meier50e43492010-10-08 17:46:06 +0000107 def testTypedef(self, thing):
108 return thing
109
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900110
Roger Meier50e43492010-10-08 17:46:06 +0000111class ThriftTestCase(unittest.TestCase):
112
113 @defer.inlineCallbacks
114 def setUp(self):
115 self.handler = TestHandler()
116 self.processor = ThriftTest.Processor(self.handler)
117 self.pfactory = TBinaryProtocol.TBinaryProtocolFactory()
118
Nobuaki Sukegawad479e232016-02-28 11:28:19 +0900119 self.server = reactor.listenTCP(
120 0, TTwisted.ThriftServerFactory(self.processor, self.pfactory), interface="127.0.0.1")
Roger Meier50e43492010-10-08 17:46:06 +0000121
122 self.portNo = self.server.getHost().port
123
124 self.txclient = yield ClientCreator(reactor,
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900125 TTwisted.ThriftClientProtocol,
126 ThriftTest.Client,
127 self.pfactory).connectTCP("127.0.0.1", self.portNo)
Roger Meier50e43492010-10-08 17:46:06 +0000128 self.client = self.txclient.client
129
130 @defer.inlineCallbacks
131 def tearDown(self):
132 yield self.server.stopListening()
133 self.txclient.transport.loseConnection()
134
135 @defer.inlineCallbacks
136 def testVoid(self):
137 self.assertEquals((yield self.client.testVoid()), None)
138
139 @defer.inlineCallbacks
140 def testString(self):
141 self.assertEquals((yield self.client.testString('Python')), 'Python')
James Broadhead79c3f4a2014-08-09 18:21:44 +0100142
Roger Meier50e43492010-10-08 17:46:06 +0000143 @defer.inlineCallbacks
144 def testByte(self):
145 self.assertEquals((yield self.client.testByte(63)), 63)
146
147 @defer.inlineCallbacks
148 def testI32(self):
149 self.assertEquals((yield self.client.testI32(-1)), -1)
150 self.assertEquals((yield self.client.testI32(0)), 0)
151
152 @defer.inlineCallbacks
153 def testI64(self):
154 self.assertEquals((yield self.client.testI64(-34359738368)), -34359738368)
155
156 @defer.inlineCallbacks
157 def testDouble(self):
158 self.assertEquals((yield self.client.testDouble(-5.235098235)), -5.235098235)
159
jfarrell6cdbd982015-06-23 21:58:50 -0400160 # TODO: def testBinary(self) ...
161
James Broadhead79c3f4a2014-08-09 18:21:44 +0100162 @defer.inlineCallbacks
Roger Meier50e43492010-10-08 17:46:06 +0000163 def testStruct(self):
164 x = Xtruct()
165 x.string_thing = "Zero"
166 x.byte_thing = 1
167 x.i32_thing = -3
168 x.i64_thing = -5
169 y = yield self.client.testStruct(x)
James Broadhead79c3f4a2014-08-09 18:21:44 +0100170
Roger Meier50e43492010-10-08 17:46:06 +0000171 self.assertEquals(y.string_thing, "Zero")
172 self.assertEquals(y.byte_thing, 1)
173 self.assertEquals(y.i32_thing, -3)
174 self.assertEquals(y.i64_thing, -5)
James Broadhead79c3f4a2014-08-09 18:21:44 +0100175
Roger Meier50e43492010-10-08 17:46:06 +0000176 @defer.inlineCallbacks
177 def testException(self):
Roger Meier50e43492010-10-08 17:46:06 +0000178 try:
179 yield self.client.testException('Xception')
180 self.fail("should have gotten exception")
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900181 except Xception as x:
Roger Meier50e43492010-10-08 17:46:06 +0000182 self.assertEquals(x.errorCode, 1001)
183 self.assertEquals(x.message, 'Xception')
184
185 try:
186 yield self.client.testException("throw_undeclared")
Nobuaki Sukegawa6f826402016-02-10 19:42:19 +0900187 self.fail("should have gotten exception")
188 except TApplicationException:
Roger Meier50e43492010-10-08 17:46:06 +0000189 pass
James Broadhead79c3f4a2014-08-09 18:21:44 +0100190
Nobuaki Sukegawa6f826402016-02-10 19:42:19 +0900191 yield self.client.testException('Safe')
192
Roger Meier50e43492010-10-08 17:46:06 +0000193 @defer.inlineCallbacks
194 def testOneway(self):
James Broadhead79c3f4a2014-08-09 18:21:44 +0100195 yield self.client.testOneway(1)
Roger Meier50e43492010-10-08 17:46:06 +0000196 start, end, seconds = yield self.handler.onewaysQueue.get()
James Broadhead79c3f4a2014-08-09 18:21:44 +0100197 self.assertAlmostEquals(seconds, (end - start), places=1)
Nobuaki Sukegawa6f826402016-02-10 19:42:19 +0900198 self.assertEquals((yield self.client.testI32(-1)), -1)