Roger Meier | 50e4349 | 2010-10-08 17:46:06 +0000 | [diff] [blame] | 1 | # |
| 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 | |
| 20 | import sys, glob, time |
| 21 | sys.path.insert(0, './gen-py.twisted') |
| 22 | sys.path.insert(0, glob.glob('../../lib/py/build/lib.*')[0]) |
| 23 | |
| 24 | from ThriftTest import ThriftTest |
| 25 | from ThriftTest.ttypes import * |
| 26 | from thrift.transport import TTwisted |
| 27 | from thrift.protocol import TBinaryProtocol |
| 28 | |
| 29 | from twisted.trial import unittest |
| 30 | from twisted.internet import defer, reactor |
| 31 | from twisted.internet.protocol import ClientCreator |
| 32 | |
| 33 | from zope.interface import implements |
| 34 | |
| 35 | import random |
| 36 | |
| 37 | class TestHandler: |
| 38 | implements(ThriftTest.Iface) |
| 39 | |
| 40 | def __init__(self): |
| 41 | self.onewaysQueue = defer.DeferredQueue() |
| 42 | |
| 43 | def testVoid(self): |
| 44 | pass |
| 45 | |
| 46 | def testString(self, s): |
| 47 | return s |
| 48 | |
| 49 | def testByte(self, b): |
| 50 | return b |
| 51 | |
| 52 | def testI16(self, i16): |
| 53 | return i16 |
| 54 | |
| 55 | def testI32(self, i32): |
| 56 | return i32 |
| 57 | |
| 58 | def testI64(self, i64): |
| 59 | return i64 |
| 60 | |
| 61 | def testDouble(self, dub): |
| 62 | return dub |
| 63 | |
| 64 | def testStruct(self, thing): |
| 65 | return thing |
| 66 | |
| 67 | def testException(self, s): |
| 68 | if s == 'Xception': |
| 69 | x = Xception() |
| 70 | x.errorCode = 1001 |
| 71 | x.message = s |
| 72 | raise x |
| 73 | elif s == "throw_undeclared": |
| 74 | raise ValueError("foo") |
| 75 | |
| 76 | def testOneway(self, seconds): |
| 77 | def fireOneway(t): |
| 78 | self.onewaysQueue.put((t, time.time(), seconds)) |
| 79 | reactor.callLater(seconds, fireOneway, time.time()) |
| 80 | return d |
| 81 | |
| 82 | def testNest(self, thing): |
| 83 | return thing |
| 84 | |
| 85 | def testMap(self, thing): |
| 86 | return thing |
| 87 | |
| 88 | def testSet(self, thing): |
| 89 | return thing |
| 90 | |
| 91 | def testList(self, thing): |
| 92 | return thing |
| 93 | |
| 94 | def testEnum(self, thing): |
| 95 | return thing |
| 96 | |
| 97 | def testTypedef(self, thing): |
| 98 | return thing |
| 99 | |
| 100 | class ThriftTestCase(unittest.TestCase): |
| 101 | |
| 102 | @defer.inlineCallbacks |
| 103 | def setUp(self): |
| 104 | self.handler = TestHandler() |
| 105 | self.processor = ThriftTest.Processor(self.handler) |
| 106 | self.pfactory = TBinaryProtocol.TBinaryProtocolFactory() |
| 107 | |
| 108 | self.server = reactor.listenTCP(0, |
| 109 | TTwisted.ThriftServerFactory(self.processor, |
| 110 | self.pfactory), interface="127.0.0.1") |
| 111 | |
| 112 | self.portNo = self.server.getHost().port |
| 113 | |
| 114 | self.txclient = yield ClientCreator(reactor, |
| 115 | TTwisted.ThriftClientProtocol, |
| 116 | ThriftTest.Client, |
| 117 | self.pfactory).connectTCP("127.0.0.1", self.portNo) |
| 118 | self.client = self.txclient.client |
| 119 | |
| 120 | @defer.inlineCallbacks |
| 121 | def tearDown(self): |
| 122 | yield self.server.stopListening() |
| 123 | self.txclient.transport.loseConnection() |
| 124 | |
| 125 | @defer.inlineCallbacks |
| 126 | def testVoid(self): |
| 127 | self.assertEquals((yield self.client.testVoid()), None) |
| 128 | |
| 129 | @defer.inlineCallbacks |
| 130 | def testString(self): |
| 131 | self.assertEquals((yield self.client.testString('Python')), 'Python') |
| 132 | |
| 133 | @defer.inlineCallbacks |
| 134 | def testByte(self): |
| 135 | self.assertEquals((yield self.client.testByte(63)), 63) |
| 136 | |
| 137 | @defer.inlineCallbacks |
| 138 | def testI32(self): |
| 139 | self.assertEquals((yield self.client.testI32(-1)), -1) |
| 140 | self.assertEquals((yield self.client.testI32(0)), 0) |
| 141 | |
| 142 | @defer.inlineCallbacks |
| 143 | def testI64(self): |
| 144 | self.assertEquals((yield self.client.testI64(-34359738368)), -34359738368) |
| 145 | |
| 146 | @defer.inlineCallbacks |
| 147 | def testDouble(self): |
| 148 | self.assertEquals((yield self.client.testDouble(-5.235098235)), -5.235098235) |
| 149 | |
| 150 | @defer.inlineCallbacks |
| 151 | def testStruct(self): |
| 152 | x = Xtruct() |
| 153 | x.string_thing = "Zero" |
| 154 | x.byte_thing = 1 |
| 155 | x.i32_thing = -3 |
| 156 | x.i64_thing = -5 |
| 157 | y = yield self.client.testStruct(x) |
| 158 | |
| 159 | self.assertEquals(y.string_thing, "Zero") |
| 160 | self.assertEquals(y.byte_thing, 1) |
| 161 | self.assertEquals(y.i32_thing, -3) |
| 162 | self.assertEquals(y.i64_thing, -5) |
| 163 | |
| 164 | @defer.inlineCallbacks |
| 165 | def testException(self): |
| 166 | yield self.client.testException('Safe') |
| 167 | try: |
| 168 | yield self.client.testException('Xception') |
| 169 | self.fail("should have gotten exception") |
| 170 | except Xception, x: |
| 171 | self.assertEquals(x.errorCode, 1001) |
| 172 | self.assertEquals(x.message, 'Xception') |
| 173 | |
| 174 | try: |
| 175 | yield self.client.testException("throw_undeclared") |
| 176 | self.fail("should have thrown exception") |
| 177 | except Exception: # type is undefined |
| 178 | pass |
| 179 | |
| 180 | @defer.inlineCallbacks |
| 181 | def testOneway(self): |
| 182 | yield self.client.testOneway(2) |
| 183 | start, end, seconds = yield self.handler.onewaysQueue.get() |
| 184 | self.assertAlmostEquals(seconds, (end - start), places=2) |