random_string helper should return str not bytes

This commit is contained in:
Dana Powers
2015-06-04 22:10:21 -07:00
parent 868115c703
commit 207499b26d
4 changed files with 16 additions and 17 deletions

View File

@@ -13,16 +13,16 @@ from test.testutil import random_string
class TestCodec(unittest.TestCase):
def test_gzip(self):
for i in xrange(1000):
s1 = random_string(100)
s2 = gzip_decode(gzip_encode(s1))
self.assertEqual(s1, s2)
b1 = random_string(100).encode('utf-8')
b2 = gzip_decode(gzip_encode(b1))
self.assertEqual(b1, b2)
@unittest.skipUnless(has_snappy(), "Snappy not available")
def test_snappy(self):
for i in xrange(1000):
s1 = random_string(100)
s2 = snappy_decode(snappy_encode(s1))
self.assertEqual(s1, s2)
b1 = random_string(100).encode('utf-8')
b2 = snappy_decode(snappy_encode(b1))
self.assertEqual(b1, b2)
@unittest.skipUnless(has_snappy(), "Snappy not available")
def test_snappy_detect_xerial(self):

View File

@@ -475,7 +475,7 @@ class TestConsumerIntegration(KafkaIntegrationTestCase):
@kafka_versions("0.8.1", "0.8.1.1", "0.8.2.0")
def test_kafka_consumer__offset_commit_resume(self):
GROUP_ID = random_string(10)
GROUP_ID = random_string(10).encode('utf-8')
self.send_messages(0, range(0, 100))
self.send_messages(1, range(100, 200))

View File

@@ -133,8 +133,8 @@ class TestFailover(KafkaIntegrationTestCase):
# Send 10 random messages
for _ in range(10):
key = random_string(3)
msg = random_string(10)
key = random_string(3).encode('utf-8')
msg = random_string(10).encode('utf-8')
producer.send_messages(topic, key, msg)
# kill leader for partition 0
@@ -145,8 +145,8 @@ class TestFailover(KafkaIntegrationTestCase):
timeout = 60
while not recovered and (time.time() - started) < timeout:
try:
key = random_string(3)
msg = random_string(10)
key = random_string(3).encode('utf-8')
msg = random_string(10).encode('utf-8')
producer.send_messages(topic, key, msg)
if producer.partitioners[kafka_bytestring(topic)].partition(key) == 0:
recovered = True
@@ -159,15 +159,15 @@ class TestFailover(KafkaIntegrationTestCase):
# send some more messages just to make sure no more exceptions
for _ in range(10):
key = random_string(3)
msg = random_string(10)
key = random_string(3).encode('utf-8')
msg = random_string(10).encode('utf-8')
producer.send_messages(topic, key, msg)
def _send_random_messages(self, producer, topic, partition, n):
for j in range(n):
logging.debug('_send_random_message to %s:%d -- try %d', topic, partition, j)
resp = producer.send_messages(topic, partition, random_string(10))
resp = producer.send_messages(topic, partition, random_string(10).encode('utf-8'))
if len(resp) > 0:
self.assertEqual(resp[0].error, 0)
logging.debug('_send_random_message to %s:%d -- try %d success', topic, partition, j)

View File

@@ -23,8 +23,7 @@ __all__ = [
]
def random_string(l):
s = "".join(random.choice(string.ascii_letters) for i in xrange(l))
return s.encode('utf-8')
return "".join(random.choice(string.ascii_letters) for i in xrange(l))
def kafka_versions(*versions):
def kafka_versions(func):
@@ -60,7 +59,7 @@ class KafkaIntegrationTestCase(unittest.TestCase):
return
if not self.topic:
topic = "%s-%s" % (self.id()[self.id().rindex(".") + 1:], random_string(10).decode('utf-8'))
topic = "%s-%s" % (self.id()[self.id().rindex(".") + 1:], random_string(10))
self.topic = topic
self.bytes_topic = topic.encode('utf-8')