Skip to content

Commit 18fdddf

Browse files
author
Dana Powers
committed
Add KafkaTimeoutError (used by client.ensure_topic_exists) and add a test
1 parent 5a02d63 commit 18fdddf

File tree

3 files changed

+18
-3
lines changed

3 files changed

+18
-3
lines changed

kafka/client.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88

99
from kafka.common import (TopicAndPartition,
1010
ConnectionError, FailedPayloadsError,
11-
PartitionUnavailableError,
12-
LeaderUnavailableError, KafkaUnavailableError,
11+
PartitionUnavailableError, LeaderUnavailableError, KafkaUnavailableError,
12+
KafkaTimeoutError,
1313
UnknownTopicOrPartitionError, NotLeaderForPartitionError)
1414

1515
from kafka.conn import collect_hosts, KafkaConnection, DEFAULT_SOCKET_TIMEOUT_SECONDS
@@ -219,7 +219,7 @@ def ensure_topic_exists(self, topic, timeout = 30):
219219
self.load_metadata_for_topics(topic)
220220
while not self.has_metadata_for_topic(topic):
221221
if time.time() > start_time + timeout:
222-
raise KafkaTimeoutError("Unable to create topic {}".format(topic))
222+
raise KafkaTimeoutError("Unable to create topic {0}".format(topic))
223223
self.load_metadata_for_topics(topic)
224224
time.sleep(.5)
225225

kafka/common.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,10 @@ class KafkaUnavailableError(KafkaError):
135135
pass
136136

137137

138+
class KafkaTimeoutError(KafkaError):
139+
pass
140+
141+
138142
class LeaderUnavailableError(KafkaError):
139143
pass
140144

test/test_client_integration.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,17 @@ def test_consume_none(self):
4949
messages = list(fetch_resp.messages)
5050
self.assertEquals(len(messages), 0)
5151

52+
@kafka_versions("all")
53+
def test_ensure_topic_exists(self):
54+
55+
# assume that self.topic was created by setUp
56+
# if so, this should succeed
57+
self.client.ensure_topic_exists(self.topic, timeout=1)
58+
59+
# ensure_topic_exists should fail with KafkaTimeoutError
60+
with self.assertRaises(KafkaTimeoutError):
61+
self.client.ensure_topic_exists("this_topic_doesnt_exist", timeout=0)
62+
5263
####################
5364
# Offset Tests #
5465
####################

0 commit comments

Comments
 (0)