blob: 048abc5bc6d95ec567f7381bfc07ac5b0c56d6d2 [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
jfarrell6cdbd982015-06-23 21:58:50 -040022import sys, os, glob, time
23basepath = os.path.abspath(os.path.dirname(__file__))
24sys.path.insert(0, os.path.join(basepath, 'gen-py.twisted'))
25sys.path.insert(0, glob.glob(os.path.join(basepath, '../../lib/py/build/lib.*'))[0])
Roger Meier50e43492010-10-08 17:46:06 +000026
27from ThriftTest import ThriftTest
James Broadhead79c3f4a2014-08-09 18:21:44 +010028from ThriftTest.ttypes import Xception, Xtruct
Roger Meier50e43492010-10-08 17:46:06 +000029from thrift.transport import TTwisted
30from thrift.protocol import TBinaryProtocol
31
32from twisted.trial import unittest
33from twisted.internet import defer, reactor
34from twisted.internet.protocol import ClientCreator
35
36from zope.interface import implements
37
Roger Meier50e43492010-10-08 17:46:06 +000038class TestHandler:
39 implements(ThriftTest.Iface)
40
41 def __init__(self):
42 self.onewaysQueue = defer.DeferredQueue()
43
44 def testVoid(self):
45 pass
James Broadhead79c3f4a2014-08-09 18:21:44 +010046
Roger Meier50e43492010-10-08 17:46:06 +000047 def testString(self, s):
48 return s
James Broadhead79c3f4a2014-08-09 18:21:44 +010049
Roger Meier50e43492010-10-08 17:46:06 +000050 def testByte(self, b):
51 return b
James Broadhead79c3f4a2014-08-09 18:21:44 +010052
Roger Meier50e43492010-10-08 17:46:06 +000053 def testI16(self, i16):
54 return i16
James Broadhead79c3f4a2014-08-09 18:21:44 +010055
Roger Meier50e43492010-10-08 17:46:06 +000056 def testI32(self, i32):
57 return i32
James Broadhead79c3f4a2014-08-09 18:21:44 +010058
Roger Meier50e43492010-10-08 17:46:06 +000059 def testI64(self, i64):
60 return i64
James Broadhead79c3f4a2014-08-09 18:21:44 +010061
Roger Meier50e43492010-10-08 17:46:06 +000062 def testDouble(self, dub):
63 return dub
James Broadhead79c3f4a2014-08-09 18:21:44 +010064
Jens Geyer8bcfdd92014-12-14 03:14:26 +010065 def testBinary(self, thing):
66 return thing
67
Roger Meier50e43492010-10-08 17:46:06 +000068 def testStruct(self, thing):
69 return thing
James Broadhead79c3f4a2014-08-09 18:21:44 +010070
Roger Meier50e43492010-10-08 17:46:06 +000071 def testException(self, s):
72 if s == 'Xception':
73 x = Xception()
74 x.errorCode = 1001
75 x.message = s
76 raise x
77 elif s == "throw_undeclared":
78 raise ValueError("foo")
James Broadhead79c3f4a2014-08-09 18:21:44 +010079
Roger Meier50e43492010-10-08 17:46:06 +000080 def testOneway(self, seconds):
81 def fireOneway(t):
82 self.onewaysQueue.put((t, time.time(), seconds))
83 reactor.callLater(seconds, fireOneway, time.time())
James Broadhead79c3f4a2014-08-09 18:21:44 +010084
Roger Meier50e43492010-10-08 17:46:06 +000085 def testNest(self, thing):
86 return thing
James Broadhead79c3f4a2014-08-09 18:21:44 +010087
Roger Meier50e43492010-10-08 17:46:06 +000088 def testMap(self, thing):
89 return thing
James Broadhead79c3f4a2014-08-09 18:21:44 +010090
Roger Meier50e43492010-10-08 17:46:06 +000091 def testSet(self, thing):
92 return thing
James Broadhead79c3f4a2014-08-09 18:21:44 +010093
Roger Meier50e43492010-10-08 17:46:06 +000094 def testList(self, thing):
95 return thing
James Broadhead79c3f4a2014-08-09 18:21:44 +010096
Roger Meier50e43492010-10-08 17:46:06 +000097 def testEnum(self, thing):
98 return thing
James Broadhead79c3f4a2014-08-09 18:21:44 +010099
Roger Meier50e43492010-10-08 17:46:06 +0000100 def testTypedef(self, thing):
101 return thing
102
103class ThriftTestCase(unittest.TestCase):
104
105 @defer.inlineCallbacks
106 def setUp(self):
107 self.handler = TestHandler()
108 self.processor = ThriftTest.Processor(self.handler)
109 self.pfactory = TBinaryProtocol.TBinaryProtocolFactory()
110
111 self.server = reactor.listenTCP(0,
112 TTwisted.ThriftServerFactory(self.processor,
113 self.pfactory), interface="127.0.0.1")
114
115 self.portNo = self.server.getHost().port
116
117 self.txclient = yield ClientCreator(reactor,
118 TTwisted.ThriftClientProtocol,
119 ThriftTest.Client,
120 self.pfactory).connectTCP("127.0.0.1", self.portNo)
121 self.client = self.txclient.client
122
123 @defer.inlineCallbacks
124 def tearDown(self):
125 yield self.server.stopListening()
126 self.txclient.transport.loseConnection()
127
128 @defer.inlineCallbacks
129 def testVoid(self):
130 self.assertEquals((yield self.client.testVoid()), None)
131
132 @defer.inlineCallbacks
133 def testString(self):
134 self.assertEquals((yield self.client.testString('Python')), 'Python')
James Broadhead79c3f4a2014-08-09 18:21:44 +0100135
Roger Meier50e43492010-10-08 17:46:06 +0000136 @defer.inlineCallbacks
137 def testByte(self):
138 self.assertEquals((yield self.client.testByte(63)), 63)
139
140 @defer.inlineCallbacks
141 def testI32(self):
142 self.assertEquals((yield self.client.testI32(-1)), -1)
143 self.assertEquals((yield self.client.testI32(0)), 0)
144
145 @defer.inlineCallbacks
146 def testI64(self):
147 self.assertEquals((yield self.client.testI64(-34359738368)), -34359738368)
148
149 @defer.inlineCallbacks
150 def testDouble(self):
151 self.assertEquals((yield self.client.testDouble(-5.235098235)), -5.235098235)
152
jfarrell6cdbd982015-06-23 21:58:50 -0400153 # TODO: def testBinary(self) ...
154
James Broadhead79c3f4a2014-08-09 18:21:44 +0100155 @defer.inlineCallbacks
Roger Meier50e43492010-10-08 17:46:06 +0000156 def testStruct(self):
157 x = Xtruct()
158 x.string_thing = "Zero"
159 x.byte_thing = 1
160 x.i32_thing = -3
161 x.i64_thing = -5
162 y = yield self.client.testStruct(x)
James Broadhead79c3f4a2014-08-09 18:21:44 +0100163
Roger Meier50e43492010-10-08 17:46:06 +0000164 self.assertEquals(y.string_thing, "Zero")
165 self.assertEquals(y.byte_thing, 1)
166 self.assertEquals(y.i32_thing, -3)
167 self.assertEquals(y.i64_thing, -5)
James Broadhead79c3f4a2014-08-09 18:21:44 +0100168
Roger Meier50e43492010-10-08 17:46:06 +0000169 @defer.inlineCallbacks
170 def testException(self):
171 yield self.client.testException('Safe')
172 try:
173 yield self.client.testException('Xception')
174 self.fail("should have gotten exception")
175 except Xception, x:
176 self.assertEquals(x.errorCode, 1001)
177 self.assertEquals(x.message, 'Xception')
178
179 try:
180 yield self.client.testException("throw_undeclared")
181 self.fail("should have thrown exception")
182 except Exception: # type is undefined
183 pass
James Broadhead79c3f4a2014-08-09 18:21:44 +0100184
Roger Meier50e43492010-10-08 17:46:06 +0000185 @defer.inlineCallbacks
186 def testOneway(self):
James Broadhead79c3f4a2014-08-09 18:21:44 +0100187 yield self.client.testOneway(1)
Roger Meier50e43492010-10-08 17:46:06 +0000188 start, end, seconds = yield self.handler.onewaysQueue.get()
James Broadhead79c3f4a2014-08-09 18:21:44 +0100189 self.assertAlmostEquals(seconds, (end - start), places=1)