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 | |
jfarrell | 6cdbd98 | 2015-06-23 21:58:50 -0400 | [diff] [blame] | 22 | import sys, os, glob, time |
| 23 | basepath = os.path.abspath(os.path.dirname(__file__)) |
| 24 | sys.path.insert(0, os.path.join(basepath, 'gen-py.twisted')) |
| 25 | 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] | 26 | |
| 27 | from ThriftTest import ThriftTest |
James Broadhead | 79c3f4a | 2014-08-09 18:21:44 +0100 | [diff] [blame] | 28 | from ThriftTest.ttypes import Xception, Xtruct |
Roger Meier | 50e4349 | 2010-10-08 17:46:06 +0000 | [diff] [blame] | 29 | from thrift.transport import TTwisted |
| 30 | from thrift.protocol import TBinaryProtocol |
| 31 | |
| 32 | from twisted.trial import unittest |
| 33 | from twisted.internet import defer, reactor |
| 34 | from twisted.internet.protocol import ClientCreator |
| 35 | |
| 36 | from zope.interface import implements |
| 37 | |
Roger Meier | 50e4349 | 2010-10-08 17:46:06 +0000 | [diff] [blame] | 38 | class TestHandler: |
| 39 | implements(ThriftTest.Iface) |
| 40 | |
| 41 | def __init__(self): |
| 42 | self.onewaysQueue = defer.DeferredQueue() |
| 43 | |
| 44 | def testVoid(self): |
| 45 | pass |
James Broadhead | 79c3f4a | 2014-08-09 18:21:44 +0100 | [diff] [blame] | 46 | |
Roger Meier | 50e4349 | 2010-10-08 17:46:06 +0000 | [diff] [blame] | 47 | def testString(self, s): |
| 48 | return s |
James Broadhead | 79c3f4a | 2014-08-09 18:21:44 +0100 | [diff] [blame] | 49 | |
Roger Meier | 50e4349 | 2010-10-08 17:46:06 +0000 | [diff] [blame] | 50 | def testByte(self, b): |
| 51 | return b |
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 testI16(self, i16): |
| 54 | return i16 |
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 testI32(self, i32): |
| 57 | return i32 |
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 testI64(self, i64): |
| 60 | return i64 |
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 testDouble(self, dub): |
| 63 | return dub |
James Broadhead | 79c3f4a | 2014-08-09 18:21:44 +0100 | [diff] [blame] | 64 | |
Jens Geyer | 8bcfdd9 | 2014-12-14 03:14:26 +0100 | [diff] [blame] | 65 | def testBinary(self, thing): |
| 66 | return thing |
| 67 | |
Roger Meier | 50e4349 | 2010-10-08 17:46:06 +0000 | [diff] [blame] | 68 | def testStruct(self, thing): |
| 69 | return thing |
James Broadhead | 79c3f4a | 2014-08-09 18:21:44 +0100 | [diff] [blame] | 70 | |
Roger Meier | 50e4349 | 2010-10-08 17:46:06 +0000 | [diff] [blame] | 71 | 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 Broadhead | 79c3f4a | 2014-08-09 18:21:44 +0100 | [diff] [blame] | 79 | |
Roger Meier | 50e4349 | 2010-10-08 17:46:06 +0000 | [diff] [blame] | 80 | def testOneway(self, seconds): |
| 81 | def fireOneway(t): |
| 82 | self.onewaysQueue.put((t, time.time(), seconds)) |
| 83 | reactor.callLater(seconds, fireOneway, time.time()) |
James Broadhead | 79c3f4a | 2014-08-09 18:21:44 +0100 | [diff] [blame] | 84 | |
Roger Meier | 50e4349 | 2010-10-08 17:46:06 +0000 | [diff] [blame] | 85 | def testNest(self, thing): |
| 86 | return thing |
James Broadhead | 79c3f4a | 2014-08-09 18:21:44 +0100 | [diff] [blame] | 87 | |
Roger Meier | 50e4349 | 2010-10-08 17:46:06 +0000 | [diff] [blame] | 88 | def testMap(self, thing): |
| 89 | return thing |
James Broadhead | 79c3f4a | 2014-08-09 18:21:44 +0100 | [diff] [blame] | 90 | |
Roger Meier | 50e4349 | 2010-10-08 17:46:06 +0000 | [diff] [blame] | 91 | def testSet(self, thing): |
| 92 | return thing |
James Broadhead | 79c3f4a | 2014-08-09 18:21:44 +0100 | [diff] [blame] | 93 | |
Roger Meier | 50e4349 | 2010-10-08 17:46:06 +0000 | [diff] [blame] | 94 | def testList(self, thing): |
| 95 | return thing |
James Broadhead | 79c3f4a | 2014-08-09 18:21:44 +0100 | [diff] [blame] | 96 | |
Roger Meier | 50e4349 | 2010-10-08 17:46:06 +0000 | [diff] [blame] | 97 | def testEnum(self, thing): |
| 98 | return thing |
James Broadhead | 79c3f4a | 2014-08-09 18:21:44 +0100 | [diff] [blame] | 99 | |
Roger Meier | 50e4349 | 2010-10-08 17:46:06 +0000 | [diff] [blame] | 100 | def testTypedef(self, thing): |
| 101 | return thing |
| 102 | |
| 103 | class 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 Broadhead | 79c3f4a | 2014-08-09 18:21:44 +0100 | [diff] [blame] | 135 | |
Roger Meier | 50e4349 | 2010-10-08 17:46:06 +0000 | [diff] [blame] | 136 | @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 | |
jfarrell | 6cdbd98 | 2015-06-23 21:58:50 -0400 | [diff] [blame] | 153 | # TODO: def testBinary(self) ... |
| 154 | |
James Broadhead | 79c3f4a | 2014-08-09 18:21:44 +0100 | [diff] [blame] | 155 | @defer.inlineCallbacks |
Roger Meier | 50e4349 | 2010-10-08 17:46:06 +0000 | [diff] [blame] | 156 | 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 Broadhead | 79c3f4a | 2014-08-09 18:21:44 +0100 | [diff] [blame] | 163 | |
Roger Meier | 50e4349 | 2010-10-08 17:46:06 +0000 | [diff] [blame] | 164 | 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 Broadhead | 79c3f4a | 2014-08-09 18:21:44 +0100 | [diff] [blame] | 168 | |
Roger Meier | 50e4349 | 2010-10-08 17:46:06 +0000 | [diff] [blame] | 169 | @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 Broadhead | 79c3f4a | 2014-08-09 18:21:44 +0100 | [diff] [blame] | 184 | |
Roger Meier | 50e4349 | 2010-10-08 17:46:06 +0000 | [diff] [blame] | 185 | @defer.inlineCallbacks |
| 186 | def testOneway(self): |
James Broadhead | 79c3f4a | 2014-08-09 18:21:44 +0100 | [diff] [blame] | 187 | yield self.client.testOneway(1) |
Roger Meier | 50e4349 | 2010-10-08 17:46:06 +0000 | [diff] [blame] | 188 | start, end, seconds = yield self.handler.onewaysQueue.get() |
James Broadhead | 79c3f4a | 2014-08-09 18:21:44 +0100 | [diff] [blame] | 189 | self.assertAlmostEquals(seconds, (end - start), places=1) |