David Reiss | 9ff3b9d | 2008-02-15 01:10:23 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | import sys, glob |
| 4 | sys.path.insert(0, './gen-py') |
| 5 | sys.path.insert(0, glob.glob('../../lib/py/build/lib.*')[0]) |
| 6 | |
| 7 | from ThriftTest.ttypes import * |
| 8 | from thrift.transport import TTransport |
| 9 | from thrift.transport import TSocket |
| 10 | from thrift.protocol import TBinaryProtocol |
| 11 | import unittest |
| 12 | import time |
| 13 | |
| 14 | class AbstractTest(unittest.TestCase): |
| 15 | |
| 16 | def setUp(self): |
David Reiss | 46bb4ae | 2009-01-14 22:34:15 +0000 | [diff] [blame] | 17 | self.v1obj = VersioningTestV1( |
David Reiss | 9ff3b9d | 2008-02-15 01:10:23 +0000 | [diff] [blame] | 18 | begin_in_both=12345, |
David Reiss | a528f54 | 2009-03-24 22:48:40 +0000 | [diff] [blame] | 19 | old_string='aaa', |
David Reiss | 9ff3b9d | 2008-02-15 01:10:23 +0000 | [diff] [blame] | 20 | end_in_both=54321, |
David Reiss | 46bb4ae | 2009-01-14 22:34:15 +0000 | [diff] [blame] | 21 | ) |
David Reiss | 9ff3b9d | 2008-02-15 01:10:23 +0000 | [diff] [blame] | 22 | |
David Reiss | 46bb4ae | 2009-01-14 22:34:15 +0000 | [diff] [blame] | 23 | self.v2obj = VersioningTestV2( |
David Reiss | 9ff3b9d | 2008-02-15 01:10:23 +0000 | [diff] [blame] | 24 | begin_in_both=12345, |
| 25 | newint=1, |
| 26 | newbyte=2, |
| 27 | newshort=3, |
| 28 | newlong=4, |
| 29 | newdouble=5.0, |
David Reiss | 46bb4ae | 2009-01-14 22:34:15 +0000 | [diff] [blame] | 30 | newstruct=Bonk(message="Hello!", type=123), |
David Reiss | 9ff3b9d | 2008-02-15 01:10:23 +0000 | [diff] [blame] | 31 | newlist=[7,8,9], |
| 32 | newset=[42,1,8], |
| 33 | newmap={1:2,2:3}, |
| 34 | newstring="Hola!", |
| 35 | end_in_both=54321, |
David Reiss | 46bb4ae | 2009-01-14 22:34:15 +0000 | [diff] [blame] | 36 | ) |
David Reiss | 9ff3b9d | 2008-02-15 01:10:23 +0000 | [diff] [blame] | 37 | |
| 38 | def _serialize(self, obj): |
| 39 | trans = TTransport.TMemoryBuffer() |
| 40 | prot = self.protocol_factory.getProtocol(trans) |
| 41 | obj.write(prot) |
| 42 | return trans.getvalue() |
| 43 | |
| 44 | def _deserialize(self, objtype, data): |
| 45 | prot = self.protocol_factory.getProtocol(TTransport.TMemoryBuffer(data)) |
| 46 | ret = objtype() |
| 47 | ret.read(prot) |
| 48 | return ret |
| 49 | |
| 50 | def testForwards(self): |
| 51 | obj = self._deserialize(VersioningTestV2, self._serialize(self.v1obj)) |
David Reiss | 1cc0c5e | 2008-10-17 19:30:35 +0000 | [diff] [blame] | 52 | self.assertEquals(obj.begin_in_both, self.v1obj.begin_in_both) |
| 53 | self.assertEquals(obj.end_in_both, self.v1obj.end_in_both) |
David Reiss | 9ff3b9d | 2008-02-15 01:10:23 +0000 | [diff] [blame] | 54 | |
| 55 | def testBackwards(self): |
| 56 | obj = self._deserialize(VersioningTestV1, self._serialize(self.v2obj)) |
David Reiss | 1cc0c5e | 2008-10-17 19:30:35 +0000 | [diff] [blame] | 57 | self.assertEquals(obj.begin_in_both, self.v2obj.begin_in_both) |
| 58 | self.assertEquals(obj.end_in_both, self.v2obj.end_in_both) |
David Reiss | 9ff3b9d | 2008-02-15 01:10:23 +0000 | [diff] [blame] | 59 | |
| 60 | |
| 61 | class NormalBinaryTest(AbstractTest): |
| 62 | protocol_factory = TBinaryProtocol.TBinaryProtocolFactory() |
| 63 | |
| 64 | class AcceleratedBinaryTest(AbstractTest): |
| 65 | protocol_factory = TBinaryProtocol.TBinaryProtocolAcceleratedFactory() |
| 66 | |
| 67 | |
David Reiss | 4c591c9 | 2009-01-31 21:39:25 +0000 | [diff] [blame] | 68 | class AcceleratedFramedTest(unittest.TestCase): |
| 69 | def testSplit(self): |
| 70 | """Test FramedTransport and BinaryProtocolAccelerated |
| 71 | |
| 72 | Tests that TBinaryProtocolAccelerated and TFramedTransport |
| 73 | play nicely together when a read spans a frame""" |
| 74 | |
| 75 | protocol_factory = TBinaryProtocol.TBinaryProtocolAcceleratedFactory() |
| 76 | bigstring = "".join(chr(byte) for byte in range(ord("a"), ord("z")+1)) |
| 77 | |
| 78 | databuf = TTransport.TMemoryBuffer() |
| 79 | prot = protocol_factory.getProtocol(databuf) |
| 80 | prot.writeI32(42) |
| 81 | prot.writeString(bigstring) |
| 82 | prot.writeI16(24) |
| 83 | data = databuf.getvalue() |
| 84 | cutpoint = len(data)/2 |
| 85 | parts = [ data[:cutpoint], data[cutpoint:] ] |
| 86 | |
| 87 | framed_buffer = TTransport.TMemoryBuffer() |
| 88 | framed_writer = TTransport.TFramedTransport(framed_buffer) |
| 89 | for part in parts: |
| 90 | framed_writer.write(part) |
| 91 | framed_writer.flush() |
| 92 | self.assertEquals(len(framed_buffer.getvalue()), len(data) + 8) |
| 93 | |
| 94 | # Recreate framed_buffer so we can read from it. |
| 95 | framed_buffer = TTransport.TMemoryBuffer(framed_buffer.getvalue()) |
| 96 | framed_reader = TTransport.TFramedTransport(framed_buffer) |
| 97 | prot = protocol_factory.getProtocol(framed_reader) |
| 98 | self.assertEqual(prot.readI32(), 42) |
| 99 | self.assertEqual(prot.readString(), bigstring) |
| 100 | self.assertEqual(prot.readI16(), 24) |
| 101 | |
| 102 | |
| 103 | |
David Reiss | 9ff3b9d | 2008-02-15 01:10:23 +0000 | [diff] [blame] | 104 | def suite(): |
| 105 | suite = unittest.TestSuite() |
| 106 | loader = unittest.TestLoader() |
| 107 | |
| 108 | suite.addTest(loader.loadTestsFromTestCase(NormalBinaryTest)) |
| 109 | suite.addTest(loader.loadTestsFromTestCase(AcceleratedBinaryTest)) |
David Reiss | 4c591c9 | 2009-01-31 21:39:25 +0000 | [diff] [blame] | 110 | suite.addTest(loader.loadTestsFromTestCase(AcceleratedFramedTest)) |
David Reiss | 9ff3b9d | 2008-02-15 01:10:23 +0000 | [diff] [blame] | 111 | return suite |
| 112 | |
| 113 | if __name__ == "__main__": |
| 114 | unittest.main(defaultTest="suite", testRunner=unittest.TextTestRunner(verbosity=2)) |