blob: a99bce6f1fc4b66da56bbc07837558fb20200ce8 [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,
19 end_in_both=54321,
David Reiss46bb4ae2009-01-14 22:34:15 +000020 )
David Reiss9ff3b9d2008-02-15 01:10:23 +000021
David Reiss46bb4ae2009-01-14 22:34:15 +000022 self.v2obj = VersioningTestV2(
David Reiss9ff3b9d2008-02-15 01:10:23 +000023 begin_in_both=12345,
24 newint=1,
25 newbyte=2,
26 newshort=3,
27 newlong=4,
28 newdouble=5.0,
David Reiss46bb4ae2009-01-14 22:34:15 +000029 newstruct=Bonk(message="Hello!", type=123),
David Reiss9ff3b9d2008-02-15 01:10:23 +000030 newlist=[7,8,9],
31 newset=[42,1,8],
32 newmap={1:2,2:3},
33 newstring="Hola!",
34 end_in_both=54321,
David Reiss46bb4ae2009-01-14 22:34:15 +000035 )
David Reiss9ff3b9d2008-02-15 01:10:23 +000036
37 def _serialize(self, obj):
38 trans = TTransport.TMemoryBuffer()
39 prot = self.protocol_factory.getProtocol(trans)
40 obj.write(prot)
41 return trans.getvalue()
42
43 def _deserialize(self, objtype, data):
44 prot = self.protocol_factory.getProtocol(TTransport.TMemoryBuffer(data))
45 ret = objtype()
46 ret.read(prot)
47 return ret
48
49 def testForwards(self):
50 obj = self._deserialize(VersioningTestV2, self._serialize(self.v1obj))
David Reiss1cc0c5e2008-10-17 19:30:35 +000051 self.assertEquals(obj.begin_in_both, self.v1obj.begin_in_both)
52 self.assertEquals(obj.end_in_both, self.v1obj.end_in_both)
David Reiss9ff3b9d2008-02-15 01:10:23 +000053
54 def testBackwards(self):
55 obj = self._deserialize(VersioningTestV1, self._serialize(self.v2obj))
David Reiss1cc0c5e2008-10-17 19:30:35 +000056 self.assertEquals(obj.begin_in_both, self.v2obj.begin_in_both)
57 self.assertEquals(obj.end_in_both, self.v2obj.end_in_both)
David Reiss9ff3b9d2008-02-15 01:10:23 +000058
59
60class NormalBinaryTest(AbstractTest):
61 protocol_factory = TBinaryProtocol.TBinaryProtocolFactory()
62
63class AcceleratedBinaryTest(AbstractTest):
64 protocol_factory = TBinaryProtocol.TBinaryProtocolAcceleratedFactory()
65
66
David Reiss4c591c92009-01-31 21:39:25 +000067class AcceleratedFramedTest(unittest.TestCase):
68 def testSplit(self):
69 """Test FramedTransport and BinaryProtocolAccelerated
70
71 Tests that TBinaryProtocolAccelerated and TFramedTransport
72 play nicely together when a read spans a frame"""
73
74 protocol_factory = TBinaryProtocol.TBinaryProtocolAcceleratedFactory()
75 bigstring = "".join(chr(byte) for byte in range(ord("a"), ord("z")+1))
76
77 databuf = TTransport.TMemoryBuffer()
78 prot = protocol_factory.getProtocol(databuf)
79 prot.writeI32(42)
80 prot.writeString(bigstring)
81 prot.writeI16(24)
82 data = databuf.getvalue()
83 cutpoint = len(data)/2
84 parts = [ data[:cutpoint], data[cutpoint:] ]
85
86 framed_buffer = TTransport.TMemoryBuffer()
87 framed_writer = TTransport.TFramedTransport(framed_buffer)
88 for part in parts:
89 framed_writer.write(part)
90 framed_writer.flush()
91 self.assertEquals(len(framed_buffer.getvalue()), len(data) + 8)
92
93 # Recreate framed_buffer so we can read from it.
94 framed_buffer = TTransport.TMemoryBuffer(framed_buffer.getvalue())
95 framed_reader = TTransport.TFramedTransport(framed_buffer)
96 prot = protocol_factory.getProtocol(framed_reader)
97 self.assertEqual(prot.readI32(), 42)
98 self.assertEqual(prot.readString(), bigstring)
99 self.assertEqual(prot.readI16(), 24)
100
101
102
David Reiss9ff3b9d2008-02-15 01:10:23 +0000103def suite():
104 suite = unittest.TestSuite()
105 loader = unittest.TestLoader()
106
107 suite.addTest(loader.loadTestsFromTestCase(NormalBinaryTest))
108 suite.addTest(loader.loadTestsFromTestCase(AcceleratedBinaryTest))
David Reiss4c591c92009-01-31 21:39:25 +0000109 suite.addTest(loader.loadTestsFromTestCase(AcceleratedFramedTest))
David Reiss9ff3b9d2008-02-15 01:10:23 +0000110 return suite
111
112if __name__ == "__main__":
113 unittest.main(defaultTest="suite", testRunner=unittest.TextTestRunner(verbosity=2))