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")
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)
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)
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))
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))
def test_snappy(self): for i in range(100): s1 = os.urandom(120) s2 = snappy_decode(snappy_encode(s1)) self.assertEqual(s1, s2)
def test_snappy(self): for i in xrange(100): s1 = random_string(120) s2 = snappy_decode(snappy_encode(s1)) self.assertEqual(s1, s2)
def test_snappy(self): for _i in range(100): s1 = os.urandom(120) s2 = snappy_decode(snappy_encode(s1)) self.assertEqual(s1, s2)