Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion kafka/cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -728,7 +728,11 @@ def collect_hosts(hosts, randomize=True):
result = []
for host_port in hosts:
# ignore leading SECURITY_PROTOCOL:// to mimic java client
host_port = re.sub('^.*://', '', host_port)
host_port = re.sub('^.*://', '', host_port.strip())
# skip empty entries (e.g. a trailing comma), like the java client does;
# otherwise '' resolves to localhost:9092
if not host_port:
continue
host, port, afi = get_ip_port_afi(host_port)
result.append((host, port, afi))

Expand Down
13 changes: 13 additions & 0 deletions test/test_cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,19 @@ def test_collect_hosts__protocol(self):
('fizz.buzz', 5678, socket.AF_UNSPEC),
])

@pytest.mark.parametrize('hosts', [
"localhost:1234,",
"localhost:1234,,localhost",
" localhost:1234 , ",
"localhost:1234,PLAINTEXT://",
['localhost:1234', ' ', ''],
])
def test_collect_hosts__ignores_empty_entries(self, hosts):
# An empty entry would otherwise become ('', 9092), which resolves to localhost
results = collect_hosts(hosts)
assert results
assert all(host == 'localhost' for host, _port, _afi in results)


class TestExpandToCanonicalBootstrapHosts:
def test_expands_multi_ip_host_to_canonical_names(self):
Expand Down