Skip to content
Merged
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
1 change: 1 addition & 0 deletions rabbitmq_amqp_python_client/qpid/proton/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,7 @@ def __init__(
timeout=10,
msg="Connection opened",
)
break

except ConnectionException:
if self.conn is not None:
Expand Down
27 changes: 27 additions & 0 deletions tests/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
from rabbitmq_amqp_python_client.qpid.proton._transport import (
SSLDomain,
)
from rabbitmq_amqp_python_client.qpid.proton._utils import (
BlockingConnection,
)

from .http_requests import (
create_vhost,
Expand Down Expand Up @@ -309,3 +312,27 @@ def test_connection_ssl_explicit_peer_verification() -> None:
)
finally:
os.unlink(ca_path)


def test_multiconnection_breaks_after_first_success() -> None:
mock_container = MagicMock()
mock_container.connect.return_value = MagicMock()
urls = ["amqp://host1:5672", "amqp://host2:5672", "amqp://host3:5672"]

with patch.object(BlockingConnection, "wait"):
BlockingConnection(urls=urls, container=mock_container)

assert mock_container.connect.call_count == 1


def test_multiconnection_tries_next_url_on_failure() -> None:
mock_container = MagicMock()
mock_container.connect.return_value = MagicMock()
urls = ["amqp://host1:5672", "amqp://host2:5672"]

with patch.object(BlockingConnection, "wait") as mock_wait:
# first wait raises (first URL fails), next two succeed (second URL connects)
mock_wait.side_effect = [ConnectionException("refused"), None, None]
BlockingConnection(urls=urls, container=mock_container)

assert mock_container.connect.call_count == 2