コード例 #1
0
    def _test_protocol_write(self, obj):
        # cython版本
        buff0 = TCyMemoryBuffer()
        op0 = TCyBinaryProtocol(buff0)
        op0.write_struct(obj)
        value0 = buff0.getvalue()

        # Python版本
        buff1 = TMemoryBuffer()
        op1 = TUtf8BinaryProtocol(buff1)
        obj.write(op1)
        value1 = buff1.get_raw_value()

        self.assertEqual(value1, value0, "write result not the same")
コード例 #2
0
    def _test_protocol_write(self, obj):
        # cython版本
        buff0 = TCyMemoryBuffer()
        op0 = TCyBinaryProtocol(buff0)
        op0.write_struct(obj)
        value0 = buff0.getvalue()

        # Python版本
        buff1 = TMemoryBuffer()
        op1 = TUtf8BinaryProtocol(buff1)
        obj.write(op1)
        value1 = buff1.get_raw_value()

        self.assertEqual(value1, value0, "write result not the same")
コード例 #3
0
    def test_write_flush(self):
        """
            py.test test/test_framed_buffer.py::FramedBufferTest::test_write_flush -s
        """
        buf = TMemoryBuffer()
        transport = TCyFramedTransport(buf)
        transport.write("abcdef")
        transport.flush()

        print "Framed Output: ", ["%03d" % ord(i) for i in buf.getvalue()]

        # MemoryBuffer作为FrameBuffer来使用
        buf2 = TCyMemoryBuffer()
        buf2.prepare_4_frame()
        buf2.write("abcdef")

        buf1 = TMemoryBuffer()
        tran1 = TCyFramedTransportEx(buf1)
        tran1.flush_frame_buff(buf2)

        print "Framed Output: ", ["%03d" % ord(i) for i in buf1.getvalue()]

        buf1.reset()
        tran1 = TCyFramedTransportEx(buf1)

        mem_trans = tran1.read_frame()
        print "ReadFrame: ", mem_trans
        value = mem_trans.getvalue()
        print "Value: ", value,  ["%03d" % ord(i) for i in value]
コード例 #4
0
    def test_read_write_exception(self):
        """
            测试可行性
            py.test test/test_protocol.py::SimpleTest::test_read_write_exception -s
        """
        ex = RpcException(1, "Hello")

        self._test_protocol_write(ex)

        total_time = 1
        t = time.time()
        for i in range(total_time):
            buff0 = TCyMemoryBuffer()
            op0 = TCyBinaryProtocol(buff0)
            op0.write_struct(ex)
            value0 = buff0.getvalue()
        t = time.time() - t
        print "T: %.3fms" % (t * 1000)
        print "Value0:", ["%03d" % ord(i) for i in value0]

        buff0 = TCyMemoryBuffer(value0)
        op0 = TCyBinaryProtocol(buff0)
        ex0 = op0.read_struct(RpcException())
        print "Code: ", ex0.code
        print "Msg: ", ex0.msg







        t = time.time()
        for i in range(total_time):
            buff1 = TMemoryBuffer()
            op1 = TBinaryProtocol(buff1)
            ex.write(op1)
            value1 = buff1.get_raw_value()
        t = time.time() - t
        print "T: %.3fms" % (t * 1000)
        print "Value1:", ["%03d" % ord(i) for i in value1]

        self.assertEqual(value1, value0, "write struct test failed")
コード例 #5
0
    def test_read_write_exception(self):
        """
            测试可行性
            py.test test/test_protocol.py::SimpleTest::test_read_write_exception -s
        """
        ex = RpcException(1, "Hello")

        self._test_protocol_write(ex)

        total_time = 1
        t = time.time()
        for i in range(total_time):
            buff0 = TCyMemoryBuffer()
            op0 = TCyBinaryProtocol(buff0)
            op0.write_struct(ex)
            value0 = buff0.getvalue()
        t = time.time() - t
        print "T: %.3fms" % (t * 1000)
        print "Value0:", ["%03d" % ord(i) for i in value0]

        buff0 = TCyMemoryBuffer(value0)
        op0 = TCyBinaryProtocol(buff0)
        ex0 = op0.read_struct(RpcException())
        print "Code: ", ex0.code
        print "Msg: ", ex0.msg

        t = time.time()
        for i in range(total_time):
            buff1 = TMemoryBuffer()
            op1 = TBinaryProtocol(buff1)
            ex.write(op1)
            value1 = buff1.get_raw_value()
        t = time.time() - t
        print "T: %.3fms" % (t * 1000)
        print "Value1:", ["%03d" % ord(i) for i in value1]

        self.assertEqual(value1, value0, "write struct test failed")