blob: 43149a4c9c0ed53e8f5d6d48adf7cf04f6ed3891 [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 sys
23import os
24import glob
25import time
jfarrell6cdbd982015-06-23 21:58:50 -040026basepath = os.path.abspath(os.path.dirname(__file__))
27sys.path.insert(0, os.path.join(basepath, 'gen-py.twisted'))
28sys.path.insert(0, glob.glob(os.path.join(basepath, '../../lib/py/build/lib.*'))[0])
Roger Meier50e43492010-10-08 17:46:06 +000029
30from ThriftTest import ThriftTest
James Broadhead79c3f4a2014-08-09 18:21:44 +010031from ThriftTest.ttypes import Xception, Xtruct
Roger Meier50e43492010-10-08 17:46:06 +000032from thrift.transport import TTwisted
33from thrift.protocol import TBinaryProtocol
34
35from twisted.trial import unittest
36from twisted.internet import defer, reactor
37from twisted.internet.protocol import ClientCreator
38
39from zope.interface import implements
40
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090041
Roger Meier50e43492010-10-08 17:46:06 +000042class TestHandler:
43 implements(ThriftTest.Iface)
44
45 def __init__(self):
46 self.onewaysQueue = defer.DeferredQueue()
47
48 def testVoid(self):
49 pass
James Broadhead79c3f4a2014-08-09 18:21:44 +010050
Roger Meier50e43492010-10-08 17:46:06 +000051 def testString(self, s):
52 return s
James Broadhead79c3f4a2014-08-09 18:21:44 +010053
Roger Meier50e43492010-10-08 17:46:06 +000054 def testByte(self, b):
55 return b
James Broadhead79c3f4a2014-08-09 18:21:44 +010056
Roger Meier50e43492010-10-08 17:46:06 +000057 def testI16(self, i16):
58 return i16
James Broadhead79c3f4a2014-08-09 18:21:44 +010059
Roger Meier50e43492010-10-08 17:46:06 +000060 def testI32(self, i32):
61 return i32
James Broadhead79c3f4a2014-08-09 18:21:44 +010062
Roger Meier50e43492010-10-08 17:46:06 +000063 def testI64(self, i64):
64 return i64
James Broadhead79c3f4a2014-08-09 18:21:44 +010065
Roger Meier50e43492010-10-08 17:46:06 +000066 def testDouble(self, dub):
67 return dub
James Broadhead79c3f4a2014-08-09 18:21:44 +010068
Jens Geyer8bcfdd92014-12-14 03:14:26 +010069 def testBinary(self, thing):
70 return thing
71
Roger Meier50e43492010-10-08 17:46:06 +000072 def testStruct(self, thing):
73 return thing
James Broadhead79c3f4a2014-08-09 18:21:44 +010074
Roger Meier50e43492010-10-08 17:46:06 +000075 def testException(self, s):
76 if s == 'Xception':
77 x = Xception()
78 x.errorCode = 1001
79 x.message = s
80 raise x
81 elif s == "throw_undeclared":
82 raise ValueError("foo")
James Broadhead79c3f4a2014-08-09 18:21:44 +010083
Roger Meier50e43492010-10-08 17:46:06 +000084 def testOneway(self, seconds):
85 def fireOneway(t):
86 self.onewaysQueue.put((t, time.time(), seconds))
87 reactor.callLater(seconds, fireOneway, time.time())
James Broadhead79c3f4a2014-08-09 18:21:44 +010088
Roger Meier50e43492010-10-08 17:46:06 +000089 def testNest(self, thing):
90 return thing
James Broadhead79c3f4a2014-08-09 18:21:44 +010091
Roger Meier50e43492010-10-08 17:46:06 +000092 def testMap(self, thing):
93 return thing
James Broadhead79c3f4a2014-08-09 18:21:44 +010094
Roger Meier50e43492010-10-08 17:46:06 +000095 def testSet(self, thing):
96 return thing
James Broadhead79c3f4a2014-08-09 18:21:44 +010097
Roger Meier50e43492010-10-08 17:46:06 +000098 def testList(self, thing):
99 return thing
James Broadhead79c3f4a2014-08-09 18:21:44 +0100100
Roger Meier50e43492010-10-08 17:46:06 +0000101 def testEnum(self, thing):
102 return thing
James Broadhead79c3f4a2014-08-09 18:21:44 +0100103
Roger Meier50e43492010-10-08 17:46:06 +0000104 def testTypedef(self, thing):
105 return thing
106
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900107
Roger Meier50e43492010-10-08 17:46:06 +0000108class ThriftTestCase(unittest.TestCase):
109
110 @defer.inlineCallbacks
111 def setUp(self):
112 self.handler = TestHandler()
113 self.processor = ThriftTest.Processor(self.handler)
114 self.pfactory = TBinaryProtocol.TBinaryProtocolFactory()
115
Nobuaki Sukegawad479e232016-02-28 11:28:19 +0900116 self.server = reactor.listenTCP(
117 0, TTwisted.ThriftServerFactory(self.processor, self.pfactory), interface="127.0.0.1")
Roger Meier50e43492010-10-08 17:46:06 +0000118
119 self.portNo = self.server.getHost().port
120
121 self.txclient = yield ClientCreator(reactor,
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900122 TTwisted.ThriftClientProtocol,
123 ThriftTest.Client,
124 self.pfactory).connectTCP("127.0.0.1", self.portNo)
Roger Meier50e43492010-10-08 17:46:06 +0000125 self.client = self.txclient.client
126
127 @defer.inlineCallbacks
128 def tearDown(self):
129 yield self.server.stopListening()
130 self.txclient.transport.loseConnection()
131
132 @defer.inlineCallbacks
133 def testVoid(self):
134 self.assertEquals((yield self.client.testVoid()), None)
135
136 @defer.inlineCallbacks
137 def testString(self):
138 self.assertEquals((yield self.client.testString('Python')), 'Python')
James Broadhead79c3f4a2014-08-09 18:21:44 +0100139
Roger Meier50e43492010-10-08 17:46:06 +0000140 @defer.inlineCallbacks
141 def testByte(self):
142 self.assertEquals((yield self.client.testByte(63)), 63)
143
144 @defer.inlineCallbacks
145 def testI32(self):
146 self.assertEquals((yield self.client.testI32(-1)), -1)
147 self.assertEquals((yield self.client.testI32(0)), 0)
148
149 @defer.inlineCallbacks
150 def testI64(self):
151 self.assertEquals((yield self.client.testI64(-34359738368)), -34359738368)
152
153 @defer.inlineCallbacks
154 def testDouble(self):
155 self.assertEquals((yield self.client.testDouble(-5.235098235)), -5.235098235)
156
jfarrell6cdbd982015-06-23 21:58:50 -0400157 # TODO: def testBinary(self) ...
158
James Broadhead79c3f4a2014-08-09 18:21:44 +0100159 @defer.inlineCallbacks
Roger Meier50e43492010-10-08 17:46:06 +0000160 def testStruct(self):
161 x = Xtruct()
162 x.string_thing = "Zero"
163 x.byte_thing = 1
164 x.i32_thing = -3
165 x.i64_thing = -5
166 y = yield self.client.testStruct(x)
James Broadhead79c3f4a2014-08-09 18:21:44 +0100167
Roger Meier50e43492010-10-08 17:46:06 +0000168 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)
James Broadhead79c3f4a2014-08-09 18:21:44 +0100172
Roger Meier50e43492010-10-08 17:46:06 +0000173 @defer.inlineCallbacks
174 def testException(self):
175 yield self.client.testException('Safe')
176 try:
177 yield self.client.testException('Xception')
178 self.fail("should have gotten exception")
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900179 except Xception as x:
Roger Meier50e43492010-10-08 17:46:06 +0000180 self.assertEquals(x.errorCode, 1001)
181 self.assertEquals(x.message, 'Xception')
182
183 try:
184 yield self.client.testException("throw_undeclared")
185 self.fail("should have thrown exception")
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900186 except Exception: # type is undefined
Roger Meier50e43492010-10-08 17:46:06 +0000187 pass
James Broadhead79c3f4a2014-08-09 18:21:44 +0100188
Roger Meier50e43492010-10-08 17:46:06 +0000189 @defer.inlineCallbacks
190 def testOneway(self):
James Broadhead79c3f4a2014-08-09 18:21:44 +0100191 yield self.client.testOneway(1)
Roger Meier50e43492010-10-08 17:46:06 +0000192 start, end, seconds = yield self.handler.onewaysQueue.get()
James Broadhead79c3f4a2014-08-09 18:21:44 +0100193 self.assertAlmostEquals(seconds, (end - start), places=1)