Skip to content

Commit

Permalink
Skip additional RabbitMQ tests if RabbitMQ is unreachable
Browse files Browse the repository at this point in the history
  • Loading branch information
kurtmckee committed Jun 26, 2024
1 parent ba06966 commit 9afe685
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
19 changes: 19 additions & 0 deletions tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
from contextlib import contextmanager

import pika
import pika.exceptions
import pytest

from dramatiq import Worker
from dramatiq.brokers.rabbitmq import RabbitmqBroker
from dramatiq.threading import is_gevent_active


Expand Down Expand Up @@ -33,3 +35,20 @@ def worker(*args, **kwargs):
RABBITMQ_USERNAME = os.getenv("RABBITMQ_USERNAME", "guest")
RABBITMQ_PASSWORD = os.getenv("RABBITMQ_PASSWORD", "guest")
RABBITMQ_CREDENTIALS = pika.credentials.PlainCredentials(RABBITMQ_USERNAME, RABBITMQ_PASSWORD)


def rabbit_mq_is_unreachable():
broker = RabbitmqBroker(
host="127.0.0.1",
max_priority=10,
credentials=RABBITMQ_CREDENTIALS,
)
try:
broker.connection
except pika.exceptions.AMQPConnectionError:
# RabbitMQ is unreachable
return True
return False


skip_unless_rabbit_mq = pytest.mark.skipif(rabbit_mq_is_unreachable(), reason="RabbitMQ is unreachable")
6 changes: 5 additions & 1 deletion tests/test_rabbitmq.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from dramatiq.brokers.rabbitmq import RabbitmqBroker, URLRabbitmqBroker, _IgnoreScaryLogs
from dramatiq.common import current_millis

from .common import RABBITMQ_CREDENTIALS, RABBITMQ_PASSWORD, RABBITMQ_USERNAME
from .common import RABBITMQ_CREDENTIALS, RABBITMQ_PASSWORD, RABBITMQ_USERNAME, skip_unless_rabbit_mq


def test_urlrabbitmq_creates_instances_of_rabbitmq_broker():
Expand All @@ -26,6 +26,7 @@ def test_urlrabbitmq_creates_instances_of_rabbitmq_broker():
assert isinstance(broker, RabbitmqBroker)


@skip_unless_rabbit_mq
def test_rabbitmq_broker_can_be_passed_a_semicolon_separated_list_of_uris():
# Given a string with a list of RabbitMQ connection URIs, including an invalid one
# When I pass those URIs to RabbitMQ broker as a ;-separated string
Expand All @@ -36,6 +37,7 @@ def test_rabbitmq_broker_can_be_passed_a_semicolon_separated_list_of_uris():
assert broker.connection


@skip_unless_rabbit_mq
def test_rabbitmq_broker_can_be_passed_a_list_of_uri_for_failover():
# Given a string with a list of RabbitMQ connection URIs, including an invalid one
# When I pass those URIs to RabbitMQ broker as a list
Expand Down Expand Up @@ -64,6 +66,7 @@ def test_rabbitmq_broker_raises_an_error_if_given_invalid_parameter_combinations
RabbitmqBroker(host="127.0.0.1", parameters=[dict(host="127.0.0.1")])


@skip_unless_rabbit_mq
def test_rabbitmq_broker_can_be_passed_a_list_of_parameters_for_failover():
# Given a list of pika connection parameters including an invalid one
parameters = [
Expand Down Expand Up @@ -217,6 +220,7 @@ def do_work():
assert xq_count == 1


@skip_unless_rabbit_mq
def test_rabbitmq_broker_connections_are_lazy():
# When I create an RMQ broker
broker = RabbitmqBroker(
Expand Down

0 comments on commit 9afe685

Please sign in to comment.