예제 #1
0
 def test_snappy_raises_when_not_present(self):
     with patch.object(afkak.codec, 'has_snappy',
                       return_value=False):
         with self.assertRaises(NotImplementedError):
             snappy_encode("Snappy not available")
         with self.assertRaises(NotImplementedError):
             snappy_decode("Snappy not available")
예제 #2
0
    def test_create_snappy(self):
        if not has_snappy():
            raise SkipTest("Snappy not available")  # pragma: no cover
        message_list = [create_message(b"v3", key=b'84'),
                        create_message(b"v4", None)]
        msg = create_snappy_message(message_list)
        self.assertEqual(msg.magic, 0)
        self.assertEqual(msg.attributes, ATTRIBUTE_CODEC_MASK & CODEC_SNAPPY)
        self.assertEqual(msg.key, None)
        decoded = snappy_decode(msg.value)
        expect = b"".join([
            struct.pack(">q", 0),           # MsgSet offset
            struct.pack(">i", 18),          # MsgSet size
            struct.pack(">i", 813233088),   # CRC
            struct.pack(">bb", 0, 0),       # Magic, flags
            struct.pack(">i2s", 2, b'84'),  # Key is 2 bytes long, '84'
            struct.pack(">i", 2),           # Msg length (bytes)
            b"v3",                          # Message contents

            struct.pack(">q", 0),           # MsgSet offset
            struct.pack(">i", 16),          # MsgSet size
            struct.pack(">i", 1022734157),  # CRC
            struct.pack(">bb", 0, 0),       # Magic, flags
            struct.pack(">i", -1),          # -1 indicates a null key
            struct.pack(">i", 2),           # Msg length (bytes)
            b"v4",                          # Message contents
        ])

        self.assertEqual(decoded, expect)
예제 #3
0
    def test_create_snappy(self):
        if not has_snappy():
            raise SkipTest("Snappy not available")  # pragma: no cover
        message_list = [
            create_message(b"v3", key=b'84'),
            create_message(b"v4", None)
        ]
        msg = create_snappy_message(message_list)
        self.assertEqual(msg.magic, 0)
        self.assertEqual(msg.attributes, ATTRIBUTE_CODEC_MASK & CODEC_SNAPPY)
        self.assertEqual(msg.key, None)
        decoded = snappy_decode(msg.value)
        expect = b"".join([
            struct.pack(">q", 0),  # MsgSet offset
            struct.pack(">i", 18),  # MsgSet size
            struct.pack(">i", 813233088),  # CRC
            struct.pack(">bb", 0, 0),  # Magic, flags
            struct.pack(">i2s", 2, b'84'),  # Key is 2 bytes long, '84'
            struct.pack(">i", 2),  # Msg length (bytes)
            b"v3",  # Message contents
            struct.pack(">q", 0),  # MsgSet offset
            struct.pack(">i", 16),  # MsgSet size
            struct.pack(">i", 1022734157),  # CRC
            struct.pack(">bb", 0, 0),  # Magic, flags
            struct.pack(">i", -1),  # -1 indicates a null key
            struct.pack(">i", 2),  # Msg length (bytes)
            b"v4",  # Message contents
        ])

        self.assertEqual(decoded, expect)
예제 #4
0
    def test_snappy_decode_xerial(self):
        header = b'\x82SNAPPY\x00\x00\x00\x00\x01\x00\x00\x00\x01'
        random_snappy = snappy_encode(b'SNAPPY' * 50)
        block_len = len(random_snappy)
        random_snappy2 = snappy_encode(b'XERIAL' * 50)
        block_len2 = len(random_snappy2)

        to_test = header \
            + struct.pack('!i', block_len) + random_snappy \
            + struct.pack('!i', block_len2) + random_snappy2 \

        self.assertEqual(snappy_decode(to_test),
                         (b'SNAPPY' * 50) + (b'XERIAL' * 50))
예제 #5
0
    def test_snappy_decode_xerial(self):
        header = b'\x82SNAPPY\x00\x00\x00\x00\x01\x00\x00\x00\x01'
        random_snappy = snappy_encode('SNAPPY' * 50)
        block_len = len(random_snappy)
        random_snappy2 = snappy_encode('XERIAL' * 50)
        block_len2 = len(random_snappy2)

        to_test = header \
            + struct.pack('!i', block_len) + random_snappy \
            + struct.pack('!i', block_len2) + random_snappy2 \

        self.assertEqual(
            snappy_decode(to_test), ('SNAPPY' * 50) + ('XERIAL' * 50))
예제 #6
0
 def test_snappy(self):
     for i in range(100):
         s1 = os.urandom(120)
         s2 = snappy_decode(snappy_encode(s1))
         self.assertEqual(s1, s2)
예제 #7
0
 def test_snappy(self):
     for i in xrange(100):
         s1 = random_string(120)
         s2 = snappy_decode(snappy_encode(s1))
         self.assertEqual(s1, s2)
예제 #8
0
파일: test_codec.py 프로젝트: ciena/afkak
 def test_snappy(self):
     for _i in range(100):
         s1 = os.urandom(120)
         s2 = snappy_decode(snappy_encode(s1))
         self.assertEqual(s1, s2)