Python3.5: Make KafkaProducer compatible with Py35
Change-Id: I9727a4aa4a2cabc1d55e6d242980152ef7d60011
This commit is contained in:
parent
f1f1ba90fd
commit
38e3b64d57
@ -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:
|
||||
|
@ -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)
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user