THRIFT-1094. py: bug in TCompactProto python readMessageEnd method and updated test cases


This patch fixes a TCompactProtocol bug and expands the test cases to exercise the problem.

Patch: Will Pierce

git-svn-id: https://svn.apache.org/repos/asf/thrift/trunk@1083877 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/test/py/TestClient.py b/test/py/TestClient.py
index 0a38b03..eecb850 100755
--- a/test/py/TestClient.py
+++ b/test/py/TestClient.py
@@ -29,13 +29,13 @@
 from thrift.transport import TSocket
 from thrift.transport import THttpClient
 from thrift.protocol import TBinaryProtocol
+from thrift.protocol import TCompactProtocol
 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",
@@ -50,7 +50,9 @@
 parser.add_option('-q', '--quiet', action="store_const", 
     dest="verbose", const=0,
     help="minimal output")
-
+parser.add_option('--proto',  dest="proto", type="string",
+    help="protocol to use, one of: accel, binary, compact")
+parser.set_defaults(framed=False, http_path=None, verbose=1, host='localhost', port=9090, proto='binary')
 options, args = parser.parse_args()
 
 class AbstractTest(unittest.TestCase):
@@ -81,19 +83,24 @@
 
   def testString(self):
     self.assertEqual(self.client.testString('Python'), 'Python')
+    self.assertEqual(self.client.testString(''), '')
 
   def testByte(self):
     self.assertEqual(self.client.testByte(63), 63)
+    self.assertEqual(self.client.testByte(-127), -127)
 
   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(1), 1)
     self.assertEqual(self.client.testI64(-34359738368), -34359738368)
 
   def testDouble(self):
     self.assertEqual(self.client.testDouble(-5.235098235), -5.235098235)
+    self.assertEqual(self.client.testDouble(0), 0)
+    self.assertEqual(self.client.testDouble(-1), -1)
 
   def testStruct(self):
     x = Xtruct()
@@ -102,11 +109,57 @@
     x.i32_thing = -3
     x.i64_thing = -5
     y = self.client.testStruct(x)
+    self.assertEqual(y, 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 testNest(self):
+    inner = Xtruct(string_thing="Zero", byte_thing=1, i32_thing=-3,
+      i64_thing=-5)
+    x = Xtruct2(struct_thing=inner)
+    y = self.client.testNest(x)
+    self.assertEqual(y, x)
+
+  def testMap(self):
+    x = {0:1, 1:2, 2:3, 3:4, -1:-2}
+    y = self.client.testMap(x)
+    self.assertEqual(y, x)
+
+  def testSet(self):
+    x = set([8, 1, 42])
+    y = self.client.testSet(x)
+    self.assertEqual(y, x)
+
+  def testList(self):
+    x = [1, 4, 9, -42]
+    y = self.client.testList(x)
+    self.assertEqual(y, x)
+
+  def testEnum(self):
+    x = Numberz.FIVE
+    y = self.client.testEnum(x)
+    self.assertEqual(y, x)
+
+  def testTypedef(self):
+    x = 0xffffffffffffff # 7 bytes of 0xff
+    y = self.client.testTypedef(x)
+    self.assertEqual(y, x)
+
+  def testMapMap(self):
+    # does not work: dict() is not a hashable type, so a dict() cannot be used as a key in another dict()
+    #x = { {1:10, 2:20}, {1:100, 2:200, 3:300}, {1:1000, 2:2000, 3:3000, 4:4000} }
+    try:
+      y = self.client.testMapMap()
+    except:
+      pass
+
+  def testMulti(self):
+    xpected = Xtruct(byte_thing=74, i32_thing=0xff00ff, i64_thing=0xffffffffd0d0)
+    y = self.client.testMulti(xpected.byte_thing,
+          xpected.i32_thing,
+          xpected.i64_thing,
+          { 0:'abc' },
+          Numberz.FIVE,
+          0xf0f0f0)
+    self.assertEqual(y, xpected)
 
   def testException(self):
     self.client.testException('Safe')
@@ -125,27 +178,35 @@
 
   def testOneway(self):
     start = time.time()
-    self.client.testOneway(0.5)
+    self.client.testOneway(1) # type is int, not float
     end = time.time()
-    self.assertTrue(end - start < 0.2,
+    self.assertTrue(end - start < 3,
                     "oneway sleep took %f sec" % (end - start))
   
   def testOnewayThenNormal(self):
-    self.client.testOneway(0.5)
+    self.client.testOneway(1) # type is int, not float
     self.assertEqual(self.client.testString('Python'), 'Python')
 
 class NormalBinaryTest(AbstractTest):
   protocol_factory = TBinaryProtocol.TBinaryProtocolFactory()
 
+class CompactTest(AbstractTest):
+  protocol_factory = TCompactProtocol.TCompactProtocolFactory()
+
 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))
+  if options.proto == 'binary': # look for --proto on cmdline
+    suite.addTest(loader.loadTestsFromTestCase(NormalBinaryTest))
+  elif options.proto == 'accel':
+    suite.addTest(loader.loadTestsFromTestCase(AcceleratedBinaryTest))
+  elif options.proto == 'compact':
+    suite.addTest(loader.loadTestsFromTestCase(CompactTest))
+  else:
+    raise AssertionError('Unknown protocol given with --proto: %s' % options.proto)
   return suite
 
 class OwnArgsTestProgram(unittest.TestProgram):