Skip to content

Commit 3ba504b

Browse files
committed
Relax dramatiq bootstrap logic
Signed-off-by: Federico Busetti <[email protected]>
1 parent 4dab956 commit 3ba504b

File tree

3 files changed

+19
-18
lines changed

3 files changed

+19
-18
lines changed

src/common/dramatiq.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import logging
2+
13
import orjson
24
from dramatiq import set_broker, set_encoder
35
from dramatiq.broker import Broker
@@ -26,15 +28,17 @@ def decode(self, data: bytes) -> MessageData:
2628

2729

2830
def init_dramatiq(config: AppConfig):
29-
DramatiqInstrumentor().instrument()
3031
broker: Broker
31-
if config.ENVIRONMENT == "test":
32-
broker = StubBroker()
33-
# broker.emit_after("process_boot")
34-
elif config.DRAMATIQ.REDIS_URL is not None:
32+
33+
DramatiqInstrumentor().instrument()
34+
35+
if config.DRAMATIQ.REDIS_URL is not None:
3536
broker = RedisBroker(url=config.DRAMATIQ.REDIS_URL)
3637
else:
37-
raise RuntimeError("Running a non-test environment without Redis URL set")
38+
broker = StubBroker()
39+
# broker.emit_after("process_boot")
40+
if config.ENVIRONMENT != "test":
41+
logging.critical("Running a non-test environment without Redis URL set")
3842
broker.add_middleware(AsyncIO())
3943
set_broker(broker)
4044
set_encoder(ORJSONEncoder())

tests/common/test_dramatiq.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import logging
12
from unittest.mock import MagicMock, patch
23

34
import orjson
@@ -61,12 +62,12 @@ def test_init_dramatiq_with_redis():
6162
assert isinstance(get_encoder(), ORJSONEncoder)
6263

6364

64-
def test_init_dramatiq_without_redis_url():
65+
def test_init_dramatiq_without_redis_url(caplog):
6566
"""Test if an exception is raised when in non-test environment without Redis URL."""
6667
config = AppConfig(
6768
ENVIRONMENT="production", DRAMATIQ=DramatiqConfig(REDIS_URL=None)
6869
) # Mock config
69-
with pytest.raises(
70-
RuntimeError, match="Running a non-test environment without Redis URL set"
71-
):
70+
with caplog.at_level(logging.CRITICAL):
7271
init_dramatiq(config)
72+
73+
assert "Running a non-test environment without Redis URL set" in caplog.text

tests/http_app/test_factory.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
11
from unittest.mock import patch
22

3-
import pytest
4-
53
from common.config import AppConfig
64
from http_app import create_app
75

86

97
def test_with_default_config() -> None:
10-
"""Test create_app fails without passing test config."""
11-
with (
12-
patch("common.bootstrap.init_storage", return_value=None),
13-
pytest.raises(RuntimeError),
14-
):
15-
create_app()
8+
"""Test create_app without passing test config."""
9+
with patch("common.bootstrap.init_storage", return_value=None):
10+
app = create_app()
11+
assert app.debug is False
1612

1713

1814
def test_with_debug_config() -> None:

0 commit comments

Comments
 (0)