From 6019b3ee06d234422c306352810908ab2108309b Mon Sep 17 00:00:00 2001 From: Edwin Jose Date: Mon, 28 Apr 2025 17:20:53 -0400 Subject: [PATCH] fix: cleanup worker test stability (#7826) * Update test_temp_flow_cleanup.py fix: component cleanup test * [autofix.ci] apply automated fixes * Update test_temp_flow_cleanup.py The test seems so flaky and unpredictable, hence proceeding with a mock patch of langflow.services.task.temp_flow_cleanup.logger * Update test_temp_flow_cleanup.py * [autofix.ci] apply automated fixes --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> --- .../services/tasks/test_temp_flow_cleanup.py | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/src/backend/tests/unit/services/tasks/test_temp_flow_cleanup.py b/src/backend/tests/unit/services/tasks/test_temp_flow_cleanup.py index 5b1b0a8c9..7089830c6 100644 --- a/src/backend/tests/unit/services/tasks/test_temp_flow_cleanup.py +++ b/src/backend/tests/unit/services/tasks/test_temp_flow_cleanup.py @@ -94,16 +94,24 @@ async def test_cleanup_worker_start_stop(): @pytest.mark.asyncio -async def test_cleanup_worker_run_with_exception(caplog): +async def test_cleanup_worker_run_with_exception(mocker): """Test CleanupWorker handles exceptions gracefully.""" + # Mock the logger to capture log calls + mock_logger = mocker.patch("langflow.services.task.temp_flow_cleanup.logger") + settings = get_settings_service().settings settings.public_flow_cleanup_interval = 601 # Minimum valid interval worker = CleanupWorker() - # Start worker and let it run briefly + # Start and immediately stop the worker await worker.start() await worker.stop() - # Check logs for expected messages - assert any("Started database cleanup worker" in record.message for record in caplog.records) - assert any("Stopping database cleanup worker" in record.message for record in caplog.records) + # Verify the worker was started and stopped properly + assert worker._task is None + assert worker._stop_event.is_set() + + # Verify the expected log messages were called + mock_logger.debug.assert_any_call("Started database cleanup worker") + mock_logger.debug.assert_any_call("Stopping database cleanup worker...") + mock_logger.debug.assert_any_call("Database cleanup worker stopped")