You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@thrift.apache.org by ro...@apache.org on 2010/10/08 19:46:07 UTC

svn commit: r1005923 - in /incubator/thrift/trunk: configure.ac test/Makefile.am test/ThriftTest.thrift test/py.twisted/ test/py.twisted/Makefile.am test/py.twisted/test_suite.py

Author: roger
Date: Fri Oct  8 17:46:06 2010
New Revision: 1005923

URL: http://svn.apache.org/viewvc?rev=1005923&view=rev
Log:
THRIFT-581 Add a testsuite for txThrift (Twisted) / py.twisted

Patch: Esteve Fernandez


Added:
    incubator/thrift/trunk/test/py.twisted/
    incubator/thrift/trunk/test/py.twisted/Makefile.am
    incubator/thrift/trunk/test/py.twisted/test_suite.py
Modified:
    incubator/thrift/trunk/configure.ac
    incubator/thrift/trunk/test/Makefile.am
    incubator/thrift/trunk/test/ThriftTest.thrift

Modified: incubator/thrift/trunk/configure.ac
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/configure.ac?rev=1005923&r1=1005922&r2=1005923&view=diff
==============================================================================
--- incubator/thrift/trunk/configure.ac (original)
+++ incubator/thrift/trunk/configure.ac Fri Oct  8 17:46:06 2010
@@ -156,6 +156,8 @@ if test "$with_python" = "yes";  then
 fi
 AM_CONDITIONAL(WITH_PYTHON, [test "$have_python" = "yes"])
 
+AC_PATH_PROG([TRIAL], [trial])
+
 AX_THRIFT_LIB(perl, [Perl], yes)
 if test "$with_perl" = "yes"; then
   AC_PATH_PROG([PERL], [perl])
@@ -379,6 +381,7 @@ AC_CONFIG_FILES([
   test/Makefile
   test/hs/Makefile
   test/py/Makefile
+  test/py.twisted/Makefile
   test/rb/Makefile
 ])
 

Modified: incubator/thrift/trunk/test/Makefile.am
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/test/Makefile.am?rev=1005923&r1=1005922&r2=1005923&view=diff
==============================================================================
--- incubator/thrift/trunk/test/Makefile.am (original)
+++ incubator/thrift/trunk/test/Makefile.am Fri Oct  8 17:46:06 2010
@@ -21,6 +21,7 @@ SUBDIRS =
 
 if WITH_PYTHON
 SUBDIRS += py
+SUBDIRS += py.twisted
 endif
 
 if WITH_RUBY
@@ -36,6 +37,7 @@ EXTRA_DIST = \
 	threads \
 	csharp \
 	py \
+	py.twisted \
 	rb \
 	perl \
 	php \

Modified: incubator/thrift/trunk/test/ThriftTest.thrift
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/test/ThriftTest.thrift?rev=1005923&r1=1005922&r2=1005923&view=diff
==============================================================================
--- incubator/thrift/trunk/test/ThriftTest.thrift (original)
+++ incubator/thrift/trunk/test/ThriftTest.thrift Fri Oct  8 17:46:06 2010
@@ -29,6 +29,7 @@ namespace csharp Thrift.Test
 namespace js ThriftTest
 namespace st ThriftTest
 namespace py ThriftTest
+namespace py.twisted ThriftTest
 namespace * thrift.test
 
 /**

Added: incubator/thrift/trunk/test/py.twisted/Makefile.am
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/test/py.twisted/Makefile.am?rev=1005923&view=auto
==============================================================================
--- incubator/thrift/trunk/test/py.twisted/Makefile.am (added)
+++ incubator/thrift/trunk/test/py.twisted/Makefile.am Fri Oct  8 17:46:06 2010
@@ -0,0 +1,30 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+#
+
+THRIFT = $(top_srcdir)/compiler/cpp/thrift
+
+stubs: ../ThriftTest.thrift ../SmallTest.thrift
+	$(THRIFT) --gen py:twisted ../ThriftTest.thrift
+	$(THRIFT) --gen py:twisted ../SmallTest.thrift
+
+check: stubs
+	$(TRIAL) test_suite.py
+
+clean-local:
+	$(RM) -r gen-py.twisted

Added: incubator/thrift/trunk/test/py.twisted/test_suite.py
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/test/py.twisted/test_suite.py?rev=1005923&view=auto
==============================================================================
--- incubator/thrift/trunk/test/py.twisted/test_suite.py (added)
+++ incubator/thrift/trunk/test/py.twisted/test_suite.py Fri Oct  8 17:46:06 2010
@@ -0,0 +1,184 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+#
+
+import sys, glob, time
+sys.path.insert(0, './gen-py.twisted')
+sys.path.insert(0, glob.glob('../../lib/py/build/lib.*')[0])
+
+from ThriftTest import ThriftTest
+from ThriftTest.ttypes import *
+from thrift.transport import TTwisted
+from thrift.protocol import TBinaryProtocol
+
+from twisted.trial import unittest
+from twisted.internet import defer, reactor
+from twisted.internet.protocol import ClientCreator
+
+from zope.interface import implements
+
+import random
+
+class TestHandler:
+    implements(ThriftTest.Iface)
+
+    def __init__(self):
+        self.onewaysQueue = defer.DeferredQueue()
+
+    def testVoid(self):
+        pass
+ 
+    def testString(self, s):
+        return s
+ 
+    def testByte(self, b):
+        return b
+ 
+    def testI16(self, i16):
+        return i16
+ 
+    def testI32(self, i32):
+        return i32
+ 
+    def testI64(self, i64):
+        return i64
+ 
+    def testDouble(self, dub):
+        return dub
+ 
+    def testStruct(self, thing):
+        return thing
+ 
+    def testException(self, s):
+        if s == 'Xception':
+            x = Xception()
+            x.errorCode = 1001
+            x.message = s
+            raise x
+        elif s == "throw_undeclared":
+            raise ValueError("foo")
+ 
+    def testOneway(self, seconds):
+        def fireOneway(t):
+            self.onewaysQueue.put((t, time.time(), seconds))
+        reactor.callLater(seconds, fireOneway, time.time())
+        return d
+ 
+    def testNest(self, thing):
+        return thing
+ 
+    def testMap(self, thing):
+        return thing
+ 
+    def testSet(self, thing):
+        return thing
+ 
+    def testList(self, thing):
+        return thing
+ 
+    def testEnum(self, thing):
+        return thing
+ 
+    def testTypedef(self, thing):
+        return thing
+
+class ThriftTestCase(unittest.TestCase):
+
+    @defer.inlineCallbacks
+    def setUp(self):
+        self.handler = TestHandler()
+        self.processor = ThriftTest.Processor(self.handler)
+        self.pfactory = TBinaryProtocol.TBinaryProtocolFactory()
+
+        self.server = reactor.listenTCP(0,
+            TTwisted.ThriftServerFactory(self.processor,
+            self.pfactory), interface="127.0.0.1")
+
+        self.portNo = self.server.getHost().port
+
+        self.txclient = yield ClientCreator(reactor,
+            TTwisted.ThriftClientProtocol,
+            ThriftTest.Client,
+            self.pfactory).connectTCP("127.0.0.1", self.portNo)
+        self.client = self.txclient.client
+
+    @defer.inlineCallbacks
+    def tearDown(self):
+        yield self.server.stopListening()
+        self.txclient.transport.loseConnection()
+
+    @defer.inlineCallbacks
+    def testVoid(self):
+        self.assertEquals((yield self.client.testVoid()), None)
+
+    @defer.inlineCallbacks
+    def testString(self):
+        self.assertEquals((yield self.client.testString('Python')), 'Python')
+ 
+    @defer.inlineCallbacks
+    def testByte(self):
+        self.assertEquals((yield self.client.testByte(63)), 63)
+
+    @defer.inlineCallbacks
+    def testI32(self):
+        self.assertEquals((yield self.client.testI32(-1)), -1)
+        self.assertEquals((yield self.client.testI32(0)), 0)
+
+    @defer.inlineCallbacks
+    def testI64(self):
+        self.assertEquals((yield self.client.testI64(-34359738368)), -34359738368)
+
+    @defer.inlineCallbacks
+    def testDouble(self):
+        self.assertEquals((yield self.client.testDouble(-5.235098235)), -5.235098235)
+
+    @defer.inlineCallbacks 
+    def testStruct(self):
+        x = Xtruct()
+        x.string_thing = "Zero"
+        x.byte_thing = 1
+        x.i32_thing = -3
+        x.i64_thing = -5
+        y = yield self.client.testStruct(x)
+ 
+        self.assertEquals(y.string_thing, "Zero")
+        self.assertEquals(y.byte_thing, 1)
+        self.assertEquals(y.i32_thing, -3)
+        self.assertEquals(y.i64_thing, -5)
+ 
+    @defer.inlineCallbacks
+    def testException(self):
+        yield self.client.testException('Safe')
+        try:
+            yield self.client.testException('Xception')
+            self.fail("should have gotten exception")
+        except Xception, x:
+            self.assertEquals(x.errorCode, 1001)
+            self.assertEquals(x.message, 'Xception')
+
+        try:
+            yield self.client.testException("throw_undeclared")
+            self.fail("should have thrown exception")
+        except Exception: # type is undefined
+            pass
+ 
+    @defer.inlineCallbacks
+    def testOneway(self):
+        yield self.client.testOneway(2)
+        start, end, seconds = yield self.handler.onewaysQueue.get()
+        self.assertAlmostEquals(seconds, (end - start), places=2)