Thrift now a TLP - INFRA-3116

git-svn-id: https://svn.apache.org/repos/asf/thrift/branches/0.1.x@1028168 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/test/py/Makefile.am b/test/py/Makefile.am
new file mode 100644
index 0000000..63b7a89
--- /dev/null
+++ b/test/py/Makefile.am
@@ -0,0 +1,48 @@
+#
+# 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
+
+py_unit_tests =                                 \
+        SerializationTest.py                    \
+        TestEof.py                              \
+        TestSyntax.py                           \
+        RunClientServer.py
+
+thrift_gen =                                    \
+        gen-py/ThriftTest/__init__.py           \
+        gen-py/DebugProtoTest/__init__.py
+
+helper_scripts=                                 \
+        TestClient.py                           \
+        TestServer.py
+
+check_SCRIPTS=                                  \
+        $(thrift_gen)                           \
+        $(py_unit_tests)                        \
+        $(helper_scripts)
+
+TESTS= $(py_unit_tests)
+
+
+gen-py/%/__init__.py: ../%.thrift
+	$(THRIFT) --gen py $<
+
+clean-local:
+	$(RM) -r gen-py
diff --git a/test/py/RunClientServer.py b/test/py/RunClientServer.py
new file mode 100755
index 0000000..2bd6094
--- /dev/null
+++ b/test/py/RunClientServer.py
@@ -0,0 +1,60 @@
+#!/usr/bin/env python
+
+#
+# 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 time
+import subprocess
+import sys
+import os
+import signal
+
+def relfile(fname):
+    return os.path.join(os.path.dirname(__file__), fname)
+
+FRAMED = ["TNonblockingServer"]
+
+def runTest(server_class):
+    print "Testing ", server_class
+    serverproc = subprocess.Popen([sys.executable, relfile("TestServer.py"), server_class])
+    time.sleep(0.25)
+    try:
+        argv = [sys.executable, relfile("TestClient.py")]
+        if server_class in FRAMED:
+            argv.append('--framed')
+        if server_class == 'THttpServer':
+            argv.append('--http=/')
+        ret = subprocess.call(argv)
+        if ret != 0:
+            raise Exception("subprocess failed")
+    finally:
+        # fixme: should check that server didn't die
+        os.kill(serverproc.pid, signal.SIGKILL)
+
+    # wait for shutdown
+    time.sleep(1)
+
+map(runTest, [
+  "TSimpleServer",
+  "TThreadedServer",
+  "TThreadPoolServer",
+  "TForkingServer",
+  "TNonblockingServer",
+  "THttpServer",
+  ])
diff --git a/test/py/SerializationTest.py b/test/py/SerializationTest.py
new file mode 100755
index 0000000..52bedd5
--- /dev/null
+++ b/test/py/SerializationTest.py
@@ -0,0 +1,133 @@
+#!/usr/bin/env python
+
+#
+# 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
+sys.path.insert(0, './gen-py')
+sys.path.insert(0, glob.glob('../../lib/py/build/lib.*')[0])
+
+from ThriftTest.ttypes import *
+from thrift.transport import TTransport
+from thrift.transport import TSocket
+from thrift.protocol import TBinaryProtocol
+import unittest
+import time
+
+class AbstractTest(unittest.TestCase):
+
+  def setUp(self):
+      self.v1obj = VersioningTestV1(
+          begin_in_both=12345,
+          old_string='aaa',
+          end_in_both=54321,
+          )
+
+      self.v2obj = VersioningTestV2(
+          begin_in_both=12345,
+          newint=1,
+          newbyte=2,
+          newshort=3,
+          newlong=4,
+          newdouble=5.0,
+          newstruct=Bonk(message="Hello!", type=123),
+          newlist=[7,8,9],
+          newset=[42,1,8],
+          newmap={1:2,2:3},
+          newstring="Hola!",
+          end_in_both=54321,
+          )
+
+  def _serialize(self, obj):
+      trans = TTransport.TMemoryBuffer()
+      prot = self.protocol_factory.getProtocol(trans)
+      obj.write(prot)
+      return trans.getvalue()
+
+  def _deserialize(self, objtype, data):
+      prot = self.protocol_factory.getProtocol(TTransport.TMemoryBuffer(data))
+      ret = objtype()
+      ret.read(prot)
+      return ret
+
+  def testForwards(self):
+      obj = self._deserialize(VersioningTestV2, self._serialize(self.v1obj))
+      self.assertEquals(obj.begin_in_both, self.v1obj.begin_in_both)
+      self.assertEquals(obj.end_in_both, self.v1obj.end_in_both)
+
+  def testBackwards(self):
+      obj = self._deserialize(VersioningTestV1, self._serialize(self.v2obj))
+      self.assertEquals(obj.begin_in_both, self.v2obj.begin_in_both)
+      self.assertEquals(obj.end_in_both, self.v2obj.end_in_both)
+
+
+class NormalBinaryTest(AbstractTest):
+  protocol_factory = TBinaryProtocol.TBinaryProtocolFactory()
+
+class AcceleratedBinaryTest(AbstractTest):
+  protocol_factory = TBinaryProtocol.TBinaryProtocolAcceleratedFactory()
+
+
+class AcceleratedFramedTest(unittest.TestCase):
+  def testSplit(self):
+    """Test FramedTransport and BinaryProtocolAccelerated
+
+    Tests that TBinaryProtocolAccelerated and TFramedTransport
+    play nicely together when a read spans a frame"""
+
+    protocol_factory = TBinaryProtocol.TBinaryProtocolAcceleratedFactory()
+    bigstring = "".join(chr(byte) for byte in range(ord("a"), ord("z")+1))
+
+    databuf = TTransport.TMemoryBuffer()
+    prot = protocol_factory.getProtocol(databuf)
+    prot.writeI32(42)
+    prot.writeString(bigstring)
+    prot.writeI16(24)
+    data = databuf.getvalue()
+    cutpoint = len(data)/2
+    parts = [ data[:cutpoint], data[cutpoint:] ]
+
+    framed_buffer = TTransport.TMemoryBuffer()
+    framed_writer = TTransport.TFramedTransport(framed_buffer)
+    for part in parts:
+      framed_writer.write(part)
+      framed_writer.flush()
+    self.assertEquals(len(framed_buffer.getvalue()), len(data) + 8)
+
+    # Recreate framed_buffer so we can read from it.
+    framed_buffer = TTransport.TMemoryBuffer(framed_buffer.getvalue())
+    framed_reader = TTransport.TFramedTransport(framed_buffer)
+    prot = protocol_factory.getProtocol(framed_reader)
+    self.assertEqual(prot.readI32(), 42)
+    self.assertEqual(prot.readString(), bigstring)
+    self.assertEqual(prot.readI16(), 24)
+
+
+
+def suite():
+  suite = unittest.TestSuite()
+  loader = unittest.TestLoader()
+
+  suite.addTest(loader.loadTestsFromTestCase(NormalBinaryTest))
+  suite.addTest(loader.loadTestsFromTestCase(AcceleratedBinaryTest))
+  suite.addTest(loader.loadTestsFromTestCase(AcceleratedFramedTest))
+  return suite
+
+if __name__ == "__main__":
+  unittest.main(defaultTest="suite", testRunner=unittest.TextTestRunner(verbosity=2))
diff --git a/test/py/TestClient.py b/test/py/TestClient.py
new file mode 100755
index 0000000..64e5e87
--- /dev/null
+++ b/test/py/TestClient.py
@@ -0,0 +1,156 @@
+#!/usr/bin/env python
+
+#
+# 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
+sys.path.insert(0, './gen-py')
+sys.path.insert(0, glob.glob('../../lib/py/build/lib.*')[0])
+
+from ThriftTest import ThriftTest
+from ThriftTest.ttypes import *
+from thrift.transport import TTransport
+from thrift.transport import TSocket
+from thrift.transport import THttpClient
+from thrift.protocol import TBinaryProtocol
+import unittest
+import time
+from optparse import OptionParser
+
+
+parser = OptionParser()
+parser.set_defaults(framed=False, http_path=None, verbose=1, host='localhost', port=9090)
+parser.add_option("--port", type="int", dest="port",
+    help="connect to server at port")
+parser.add_option("--host", type="string", dest="host",
+    help="connect to server")
+parser.add_option("--framed", action="store_true", dest="framed",
+    help="use framed transport")
+parser.add_option("--http", dest="http_path",
+    help="Use the HTTP transport with the specified path")
+parser.add_option('-v', '--verbose', action="store_const", 
+    dest="verbose", const=2,
+    help="verbose output")
+parser.add_option('-q', '--quiet', action="store_const", 
+    dest="verbose", const=0,
+    help="minimal output")
+
+options, args = parser.parse_args()
+
+class AbstractTest(unittest.TestCase):
+  def setUp(self):
+    if options.http_path:
+      self.transport = THttpClient.THttpClient(
+          options.host, options.port, options.http_path)
+    else:
+      socket = TSocket.TSocket(options.host, options.port)
+
+      # frame or buffer depending upon args
+      if options.framed:
+        self.transport = TTransport.TFramedTransport(socket)
+      else:
+        self.transport = TTransport.TBufferedTransport(socket)
+
+    self.transport.open()
+
+    protocol = self.protocol_factory.getProtocol(self.transport)
+    self.client = ThriftTest.Client(protocol)
+
+  def tearDown(self):
+    # Close!
+    self.transport.close()
+
+  def testVoid(self):
+    self.client.testVoid()
+
+  def testString(self):
+    self.assertEqual(self.client.testString('Python'), 'Python')
+
+  def testByte(self):
+    self.assertEqual(self.client.testByte(63), 63)
+
+  def testI32(self):
+    self.assertEqual(self.client.testI32(-1), -1)
+    self.assertEqual(self.client.testI32(0), 0)
+
+  def testI64(self):
+    self.assertEqual(self.client.testI64(-34359738368), -34359738368)
+
+  def testDouble(self):
+    self.assertEqual(self.client.testDouble(-5.235098235), -5.235098235)
+
+  def testStruct(self):
+    x = Xtruct()
+    x.string_thing = "Zero"
+    x.byte_thing = 1
+    x.i32_thing = -3
+    x.i64_thing = -5
+    y = self.client.testStruct(x)
+
+    self.assertEqual(y.string_thing, "Zero")
+    self.assertEqual(y.byte_thing, 1)
+    self.assertEqual(y.i32_thing, -3)
+    self.assertEqual(y.i64_thing, -5)
+
+  def testException(self):
+    self.client.testException('Safe')
+    try:
+      self.client.testException('Xception')
+      self.fail("should have gotten exception")
+    except Xception, x:
+      self.assertEqual(x.errorCode, 1001)
+      self.assertEqual(x.message, 'Xception')
+
+    try:
+      self.client.testException("throw_undeclared")
+      self.fail("should have thrown exception")
+    except Exception: # type is undefined
+      pass
+
+  def testOneway(self):
+    start = time.time()
+    self.client.testOneway(0.5)
+    end = time.time()
+    self.assertTrue(end - start < 0.2,
+                    "oneway sleep took %f sec" % (end - start))
+
+class NormalBinaryTest(AbstractTest):
+  protocol_factory = TBinaryProtocol.TBinaryProtocolFactory()
+
+class AcceleratedBinaryTest(AbstractTest):
+  protocol_factory = TBinaryProtocol.TBinaryProtocolAcceleratedFactory()
+
+def suite():
+  suite = unittest.TestSuite()
+  loader = unittest.TestLoader()
+
+  suite.addTest(loader.loadTestsFromTestCase(NormalBinaryTest))
+  suite.addTest(loader.loadTestsFromTestCase(AcceleratedBinaryTest))
+  return suite
+
+class OwnArgsTestProgram(unittest.TestProgram):
+    def parseArgs(self, argv):
+        if args:
+            self.testNames = args
+        else:
+            self.testNames = (self.defaultTest,)
+        self.createTests()
+
+if __name__ == "__main__":
+  OwnArgsTestProgram(defaultTest="suite", testRunner=unittest.TextTestRunner(verbosity=2))
diff --git a/test/py/TestEof.py b/test/py/TestEof.py
new file mode 100755
index 0000000..7d64289
--- /dev/null
+++ b/test/py/TestEof.py
@@ -0,0 +1,121 @@
+#!/usr/bin/env python
+
+#
+# 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
+sys.path.insert(0, './gen-py')
+sys.path.insert(0, glob.glob('../../lib/py/build/lib.*')[0])
+
+from ThriftTest import ThriftTest
+from ThriftTest.ttypes import *
+from thrift.transport import TTransport
+from thrift.transport import TSocket
+from thrift.protocol import TBinaryProtocol
+import unittest
+import time
+
+class TestEof(unittest.TestCase):
+
+  def setUp(self):
+    trans = TTransport.TMemoryBuffer()
+    prot = TBinaryProtocol.TBinaryProtocol(trans)
+
+    x = Xtruct()
+    x.string_thing = "Zero"
+    x.byte_thing = 0
+
+    x.write(prot)
+
+    x = Xtruct()
+    x.string_thing = "One"
+    x.byte_thing = 1
+
+    x.write(prot)
+
+    self.data = trans.getvalue()
+
+  def testTransportReadAll(self):
+    """Test that readAll on any type of transport throws an EOFError"""
+    trans = TTransport.TMemoryBuffer(self.data)
+    trans.readAll(1)
+
+    try:
+      trans.readAll(10000)
+    except EOFError:
+      return
+
+    self.fail("Should have gotten EOFError")
+
+  def eofTestHelper(self, pfactory):
+    trans = TTransport.TMemoryBuffer(self.data)
+    prot = pfactory.getProtocol(trans)
+
+    x = Xtruct()
+    x.read(prot)
+    self.assertEqual(x.string_thing, "Zero")
+    self.assertEqual(x.byte_thing, 0)
+
+    x = Xtruct()
+    x.read(prot)
+    self.assertEqual(x.string_thing, "One")
+    self.assertEqual(x.byte_thing, 1)
+
+    try:
+      x = Xtruct()
+      x.read(prot)
+    except EOFError:
+      return
+
+    self.fail("Should have gotten EOFError")
+
+  def eofTestHelperStress(self, pfactory):
+    """Teest the ability of TBinaryProtocol to deal with the removal of every byte in the file"""
+    # TODO: we should make sure this covers more of the code paths
+
+    for i in xrange(0, len(self.data) + 1):
+      trans = TTransport.TMemoryBuffer(self.data[0:i])
+      prot = pfactory.getProtocol(trans)
+      try:
+        x = Xtruct()
+        x.read(prot)
+        x.read(prot)
+        x.read(prot)
+      except EOFError:
+        continue
+      self.fail("Should have gotten an EOFError")
+
+  def testBinaryProtocolEof(self):
+    """Test that TBinaryProtocol throws an EOFError when it reaches the end of the stream"""
+    self.eofTestHelper(TBinaryProtocol.TBinaryProtocolFactory())
+    self.eofTestHelperStress(TBinaryProtocol.TBinaryProtocolFactory())
+
+  def testBinaryProtocolAcceleratedEof(self):
+    """Test that TBinaryProtocolAccelerated throws an EOFError when it reaches the end of the stream"""
+    self.eofTestHelper(TBinaryProtocol.TBinaryProtocolAcceleratedFactory())
+    self.eofTestHelperStress(TBinaryProtocol.TBinaryProtocolAcceleratedFactory())
+
+def suite():
+  suite = unittest.TestSuite()
+  loader = unittest.TestLoader()
+  suite.addTest(loader.loadTestsFromTestCase(TestEof))
+  return suite
+
+if __name__ == "__main__":
+  unittest.main(defaultTest="suite", testRunner=unittest.TextTestRunner(verbosity=2))
diff --git a/test/py/TestServer.py b/test/py/TestServer.py
new file mode 100755
index 0000000..3d379ea
--- /dev/null
+++ b/test/py/TestServer.py
@@ -0,0 +1,115 @@
+#!/usr/bin/env python
+
+#
+# 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')
+sys.path.insert(0, glob.glob('../../lib/py/build/lib.*')[0])
+
+from ThriftTest import ThriftTest
+from ThriftTest.ttypes import *
+from thrift.transport import TTransport
+from thrift.transport import TSocket
+from thrift.protocol import TBinaryProtocol
+from thrift.server import TServer, TNonblockingServer, THttpServer
+
+class TestHandler:
+
+  def testVoid(self):
+    print 'testVoid()'
+
+  def testString(self, str):
+    print 'testString(%s)' % str
+    return str
+
+  def testByte(self, byte):
+    print 'testByte(%d)' % byte
+    return byte
+
+  def testI16(self, i16):
+    print 'testI16(%d)' % i16
+    return i16
+
+  def testI32(self, i32):
+    print 'testI32(%d)' % i32
+    return i32
+
+  def testI64(self, i64):
+    print 'testI64(%d)' % i64
+    return i64
+
+  def testDouble(self, dub):
+    print 'testDouble(%f)' % dub
+    return dub
+
+  def testStruct(self, thing):
+    print 'testStruct({%s, %d, %d, %d})' % (thing.string_thing, thing.byte_thing, thing.i32_thing, thing.i64_thing)
+    return thing
+
+  def testException(self, str):
+    print 'testException(%s)' % str
+    if str == 'Xception':
+      x = Xception()
+      x.errorCode = 1001
+      x.message = str
+      raise x
+    elif str == "throw_undeclared":
+      raise ValueError("foo")
+
+  def testOneway(self, seconds):
+    print 'testOneway(%d) => sleeping...' % seconds
+    time.sleep(seconds)
+    print 'done sleeping'
+
+  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
+
+pfactory = TBinaryProtocol.TBinaryProtocolFactory()
+handler = TestHandler()
+processor = ThriftTest.Processor(handler)
+
+if sys.argv[1] == "THttpServer":
+  server = THttpServer.THttpServer(processor, ('', 9090), pfactory)
+else:
+  transport = TSocket.TServerSocket(9090)
+  tfactory = TTransport.TBufferedTransportFactory()
+
+  if sys.argv[1] == "TNonblockingServer":
+    server = TNonblockingServer.TNonblockingServer(processor, transport)
+  else:
+    ServerClass = getattr(TServer, sys.argv[1])
+    server = ServerClass(processor, transport, tfactory, pfactory)
+
+server.serve()
diff --git a/test/py/TestSocket.py b/test/py/TestSocket.py
new file mode 100755
index 0000000..2f7353f
--- /dev/null
+++ b/test/py/TestSocket.py
@@ -0,0 +1,84 @@
+#!/usr/bin/env python
+
+#
+# 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
+sys.path.insert(0, './gen-py')
+sys.path.insert(0, glob.glob('../../lib/py/build/lib.*')[0])
+
+from ThriftTest import ThriftTest
+from ThriftTest.ttypes import *
+from thrift.transport import TTransport
+from thrift.transport import TSocket
+from thrift.protocol import TBinaryProtocol
+import unittest
+import time
+import socket
+import random
+from optparse import OptionParser
+
+class TimeoutTest(unittest.TestCase):
+    def setUp(self):
+        for i in xrange(50):
+            try:
+                # find a port we can use
+                self.listen_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+                self.port = random.randint(10000, 30000)
+                self.listen_sock.bind(('localhost', self.port))
+                self.listen_sock.listen(5)
+                break
+            except:
+                if i == 49:
+                    raise
+
+    def testConnectTimeout(self):
+        starttime = time.time()
+
+        try:
+            leaky = []
+            for i in xrange(100):
+                socket = TSocket.TSocket('localhost', self.port)
+                socket.setTimeout(10)
+                socket.open()
+                leaky.append(socket)
+        except:
+            self.assert_(time.time() - starttime < 5.0)
+
+    def testWriteTimeout(self):
+        starttime = time.time()
+
+        try:
+            socket = TSocket.TSocket('localhost', self.port)
+            socket.setTimeout(10)
+            socket.open()
+            lsock = self.listen_sock.accept()
+            while True:
+                socket.write("hi" * 100)
+
+        except:
+            self.assert_(time.time() - starttime < 5.0)
+
+suite = unittest.TestSuite()
+loader = unittest.TestLoader()
+
+suite.addTest(loader.loadTestsFromTestCase(TimeoutTest))
+
+testRunner = unittest.TextTestRunner(verbosity=2)
+testRunner.run(suite)
diff --git a/test/py/TestSyntax.py b/test/py/TestSyntax.py
new file mode 100755
index 0000000..df67d48
--- /dev/null
+++ b/test/py/TestSyntax.py
@@ -0,0 +1,28 @@
+#!/usr/bin/env python
+
+#
+# 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
+sys.path.insert(0, './gen-py')
+sys.path.insert(0, glob.glob('../../lib/py/build/lib.*')[0])
+
+# Just import these generated files to make sure they are syntactically valid
+from DebugProtoTest import EmptyService
+from DebugProtoTest import Inherited
diff --git a/test/py/explicit_module/runtest.sh b/test/py/explicit_module/runtest.sh
new file mode 100755
index 0000000..2e5a4f1
--- /dev/null
+++ b/test/py/explicit_module/runtest.sh
@@ -0,0 +1,34 @@
+#!/bin/bash
+
+#
+# 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.
+#
+
+rm -rf gen-py
+../../../compiler/cpp/thrift --gen py test1.thrift || exit 1
+../../../compiler/cpp/thrift --gen py test2.thrift || exit 1
+PYTHONPATH=./gen-py python -c 'import foo.bar.baz' || exit 1
+PYTHONPATH=./gen-py python -c 'import test2' || exit 1
+PYTHONPATH=./gen-py python -c 'import test1' &>/dev/null && exit 1  # Should fail.
+cp -r gen-py simple
+../../../compiler/cpp/thrift -r --gen py test2.thrift || exit 1
+PYTHONPATH=./gen-py python -c 'import test2' || exit 1
+diff -ur simple gen-py > thediffs
+file thediffs | grep -s -q empty || exit 1
+rm -rf simple thediffs
+echo 'All tests pass!'
diff --git a/test/py/explicit_module/test1.thrift b/test/py/explicit_module/test1.thrift
new file mode 100644
index 0000000..ec600d7
--- /dev/null
+++ b/test/py/explicit_module/test1.thrift
@@ -0,0 +1,24 @@
+/*
+ * 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.
+ */
+
+namespace py foo.bar.baz
+
+struct astruct {
+  1: i32 how_unoriginal;
+}
diff --git a/test/py/explicit_module/test2.thrift b/test/py/explicit_module/test2.thrift
new file mode 100644
index 0000000..68f9da4
--- /dev/null
+++ b/test/py/explicit_module/test2.thrift
@@ -0,0 +1,24 @@
+/*
+ * 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.
+ */
+
+include "test1.thrift"
+
+struct another {
+  1: test1.astruct something;
+}