jfarrell | 6cdbd98 | 2015-06-23 21:58:50 -0400 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
Roger Meier | 50e4349 | 2010-10-08 17:46:06 +0000 | [diff] [blame] | 3 | # |
| 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 Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 22 | import glob |
Nobuaki Sukegawa | 6f82640 | 2016-02-10 19:42:19 +0900 | [diff] [blame] | 23 | import os |
| 24 | import sys |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 25 | import time |
Nobuaki Sukegawa | 6f82640 | 2016-02-10 19:42:19 +0900 | [diff] [blame] | 26 | |
jfarrell | 6cdbd98 | 2015-06-23 21:58:50 -0400 | [diff] [blame] | 27 | basepath = os.path.abspath(os.path.dirname(__file__)) |
| 28 | sys.path.insert(0, os.path.join(basepath, 'gen-py.twisted')) |
| 29 | sys.path.insert(0, glob.glob(os.path.join(basepath, '../../lib/py/build/lib.*'))[0]) |
Roger Meier | 50e4349 | 2010-10-08 17:46:06 +0000 | [diff] [blame] | 30 | |
Nobuaki Sukegawa | 6f82640 | 2016-02-10 19:42:19 +0900 | [diff] [blame] | 31 | from thrift.Thrift import TApplicationException |
| 32 | |
Roger Meier | 50e4349 | 2010-10-08 17:46:06 +0000 | [diff] [blame] | 33 | from ThriftTest import ThriftTest |
James Broadhead | 79c3f4a | 2014-08-09 18:21:44 +0100 | [diff] [blame] | 34 | from ThriftTest.ttypes import Xception, Xtruct |
Roger Meier | 50e4349 | 2010-10-08 17:46:06 +0000 | [diff] [blame] | 35 | from thrift.transport import TTwisted |
| 36 | from thrift.protocol import TBinaryProtocol |
| 37 | |
| 38 | from twisted.trial import unittest |
| 39 | from twisted.internet import defer, reactor |
| 40 | from twisted.internet.protocol import ClientCreator |
| 41 | |
James E. King, III | d7142b7 | 2017-09-01 13:00:36 -0700 | [diff] [blame] | 42 | from zope.interface import implementer |
Roger Meier | 50e4349 | 2010-10-08 17:46:06 +0000 | [diff] [blame] | 43 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 44 | |
James E. King, III | d7142b7 | 2017-09-01 13:00:36 -0700 | [diff] [blame] | 45 | @implementer(ThriftTest.Iface) |
Roger Meier | 50e4349 | 2010-10-08 17:46:06 +0000 | [diff] [blame] | 46 | class TestHandler: |
Roger Meier | 50e4349 | 2010-10-08 17:46:06 +0000 | [diff] [blame] | 47 | def __init__(self): |
| 48 | self.onewaysQueue = defer.DeferredQueue() |
| 49 | |
| 50 | def testVoid(self): |
| 51 | pass |
James Broadhead | 79c3f4a | 2014-08-09 18:21:44 +0100 | [diff] [blame] | 52 | |
Roger Meier | 50e4349 | 2010-10-08 17:46:06 +0000 | [diff] [blame] | 53 | def testString(self, s): |
| 54 | return s |
James Broadhead | 79c3f4a | 2014-08-09 18:21:44 +0100 | [diff] [blame] | 55 | |
Roger Meier | 50e4349 | 2010-10-08 17:46:06 +0000 | [diff] [blame] | 56 | def testByte(self, b): |
| 57 | return b |
James Broadhead | 79c3f4a | 2014-08-09 18:21:44 +0100 | [diff] [blame] | 58 | |
Roger Meier | 50e4349 | 2010-10-08 17:46:06 +0000 | [diff] [blame] | 59 | def testI16(self, i16): |
| 60 | return i16 |
James Broadhead | 79c3f4a | 2014-08-09 18:21:44 +0100 | [diff] [blame] | 61 | |
Roger Meier | 50e4349 | 2010-10-08 17:46:06 +0000 | [diff] [blame] | 62 | def testI32(self, i32): |
| 63 | return i32 |
James Broadhead | 79c3f4a | 2014-08-09 18:21:44 +0100 | [diff] [blame] | 64 | |
Roger Meier | 50e4349 | 2010-10-08 17:46:06 +0000 | [diff] [blame] | 65 | def testI64(self, i64): |
| 66 | return i64 |
James Broadhead | 79c3f4a | 2014-08-09 18:21:44 +0100 | [diff] [blame] | 67 | |
Roger Meier | 50e4349 | 2010-10-08 17:46:06 +0000 | [diff] [blame] | 68 | def testDouble(self, dub): |
| 69 | return dub |
James Broadhead | 79c3f4a | 2014-08-09 18:21:44 +0100 | [diff] [blame] | 70 | |
Jens Geyer | 8bcfdd9 | 2014-12-14 03:14:26 +0100 | [diff] [blame] | 71 | def testBinary(self, thing): |
| 72 | return thing |
| 73 | |
Roger Meier | 50e4349 | 2010-10-08 17:46:06 +0000 | [diff] [blame] | 74 | def testStruct(self, thing): |
| 75 | return thing |
James Broadhead | 79c3f4a | 2014-08-09 18:21:44 +0100 | [diff] [blame] | 76 | |
Roger Meier | 50e4349 | 2010-10-08 17:46:06 +0000 | [diff] [blame] | 77 | def testException(self, s): |
| 78 | if s == 'Xception': |
Elvis Pranskevichus | 9c43962 | 2019-12-11 16:47:52 -0500 | [diff] [blame] | 79 | raise Xception(1001, s) |
Roger Meier | 50e4349 | 2010-10-08 17:46:06 +0000 | [diff] [blame] | 80 | elif s == "throw_undeclared": |
| 81 | raise ValueError("foo") |
James Broadhead | 79c3f4a | 2014-08-09 18:21:44 +0100 | [diff] [blame] | 82 | |
Roger Meier | 50e4349 | 2010-10-08 17:46:06 +0000 | [diff] [blame] | 83 | def testOneway(self, seconds): |
| 84 | def fireOneway(t): |
| 85 | self.onewaysQueue.put((t, time.time(), seconds)) |
| 86 | reactor.callLater(seconds, fireOneway, time.time()) |
Nobuaki Sukegawa | 6f82640 | 2016-02-10 19:42:19 +0900 | [diff] [blame] | 87 | raise Exception('') |
James Broadhead | 79c3f4a | 2014-08-09 18:21:44 +0100 | [diff] [blame] | 88 | |
Roger Meier | 50e4349 | 2010-10-08 17:46:06 +0000 | [diff] [blame] | 89 | def testNest(self, thing): |
| 90 | return thing |
James Broadhead | 79c3f4a | 2014-08-09 18:21:44 +0100 | [diff] [blame] | 91 | |
Roger Meier | 50e4349 | 2010-10-08 17:46:06 +0000 | [diff] [blame] | 92 | def testMap(self, thing): |
| 93 | return thing |
James Broadhead | 79c3f4a | 2014-08-09 18:21:44 +0100 | [diff] [blame] | 94 | |
Roger Meier | 50e4349 | 2010-10-08 17:46:06 +0000 | [diff] [blame] | 95 | def testSet(self, thing): |
| 96 | return thing |
James Broadhead | 79c3f4a | 2014-08-09 18:21:44 +0100 | [diff] [blame] | 97 | |
Roger Meier | 50e4349 | 2010-10-08 17:46:06 +0000 | [diff] [blame] | 98 | def testList(self, thing): |
| 99 | return thing |
James Broadhead | 79c3f4a | 2014-08-09 18:21:44 +0100 | [diff] [blame] | 100 | |
Roger Meier | 50e4349 | 2010-10-08 17:46:06 +0000 | [diff] [blame] | 101 | def testEnum(self, thing): |
| 102 | return thing |
James Broadhead | 79c3f4a | 2014-08-09 18:21:44 +0100 | [diff] [blame] | 103 | |
Roger Meier | 50e4349 | 2010-10-08 17:46:06 +0000 | [diff] [blame] | 104 | def testTypedef(self, thing): |
| 105 | return thing |
| 106 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 107 | |
Roger Meier | 50e4349 | 2010-10-08 17:46:06 +0000 | [diff] [blame] | 108 | class 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 Sukegawa | d479e23 | 2016-02-28 11:28:19 +0900 | [diff] [blame] | 116 | self.server = reactor.listenTCP( |
| 117 | 0, TTwisted.ThriftServerFactory(self.processor, self.pfactory), interface="127.0.0.1") |
Roger Meier | 50e4349 | 2010-10-08 17:46:06 +0000 | [diff] [blame] | 118 | |
| 119 | self.portNo = self.server.getHost().port |
| 120 | |
| 121 | self.txclient = yield ClientCreator(reactor, |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 122 | TTwisted.ThriftClientProtocol, |
| 123 | ThriftTest.Client, |
| 124 | self.pfactory).connectTCP("127.0.0.1", self.portNo) |
Roger Meier | 50e4349 | 2010-10-08 17:46:06 +0000 | [diff] [blame] | 125 | 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 Broadhead | 79c3f4a | 2014-08-09 18:21:44 +0100 | [diff] [blame] | 139 | |
Roger Meier | 50e4349 | 2010-10-08 17:46:06 +0000 | [diff] [blame] | 140 | @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 | |
jfarrell | 6cdbd98 | 2015-06-23 21:58:50 -0400 | [diff] [blame] | 157 | # TODO: def testBinary(self) ... |
| 158 | |
James Broadhead | 79c3f4a | 2014-08-09 18:21:44 +0100 | [diff] [blame] | 159 | @defer.inlineCallbacks |
Roger Meier | 50e4349 | 2010-10-08 17:46:06 +0000 | [diff] [blame] | 160 | 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 Broadhead | 79c3f4a | 2014-08-09 18:21:44 +0100 | [diff] [blame] | 167 | |
Roger Meier | 50e4349 | 2010-10-08 17:46:06 +0000 | [diff] [blame] | 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) |
James Broadhead | 79c3f4a | 2014-08-09 18:21:44 +0100 | [diff] [blame] | 172 | |
Roger Meier | 50e4349 | 2010-10-08 17:46:06 +0000 | [diff] [blame] | 173 | @defer.inlineCallbacks |
| 174 | def testException(self): |
Roger Meier | 50e4349 | 2010-10-08 17:46:06 +0000 | [diff] [blame] | 175 | try: |
| 176 | yield self.client.testException('Xception') |
| 177 | self.fail("should have gotten exception") |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 178 | except Xception as x: |
Roger Meier | 50e4349 | 2010-10-08 17:46:06 +0000 | [diff] [blame] | 179 | self.assertEquals(x.errorCode, 1001) |
| 180 | self.assertEquals(x.message, 'Xception') |
| 181 | |
| 182 | try: |
| 183 | yield self.client.testException("throw_undeclared") |
Nobuaki Sukegawa | 6f82640 | 2016-02-10 19:42:19 +0900 | [diff] [blame] | 184 | self.fail("should have gotten exception") |
| 185 | except TApplicationException: |
Roger Meier | 50e4349 | 2010-10-08 17:46:06 +0000 | [diff] [blame] | 186 | pass |
James Broadhead | 79c3f4a | 2014-08-09 18:21:44 +0100 | [diff] [blame] | 187 | |
Nobuaki Sukegawa | 6f82640 | 2016-02-10 19:42:19 +0900 | [diff] [blame] | 188 | yield self.client.testException('Safe') |
| 189 | |
Roger Meier | 50e4349 | 2010-10-08 17:46:06 +0000 | [diff] [blame] | 190 | @defer.inlineCallbacks |
| 191 | def testOneway(self): |
James Broadhead | 79c3f4a | 2014-08-09 18:21:44 +0100 | [diff] [blame] | 192 | yield self.client.testOneway(1) |
Roger Meier | 50e4349 | 2010-10-08 17:46:06 +0000 | [diff] [blame] | 193 | start, end, seconds = yield self.handler.onewaysQueue.get() |
James Broadhead | 79c3f4a | 2014-08-09 18:21:44 +0100 | [diff] [blame] | 194 | self.assertAlmostEquals(seconds, (end - start), places=1) |
Nobuaki Sukegawa | 6f82640 | 2016-02-10 19:42:19 +0900 | [diff] [blame] | 195 | self.assertEquals((yield self.client.testI32(-1)), -1) |