diff --git a/src/aiida/brokers/rabbitmq/broker.py b/src/aiida/brokers/rabbitmq/broker.py index dab19e28b7..5321f6d400 100644 --- a/src/aiida/brokers/rabbitmq/broker.py +++ b/src/aiida/brokers/rabbitmq/broker.py @@ -34,7 +34,10 @@ def __init__(self, profile: Profile) -> None: self._prefix = f'aiida-{self._profile.uuid}' def __str__(self): - return f'RabbitMQ v{self.get_rabbitmq_version()} @ {self.get_url()}' + try: + return f'RabbitMQ v{self.get_rabbitmq_version()} @ {self.get_url()}' + except ConnectionError: + return f'RabbitMQ @ {self.get_url()} ' def close(self): """Close the broker.""" diff --git a/tests/brokers/test_rabbitmq.py b/tests/brokers/test_rabbitmq.py index fc27a3eaf6..00ee662338 100644 --- a/tests/brokers/test_rabbitmq.py +++ b/tests/brokers/test_rabbitmq.py @@ -22,6 +22,19 @@ pytestmark = pytest.mark.requires_rmq +def test_str_method(monkeypatch, manager): + """Test the `__str__` method of the `RabbitmqBroker`.""" + + def raise_connection_error(): + raise ConnectionError + + broker = manager.get_broker() + assert 'RabbitMQ v' in str(broker) + + monkeypatch.setattr(broker, 'get_communicator', raise_connection_error) + assert 'RabbitMQ @' in str(broker) + + @pytest.mark.parametrize( ('version', 'supported'), (