Python3.5: Make KafkaProducer compatible with Py35

Change-Id: I9727a4aa4a2cabc1d55e6d242980152ef7d60011
This commit is contained in:
Adrian Czarnecki 2018-06-14 13:35:33 +02:00
parent f1f1ba90fd
commit 38e3b64d57
2 changed files with 8 additions and 4 deletions

View File

@ -16,6 +16,8 @@
import logging
import time
from six import PY2
import monasca_common.kafka_lib.client as kafka_client
import monasca_common.kafka_lib.producer as kafka_producer
@ -53,7 +55,8 @@ class KafkaProducer(object):
try:
if key is None:
key = int(time.time() * 1000)
self._producer.send_messages(topic, str(key), *messages)
key = str(key) if PY2 else bytes(str(key), 'utf-8')
self._producer.send_messages(topic, key, *messages)
success = True
except Exception:
if first:

View File

@ -51,19 +51,20 @@ class TestKafkaProducer(base.BaseTestCase):
def test_kafka_producer_publish(self):
topic = FAKE_KAFKA_TOPIC
messages = ['message']
key = 'key'
key = "key"
expected_key = b"key"
self.monasca_kafka_producer.publish(topic, messages, key)
self.producer.send_messages.assert_called_once_with(
topic, key, *messages)
topic, expected_key, *messages)
@mock.patch('monasca_common.kafka.producer.time')
def test_kafka_producer_publish_one_message_without_key(self, mock_time):
topic = FAKE_KAFKA_TOPIC
message = 'not_a_list'
mock_time.time.return_value = 1
expected_key = '1000'
expected_key = b'1000'
self.monasca_kafka_producer.publish(topic, message)