summaryrefslogtreecommitdiff
path: root/test/test_conn.py
diff options
context:
space:
mode:
authorEvan Bender <evan.bender@percolate.com>2016-08-23 11:47:13 -0400
committerDana Powers <dana.powers@gmail.com>2016-11-20 15:15:14 -0800
commitcbe8a6a2ee9c3a054a7bbfeebc4d5f6b6c892943 (patch)
treead462fcfd81933aad794d484449dd92cf09df501 /test/test_conn.py
parentc4a6e1aa68fc48dd589ff64e1247d2886ccfa3fd (diff)
downloadkafka-python-cbe8a6a2ee9c3a054a7bbfeebc4d5f6b6c892943.tar.gz
When hostname lookup is necessary, do every connect (#812)
Fixes a bug where lookup was done only once for the whole life of the process -- if a broker's IP changed, client couldn't reconnect.
Diffstat (limited to 'test/test_conn.py')
-rw-r--r--test/test_conn.py28
1 files changed, 28 insertions, 0 deletions
diff --git a/test/test_conn.py b/test/test_conn.py
index 4f2b12f..c3e40c0 100644
--- a/test/test_conn.py
+++ b/test/test_conn.py
@@ -5,6 +5,7 @@ from errno import EALREADY, EINPROGRESS, EISCONN, ECONNRESET
import socket
import time
+import mock
import pytest
from kafka.conn import BrokerConnection, ConnectionStates, collect_hosts
@@ -264,3 +265,30 @@ def test_collect_hosts__with_spaces():
('localhost', 1234, socket.AF_UNSPEC),
('localhost', 9092, socket.AF_UNSPEC),
])
+
+
+def test_lookup_on_connect():
+ hostname = 'example.org'
+ port = 9092
+ conn = BrokerConnection(hostname, port, socket.AF_UNSPEC)
+ assert conn.host == conn.hostname == hostname
+ ip1 = '127.0.0.1'
+ mock_return1 = [
+ (2, 2, 17, '', (ip1, 9092)),
+ ]
+ with mock.patch("socket.getaddrinfo", return_value=mock_return1) as m:
+ conn.connect()
+ m.assert_called_once_with(hostname, port, 0, 1)
+ conn.close()
+ assert conn.host == ip1
+
+ ip2 = '127.0.0.2'
+ mock_return2 = [
+ (2, 2, 17, '', (ip2, 9092)),
+ ]
+
+ with mock.patch("socket.getaddrinfo", return_value=mock_return2) as m:
+ conn.connect()
+ m.assert_called_once_with(hostname, port, 0, 1)
+ conn.close()
+ assert conn.host == ip2