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
12 # http://www.apache.org/licenses/LICENSE-2.0
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
26 basepath
= os
.path
.abspath(os
.path
.dirname(__file__
))
27 sys
.path
.insert(0, os
.path
.join(basepath
, 'gen-py.twisted'))
28 sys
.path
.insert(0, glob
.glob(os
.path
.join(basepath
, '../../lib/py/build/lib.*'))[0])
30 from ThriftTest
import ThriftTest
31 from ThriftTest
.ttypes
import Xception
, Xtruct
32 from thrift
.transport
import TTwisted
33 from thrift
.protocol
import TBinaryProtocol
35 from twisted
.trial
import unittest
36 from twisted
.internet
import defer
, reactor
37 from twisted
.internet
.protocol
import ClientCreator
39 from zope
.interface
import implements
43 implements(ThriftTest
.Iface
)
46 self
.onewaysQueue
= defer
.DeferredQueue()
51 def testString(self
, s
):
54 def testByte(self
, b
):
57 def testI16(self
, i16
):
60 def testI32(self
, i32
):
63 def testI64(self
, i64
):
66 def testDouble(self
, dub
):
69 def testBinary(self
, thing
):
72 def testStruct(self
, thing
):
75 def testException(self
, s
):
81 elif s
== "throw_undeclared":
82 raise ValueError("foo")
84 def testOneway(self
, seconds
):
86 self
.onewaysQueue
.put((t
, time
.time(), seconds
))
87 reactor
.callLater(seconds
, fireOneway
, time
.time())
89 def testNest(self
, thing
):
92 def testMap(self
, thing
):
95 def testSet(self
, thing
):
98 def testList(self
, thing
):
101 def testEnum(self
, thing
):
104 def testTypedef(self
, thing
):
108 class ThriftTestCase(unittest
.TestCase
):
110 @defer.inlineCallbacks
112 self
.handler
= TestHandler()
113 self
.processor
= ThriftTest
.Processor(self
.handler
)
114 self
.pfactory
= TBinaryProtocol
.TBinaryProtocolFactory()
116 self
.server
= reactor
.listenTCP(
117 0, TTwisted
.ThriftServerFactory(self
.processor
, self
.pfactory
), interface
="127.0.0.1")
119 self
.portNo
= self
.server
.getHost().port
121 self
.txclient
= yield ClientCreator(reactor
,
122 TTwisted
.ThriftClientProtocol
,
124 self
.pfactory
).connectTCP("127.0.0.1", self
.portNo
)
125 self
.client
= self
.txclient
.client
127 @defer.inlineCallbacks
129 yield self
.server
.stopListening()
130 self
.txclient
.transport
.loseConnection()
132 @defer.inlineCallbacks
134 self
.assertEquals((yield self
.client
.testVoid()), None)
136 @defer.inlineCallbacks
137 def testString(self
):
138 self
.assertEquals((yield self
.client
.testString('Python')), 'Python')
140 @defer.inlineCallbacks
142 self
.assertEquals((yield self
.client
.testByte(63)), 63)
144 @defer.inlineCallbacks
146 self
.assertEquals((yield self
.client
.testI32(-1)), -1)
147 self
.assertEquals((yield self
.client
.testI32(0)), 0)
149 @defer.inlineCallbacks
151 self
.assertEquals((yield self
.client
.testI64(-34359738368)), -34359738368)
153 @defer.inlineCallbacks
154 def testDouble(self
):
155 self
.assertEquals((yield self
.client
.testDouble(-5.235098235)), -5.235098235)
157 # TODO: def testBinary(self) ...
159 @defer.inlineCallbacks
160 def testStruct(self
):
162 x
.string_thing
= "Zero"
166 y
= yield self
.client
.testStruct(x
)
168 self
.assertEquals(y
.string_thing
, "Zero")
169 self
.assertEquals(y
.byte_thing
, 1)
170 self
.assertEquals(y
.i32_thing
, -3)
171 self
.assertEquals(y
.i64_thing
, -5)
173 @defer.inlineCallbacks
174 def testException(self
):
175 yield self
.client
.testException('Safe')
177 yield self
.client
.testException('Xception')
178 self
.fail("should have gotten exception")
179 except Xception
as x
:
180 self
.assertEquals(x
.errorCode
, 1001)
181 self
.assertEquals(x
.message
, 'Xception')
184 yield self
.client
.testException("throw_undeclared")
185 self
.fail("should have thrown exception")
186 except Exception: # type is undefined
189 @defer.inlineCallbacks
190 def testOneway(self
):
191 yield self
.client
.testOneway(1)
192 start
, end
, seconds
= yield self
.handler
.onewaysQueue
.get()
193 self
.assertAlmostEquals(seconds
, (end
- start
), places
=1)