Commit d0b390e3 authored by lukas.burgey's avatar lukas.burgey
Browse files

Disable the broker if run with pytest

parent cc8b155b
Pipeline #81075 passed with stage
in 1 minute and 7 seconds
import sys
import os
from logging import getLogger from logging import getLogger
from pika import BlockingConnection, ConnectionParameters, BasicProperties from pika import BlockingConnection, ConnectionParameters, BasicProperties
...@@ -27,6 +29,18 @@ class RabbitMQInstance: ...@@ -27,6 +29,18 @@ class RabbitMQInstance:
'services', 'services',
] ]
@property
def _no_broker(self):
""" indicates if this module should do anything.
This is useful during testing, when there may not be a running RabbitMQ server
"""
return (
'pytest' in sys.modules
or getattr(settings, 'NO_BROKER', False)
or os.path.basename(sys.argv[0]) in ('pytest', 'py.test')
)
def _init_exchanges(self, channel): def _init_exchanges(self, channel):
# this is no error # this is no error
for exchange in self.exchanges: for exchange in self.exchanges:
...@@ -66,7 +80,7 @@ class RabbitMQInstance: ...@@ -66,7 +80,7 @@ class RabbitMQInstance:
def _publish(self, exchange, routing_key, body): def _publish(self, exchange, routing_key, body):
# when running CI tests we have no broker, so just do nothing # when running CI tests we have no broker, so just do nothing
if getattr(settings, 'NO_BROKER', False): if self._no_broker:
return return
connection = self._open_connection() connection = self._open_connection()
...@@ -92,7 +106,7 @@ class RabbitMQInstance: ...@@ -92,7 +106,7 @@ class RabbitMQInstance:
# called on client registration to make sure the exchanges exists # called on client registration to make sure the exchanges exists
def initialize(self): def initialize(self):
# when running CI tests we have no broker, so just do nothing # when running CI tests we have no broker, so just do nothing
if getattr(settings, 'NO_BROKER', False): if self._no_broker:
return return
connection = self._open_connection() connection = self._open_connection()
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment