blob: 6e1fee2409b3f10172cb9cb856f6f05bacbe3caa [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
Roger Meier50e43492010-10-08 17:46:06 +000062 def testStruct(self, thing):
63 return thing
James Broadhead79c3f4a2014-08-09 18:21:44 +010064
Roger Meier50e43492010-10-08 17:46:06 +000065 def testException(self, s):
66 if s == 'Xception':
67 x = Xception()
68 x.errorCode = 1001
69 x.message = s
70 raise x
71 elif s == "throw_undeclared":
72 raise ValueError("foo")
James Broadhead79c3f4a2014-08-09 18:21:44 +010073
Roger Meier50e43492010-10-08 17:46:06 +000074 def testOneway(self, seconds):
75 def fireOneway(t):
76 self.onewaysQueue.put((t, time.time(), seconds))
77 reactor.callLater(seconds, fireOneway, time.time())
James Broadhead79c3f4a2014-08-09 18:21:44 +010078
Roger Meier50e43492010-10-08 17:46:06 +000079 def testNest(self, thing):
80 return thing
James Broadhead79c3f4a2014-08-09 18:21:44 +010081
Roger Meier50e43492010-10-08 17:46:06 +000082 def testMap(self, thing):
83 return thing
James Broadhead79c3f4a2014-08-09 18:21:44 +010084
Roger Meier50e43492010-10-08 17:46:06 +000085 def testSet(self, thing):
86 return thing
James Broadhead79c3f4a2014-08-09 18:21:44 +010087
Roger Meier50e43492010-10-08 17:46:06 +000088 def testList(self, thing):
89 return thing
James Broadhead79c3f4a2014-08-09 18:21:44 +010090
Roger Meier50e43492010-10-08 17:46:06 +000091 def testEnum(self, thing):
92 return thing
James Broadhead79c3f4a2014-08-09 18:21:44 +010093
Roger Meier50e43492010-10-08 17:46:06 +000094 def testTypedef(self, thing):
95 return thing
96
97class ThriftTestCase(unittest.TestCase):
98
99 @defer.inlineCallbacks
100 def setUp(self):
101 self.handler = TestHandler()
102 self.processor = ThriftTest.Processor(self.handler)
103 self.pfactory = TBinaryProtocol.TBinaryProtocolFactory()
104
105 self.server = reactor.listenTCP(0,
106 TTwisted.ThriftServerFactory(self.processor,
107 self.pfactory), interface="127.0.0.1")
108
109 self.portNo = self.server.getHost().port
110
111 self.txclient = yield ClientCreator(reactor,
112 TTwisted.ThriftClientProtocol,
113 ThriftTest.Client,
114 self.pfactory).connectTCP("127.0.0.1", self.portNo)
115 self.client = self.txclient.client
116
117 @defer.inlineCallbacks
118 def tearDown(self):
119 yield self.server.stopListening()
120 self.txclient.transport.loseConnection()
121
122 @defer.inlineCallbacks
123 def testVoid(self):
124 self.assertEquals((yield self.client.testVoid()), None)
125
126 @defer.inlineCallbacks
127 def testString(self):
128 self.assertEquals((yield self.client.testString('Python')), 'Python')
James Broadhead79c3f4a2014-08-09 18:21:44 +0100129
Roger Meier50e43492010-10-08 17:46:06 +0000130 @defer.inlineCallbacks
131 def testByte(self):
132 self.assertEquals((yield self.client.testByte(63)), 63)
133
134 @defer.inlineCallbacks
135 def testI32(self):
136 self.assertEquals((yield self.client.testI32(-1)), -1)
137 self.assertEquals((yield self.client.testI32(0)), 0)
138
139 @defer.inlineCallbacks
140 def testI64(self):
141 self.assertEquals((yield self.client.testI64(-34359738368)), -34359738368)
142
143 @defer.inlineCallbacks
144 def testDouble(self):
145 self.assertEquals((yield self.client.testDouble(-5.235098235)), -5.235098235)
146
James Broadhead79c3f4a2014-08-09 18:21:44 +0100147 @defer.inlineCallbacks
Roger Meier50e43492010-10-08 17:46:06 +0000148 def testStruct(self):
149 x = Xtruct()
150 x.string_thing = "Zero"
151 x.byte_thing = 1
152 x.i32_thing = -3
153 x.i64_thing = -5
154 y = yield self.client.testStruct(x)
James Broadhead79c3f4a2014-08-09 18:21:44 +0100155
Roger Meier50e43492010-10-08 17:46:06 +0000156 self.assertEquals(y.string_thing, "Zero")
157 self.assertEquals(y.byte_thing, 1)
158 self.assertEquals(y.i32_thing, -3)
159 self.assertEquals(y.i64_thing, -5)
James Broadhead79c3f4a2014-08-09 18:21:44 +0100160
Roger Meier50e43492010-10-08 17:46:06 +0000161 @defer.inlineCallbacks
162 def testException(self):
163 yield self.client.testException('Safe')
164 try:
165 yield self.client.testException('Xception')
166 self.fail("should have gotten exception")
167 except Xception, x:
168 self.assertEquals(x.errorCode, 1001)
169 self.assertEquals(x.message, 'Xception')
170
171 try:
172 yield self.client.testException("throw_undeclared")
173 self.fail("should have thrown exception")
174 except Exception: # type is undefined
175 pass
James Broadhead79c3f4a2014-08-09 18:21:44 +0100176
Roger Meier50e43492010-10-08 17:46:06 +0000177 @defer.inlineCallbacks
178 def testOneway(self):
James Broadhead79c3f4a2014-08-09 18:21:44 +0100179 yield self.client.testOneway(1)
Roger Meier50e43492010-10-08 17:46:06 +0000180 start, end, seconds = yield self.handler.onewaysQueue.get()
James Broadhead79c3f4a2014-08-09 18:21:44 +0100181 self.assertAlmostEquals(seconds, (end - start), places=1)