Lee-W commented on code in PR #64612:
URL: https://github.com/apache/airflow/pull/64612#discussion_r3026277154
##########
providers/apache/kafka/src/airflow/providers/apache/kafka/triggers/await_message.py:
##########
@@ -108,6 +108,7 @@ async def run(self):
async_get_consumer = sync_to_async(consumer_hook.get_consumer)
consumer = await async_get_consumer()
+ self._consumer = consumer # store for cleanup
Review Comment:
do we still need `consumer`? or should we just use `self._consumer`?
##########
providers/apache/kafka/tests/unit/apache/kafka/triggers/test_await_message.py:
##########
@@ -142,6 +145,40 @@ async def test_trigger_run_bad(self, mocker):
assert task.done() is False
asyncio.get_event_loop().stop()
+ @pytest.mark.asyncio
+ async def test_cleanup_closes_consumer(self, mocker):
+ consumer = MockedConsumer()
+ close_mock = mocker.patch.object(consumer, "close")
+
+ mocker.patch.object(KafkaConsumerHook, "get_consumer",
return_value=consumer)
+
+ trigger = AwaitMessageTrigger(
+ kafka_config_id="kafka_d",
+
apply_function="unit.apache.kafka.triggers.test_await_message.apply_function_true",
+ topics=["noop"],
+ poll_timeout=0.0001,
+ poll_interval=5,
+ )
+
+ generator = trigger.run()
+ await generator.__anext__()
+ await trigger.cleanup()
+ await generator.aclose()
+
+ close_mock.assert_called_once_with()
+
+ @pytest.mark.asyncio
+ async def test_cleanup_without_consumer_is_noop(self):
Review Comment:
What is this test for?
##########
providers/apache/kafka/tests/unit/apache/kafka/triggers/test_await_message.py:
##########
@@ -142,6 +145,40 @@ async def test_trigger_run_bad(self, mocker):
assert task.done() is False
asyncio.get_event_loop().stop()
+ @pytest.mark.asyncio
+ async def test_cleanup_closes_consumer(self, mocker):
+ consumer = MockedConsumer()
+ close_mock = mocker.patch.object(consumer, "close")
+
+ mocker.patch.object(KafkaConsumerHook, "get_consumer",
return_value=consumer)
+
+ trigger = AwaitMessageTrigger(
+ kafka_config_id="kafka_d",
+
apply_function="unit.apache.kafka.triggers.test_await_message.apply_function_true",
+ topics=["noop"],
+ poll_timeout=0.0001,
+ poll_interval=5,
+ )
+
+ generator = trigger.run()
+ await generator.__anext__()
+ await trigger.cleanup()
+ await generator.aclose()
+
+ close_mock.assert_called_once_with()
Review Comment:
```suggestion
assert close_mock.mock_calls == [
call(...)
]
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]