Skip to content

Commit f34226d

Browse files
Verifying consumed message
1 parent eb6218f commit f34226d

2 files changed

Lines changed: 21 additions & 3 deletions

File tree

app/consumer/inbound_consumer.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
import asyncio
2+
import json
23
from _asyncio import Task
34

45
import structlog
56
from aio_pika.abc import AbstractIncomingMessage
7+
from fastapi.encoders import jsonable_encoder
8+
9+
from app.model.response.sample import Sample
610
from app.rabbit_mq.rabbit_mq_client import get_connection
711
from app.config import settings
812

@@ -12,10 +16,15 @@ async def process_inbound_message_task(message: AbstractIncomingMessage):
1216
async with message.process(): # Automatically ACKs if no exception occurs
1317
payload_string: str = message.body.decode()
1418
logger.info(
15-
f"Processed inbound message {payload_string}", payload=payload_string
19+
f"Received inbound message {payload_string}", payload=payload_string
20+
)
21+
samples = [Sample(**item) for item in json.loads(payload_string)]
22+
logger.info(
23+
f"Processed inbound message {payload_string}", samples=jsonable_encoder(samples)
1624
)
1725

1826

27+
1928
async def start_inbound_consumer() -> Task[str]:
2029
logger = structlog.get_logger()
2130
# Create a dedicated channel for this consumer group

tests_integration/consumer/test_inbound_consumer.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import pytest
99
import structlog
1010
from assertpy import assert_that
11+
from deepdiff import DeepDiff
1112
from fastapi.encoders import jsonable_encoder
1213
from fastapi.testclient import TestClient
1314
from tenacity import Retrying, stop_after_delay, wait_fixed
@@ -41,9 +42,9 @@ async def test_inbound_consumer_handles_message_successfully(
4142
updated_datetime=datetime.datetime.now(datetime.UTC),
4243
version=1,
4344
)
44-
samples: list[Sample] = [sample1, sample2]
45+
input_samples: list[Sample] = [sample1, sample2]
4546

46-
await publish_to_inbound(samples)
47+
await publish_to_inbound(input_samples)
4748

4849
for attempt in Retrying(
4950
stop=stop_after_delay(5), wait=wait_fixed(0.5), reraise=True
@@ -59,6 +60,14 @@ async def test_inbound_consumer_handles_message_successfully(
5960
)
6061
)
6162
assert_that(inbound_consumer_logs).is_length(1)
63+
consumed_samples: list[dict[str, Any]] = inbound_consumer_logs[0]["samples"]
64+
assert_that(
65+
DeepDiff(
66+
consumed_samples,
67+
jsonable_encoder(input_samples),
68+
ignore_order=True,
69+
)
70+
).is_empty()
6271

6372

6473
async def publish_to_inbound(samples: list[Sample]):

0 commit comments

Comments
 (0)