blob: 296a50a2f9e61864cbdfce7358587b0bf86b8fb8 [file] [log] [blame]
David Reiss9ff3b9d2008-02-15 01:10:23 +00001#!/usr/bin/env python
2
3import sys, glob
4sys.path.insert(0, './gen-py')
5sys.path.insert(0, glob.glob('../../lib/py/build/lib.*')[0])
6
7from ThriftTest.ttypes import *
8from thrift.transport import TTransport
9from thrift.transport import TSocket
10from thrift.protocol import TBinaryProtocol
11import unittest
12import time
13
14class AbstractTest(unittest.TestCase):
15
16 def setUp(self):
David Reiss46bb4ae2009-01-14 22:34:15 +000017 self.v1obj = VersioningTestV1(
David Reiss9ff3b9d2008-02-15 01:10:23 +000018 begin_in_both=12345,
David Reissa528f542009-03-24 22:48:40 +000019 old_string='aaa',
David Reiss9ff3b9d2008-02-15 01:10:23 +000020 end_in_both=54321,
David Reiss46bb4ae2009-01-14 22:34:15 +000021 )
David Reiss9ff3b9d2008-02-15 01:10:23 +000022
David Reiss46bb4ae2009-01-14 22:34:15 +000023 self.v2obj = VersioningTestV2(
David Reiss9ff3b9d2008-02-15 01:10:23 +000024 begin_in_both=12345,
25 newint=1,
26 newbyte=2,
27 newshort=3,
28 newlong=4,
29 newdouble=5.0,
David Reiss46bb4ae2009-01-14 22:34:15 +000030 newstruct=Bonk(message="Hello!", type=123),
David Reiss9ff3b9d2008-02-15 01:10:23 +000031 newlist=[7,8,9],
32 newset=[42,1,8],
33 newmap={1:2,2:3},
34 newstring="Hola!",
35 end_in_both=54321,
David Reiss46bb4ae2009-01-14 22:34:15 +000036 )
David Reiss9ff3b9d2008-02-15 01:10:23 +000037
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 Reiss1cc0c5e2008-10-17 19:30:35 +000052 self.assertEquals(obj.begin_in_both, self.v1obj.begin_in_both)
53 self.assertEquals(obj.end_in_both, self.v1obj.end_in_both)
David Reiss9ff3b9d2008-02-15 01:10:23 +000054
55 def testBackwards(self):
56 obj = self._deserialize(VersioningTestV1, self._serialize(self.v2obj))
David Reiss1cc0c5e2008-10-17 19:30:35 +000057 self.assertEquals(obj.begin_in_both, self.v2obj.begin_in_both)
58 self.assertEquals(obj.end_in_both, self.v2obj.end_in_both)
David Reiss9ff3b9d2008-02-15 01:10:23 +000059
60
61class NormalBinaryTest(AbstractTest):
62 protocol_factory = TBinaryProtocol.TBinaryProtocolFactory()
63
64class AcceleratedBinaryTest(AbstractTest):
65 protocol_factory = TBinaryProtocol.TBinaryProtocolAcceleratedFactory()
66
67
David Reiss4c591c92009-01-31 21:39:25 +000068class 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 Reiss9ff3b9d2008-02-15 01:10:23 +0000104def suite():
105 suite = unittest.TestSuite()
106 loader = unittest.TestLoader()
107
108 suite.addTest(loader.loadTestsFromTestCase(NormalBinaryTest))
109 suite.addTest(loader.loadTestsFromTestCase(AcceleratedBinaryTest))
David Reiss4c591c92009-01-31 21:39:25 +0000110 suite.addTest(loader.loadTestsFromTestCase(AcceleratedFramedTest))
David Reiss9ff3b9d2008-02-15 01:10:23 +0000111 return suite
112
113if __name__ == "__main__":
114 unittest.main(defaultTest="suite", testRunner=unittest.TextTestRunner(verbosity=2))