|
| 1 | +# Copyright 2021 - 2024 Universität Tübingen, DKFZ, EMBL, and Universität zu Köln |
| 2 | +# for the German Human Genome-Phenome Archive (GHGA) |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | + |
| 16 | +"""Test to make sure that the DLQ is correctly set up for this service.""" |
| 17 | + |
| 18 | +import pytest |
| 19 | +from hexkit.providers.akafka.testutils import KafkaFixture |
| 20 | + |
| 21 | +from ns.inject import prepare_event_subscriber |
| 22 | +from tests.fixtures.config import get_config |
| 23 | + |
| 24 | +pytestmark = pytest.mark.asyncio() |
| 25 | + |
| 26 | + |
| 27 | +async def test_event_subscriber_dlq(kafka: KafkaFixture): |
| 28 | + """Verify that if we get an error when consuming an event, it gets published to the DLQ.""" |
| 29 | + config = get_config(sources=[kafka.config], kafka_enable_dlq=True) |
| 30 | + assert config.kafka_enable_dlq |
| 31 | + |
| 32 | + # Publish an event with a bogus payload to a topic/type this service expects |
| 33 | + await kafka.publish_event( |
| 34 | + payload={"some_key": "some_value"}, |
| 35 | + type_=config.notification_event_type, |
| 36 | + topic=config.notification_event_topic, |
| 37 | + key="test", |
| 38 | + ) |
| 39 | + async with kafka.record_events(in_topic=config.kafka_dlq_topic) as recorder: |
| 40 | + # Consume the event, which should error and get sent to the DLQ |
| 41 | + async with prepare_event_subscriber(config=config) as event_subscriber: |
| 42 | + await event_subscriber.run(forever=False) |
| 43 | + assert recorder.recorded_events |
| 44 | + assert len(recorder.recorded_events) == 1 |
| 45 | + event = recorder.recorded_events[0] |
| 46 | + assert event.key == "test" |
| 47 | + assert event.payload == {"some_key": "some_value"} |
| 48 | + |
| 49 | + |
| 50 | +async def test_consume_from_retry(kafka: KafkaFixture): |
| 51 | + """Verify that this service will correctly get events from the retry topic""" |
| 52 | + config = get_config(sources=[kafka.config], kafka_enable_dlq=True) |
| 53 | + assert config.kafka_enable_dlq |
| 54 | + |
| 55 | + sample_notification = { |
| 56 | + "recipient_email": "test@example.com", |
| 57 | + "email_cc": ["test2@test.com", "test3@test.com"], |
| 58 | + "email_bcc": ["test4@test.com", "test5@test.com"], |
| 59 | + "subject": "Test123", |
| 60 | + "recipient_name": "Yolanda Martinez", |
| 61 | + "plaintext_body": "Where are you, where are you, Yolanda?", |
| 62 | + } |
| 63 | + |
| 64 | + # Publish an event with a proper payload to a topic/type this service expects |
| 65 | + await kafka.publish_event( |
| 66 | + payload=sample_notification, |
| 67 | + type_=config.notification_event_type, |
| 68 | + topic=config.service_name + "-retry", |
| 69 | + key="test", |
| 70 | + headers={"original_topic": config.notification_event_topic}, |
| 71 | + ) |
| 72 | + |
| 73 | + # Consume the event |
| 74 | + async with prepare_event_subscriber(config=config) as event_subscriber: |
| 75 | + await event_subscriber.run(forever=False) |
0 commit comments