blob: 26c1ee2a135403209c380b6a90f609b612d10fe3 [file] [log] [blame]
Roger Meier50e43492010-10-08 17:46:06 +00001#
2# Licensed to the Apache Software Foundation (ASF) under one
3# or more contributor license agreements. See the NOTICE file
4# distributed with this work for additional information
5# regarding copyright ownership. The ASF licenses this file
6# to you under the Apache License, Version 2.0 (the
7# "License"); you may not use this file except in compliance
8# with the License. You may obtain a copy of the License at
9#
10# http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing,
13# software distributed under the License is distributed on an
14# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15# KIND, either express or implied. See the License for the
16# specific language governing permissions and limitations
17# under the License.
18#
19
20import sys, glob, time
21sys.path.insert(0, './gen-py.twisted')
22sys.path.insert(0, glob.glob('../../lib/py/build/lib.*')[0])
23
24from ThriftTest import ThriftTest
James Broadhead79c3f4a2014-08-09 18:21:44 +010025from ThriftTest.ttypes import Xception, Xtruct
Roger Meier50e43492010-10-08 17:46:06 +000026from thrift.transport import TTwisted
27from thrift.protocol import TBinaryProtocol
28
29from twisted.trial import unittest
30from twisted.internet import defer, reactor
31from twisted.internet.protocol import ClientCreator
32
33from zope.interface import implements
34
Roger Meier50e43492010-10-08 17:46:06 +000035class TestHandler:
36 implements(ThriftTest.Iface)
37
38 def __init__(self):
39 self.onewaysQueue = defer.DeferredQueue()
40
41 def testVoid(self):
42 pass
James Broadhead79c3f4a2014-08-09 18:21:44 +010043
Roger Meier50e43492010-10-08 17:46:06 +000044 def testString(self, s):
45 return s
James Broadhead79c3f4a2014-08-09 18:21:44 +010046
Roger Meier50e43492010-10-08 17:46:06 +000047 def testByte(self, b):
48 return b
James Broadhead79c3f4a2014-08-09 18:21:44 +010049
Roger Meier50e43492010-10-08 17:46:06 +000050 def testI16(self, i16):
51 return i16
James Broadhead79c3f4a2014-08-09 18:21:44 +010052
Roger Meier50e43492010-10-08 17:46:06 +000053 def testI32(self, i32):
54 return i32
James Broadhead79c3f4a2014-08-09 18:21:44 +010055
Roger Meier50e43492010-10-08 17:46:06 +000056 def testI64(self, i64):
57 return i64
James Broadhead79c3f4a2014-08-09 18:21:44 +010058
Roger Meier50e43492010-10-08 17:46:06 +000059 def testDouble(self, dub):
60 return dub
James Broadhead79c3f4a2014-08-09 18:21:44 +010061
Jens Geyer8bcfdd92014-12-14 03:14:26 +010062 def testBinary(self, thing):
63 return thing
64
Roger Meier50e43492010-10-08 17:46:06 +000065 def testStruct(self, thing):
66 return thing
James Broadhead79c3f4a2014-08-09 18:21:44 +010067
Roger Meier50e43492010-10-08 17:46:06 +000068 def testException(self, s):
69 if s == 'Xception':
70 x = Xception()
71 x.errorCode = 1001
72 x.message = s
73 raise x
74 elif s == "throw_undeclared":
75 raise ValueError("foo")
James Broadhead79c3f4a2014-08-09 18:21:44 +010076
Roger Meier50e43492010-10-08 17:46:06 +000077 def testOneway(self, seconds):
78 def fireOneway(t):
79 self.onewaysQueue.put((t, time.time(), seconds))
80 reactor.callLater(seconds, fireOneway, time.time())
James Broadhead79c3f4a2014-08-09 18:21:44 +010081
Roger Meier50e43492010-10-08 17:46:06 +000082 def testNest(self, thing):
83 return thing
James Broadhead79c3f4a2014-08-09 18:21:44 +010084
Roger Meier50e43492010-10-08 17:46:06 +000085 def testMap(self, thing):
86 return thing
James Broadhead79c3f4a2014-08-09 18:21:44 +010087
Roger Meier50e43492010-10-08 17:46:06 +000088 def testSet(self, thing):
89 return thing
James Broadhead79c3f4a2014-08-09 18:21:44 +010090
Roger Meier50e43492010-10-08 17:46:06 +000091 def testList(self, thing):
92 return thing
James Broadhead79c3f4a2014-08-09 18:21:44 +010093
Roger Meier50e43492010-10-08 17:46:06 +000094 def testEnum(self, thing):
95 return thing
James Broadhead79c3f4a2014-08-09 18:21:44 +010096
Roger Meier50e43492010-10-08 17:46:06 +000097 def testTypedef(self, thing):
98 return thing
99
100class ThriftTestCase(unittest.TestCase):
101
102 @defer.inlineCallbacks
103 def setUp(self):
104 self.handler = TestHandler()
105 self.processor = ThriftTest.Processor(self.handler)
106 self.pfactory = TBinaryProtocol.TBinaryProtocolFactory()
107
108 self.server = reactor.listenTCP(0,
109 TTwisted.ThriftServerFactory(self.processor,
110 self.pfactory), interface="127.0.0.1")
111
112 self.portNo = self.server.getHost().port
113
114 self.txclient = yield ClientCreator(reactor,
115 TTwisted.ThriftClientProtocol,
116 ThriftTest.Client,
117 self.pfactory).connectTCP("127.0.0.1", self.portNo)
118 self.client = self.txclient.client
119
120 @defer.inlineCallbacks
121 def tearDown(self):
122 yield self.server.stopListening()
123 self.txclient.transport.loseConnection()
124
125 @defer.inlineCallbacks
126 def testVoid(self):
127 self.assertEquals((yield self.client.testVoid()), None)
128
129 @defer.inlineCallbacks
130 def testString(self):
131 self.assertEquals((yield self.client.testString('Python')), 'Python')
James Broadhead79c3f4a2014-08-09 18:21:44 +0100132
Roger Meier50e43492010-10-08 17:46:06 +0000133 @defer.inlineCallbacks
134 def testByte(self):
135 self.assertEquals((yield self.client.testByte(63)), 63)
136
137 @defer.inlineCallbacks
138 def testI32(self):
139 self.assertEquals((yield self.client.testI32(-1)), -1)
140 self.assertEquals((yield self.client.testI32(0)), 0)
141
142 @defer.inlineCallbacks
143 def testI64(self):
144 self.assertEquals((yield self.client.testI64(-34359738368)), -34359738368)
145
146 @defer.inlineCallbacks
147 def testDouble(self):
148 self.assertEquals((yield self.client.testDouble(-5.235098235)), -5.235098235)
149
Jens Geyer8bcfdd92014-12-14 03:14:26 +0100150 # TODO: def testBinary(self) ...
151
James Broadhead79c3f4a2014-08-09 18:21:44 +0100152 @defer.inlineCallbacks
Roger Meier50e43492010-10-08 17:46:06 +0000153 def testStruct(self):
154 x = Xtruct()
155 x.string_thing = "Zero"
156 x.byte_thing = 1
157 x.i32_thing = -3
158 x.i64_thing = -5
159 y = yield self.client.testStruct(x)
James Broadhead79c3f4a2014-08-09 18:21:44 +0100160
Roger Meier50e43492010-10-08 17:46:06 +0000161 self.assertEquals(y.string_thing, "Zero")
162 self.assertEquals(y.byte_thing, 1)
163 self.assertEquals(y.i32_thing, -3)
164 self.assertEquals(y.i64_thing, -5)
James Broadhead79c3f4a2014-08-09 18:21:44 +0100165
Roger Meier50e43492010-10-08 17:46:06 +0000166 @defer.inlineCallbacks
167 def testException(self):
168 yield self.client.testException('Safe')
169 try:
170 yield self.client.testException('Xception')
171 self.fail("should have gotten exception")
172 except Xception, x:
173 self.assertEquals(x.errorCode, 1001)
174 self.assertEquals(x.message, 'Xception')
175
176 try:
177 yield self.client.testException("throw_undeclared")
178 self.fail("should have thrown exception")
179 except Exception: # type is undefined
180 pass
James Broadhead79c3f4a2014-08-09 18:21:44 +0100181
Roger Meier50e43492010-10-08 17:46:06 +0000182 @defer.inlineCallbacks
183 def testOneway(self):
James Broadhead79c3f4a2014-08-09 18:21:44 +0100184 yield self.client.testOneway(1)
Roger Meier50e43492010-10-08 17:46:06 +0000185 start, end, seconds = yield self.handler.onewaysQueue.get()
James Broadhead79c3f4a2014-08-09 18:21:44 +0100186 self.assertAlmostEquals(seconds, (end - start), places=1)