SIENTIAPDE-1205

Enhance unit tests for async shutdown and improve assertions

- Updated `test_app.py` to verify multiple calls to `asyncio_sleep` and ensure proper handling of sleep intervals.
- Added new tests in `test_ingestor.py` and `test_opc_manager.py` to validate shutdown behavior and error handling for async operations.
- Improved assertions in existing tests to enhance reliability and clarity of test outcomes.
This commit is contained in:
vitor-aignosi
2025-08-28 08:34:30 -03:00
parent f55604c7db
commit 102507d815
3 changed files with 45 additions and 6 deletions

View File

@@ -127,8 +127,8 @@ async def test_main_successful_run_one_loop(mock_app_env, capsys):
app.metrics.APP_LOOP_COUNT.labels.assert_called_with(pod_id="test_pod")
mock_app_env["metrics_APP_LOOP_COUNT_labels_inc"].assert_called_once()
mock_app_env["asyncio_sleep"].assert_called_once_with(
mock_ingestor_instance.poll_interval)
mock_app_env["asyncio_sleep"].assert_has_calls(
[call(mock_ingestor_instance.poll_interval), call(5)])
app.metrics.APP_LOOP_DURATION.labels.assert_called_with(pod_id="test_pod")
mock_app_env["metrics_APP_LOOP_DURATION_labels_observe"].assert_called_once_with(
@@ -136,7 +136,6 @@ async def test_main_successful_run_one_loop(mock_app_env, capsys):
)
mock_ingestor_instance.shutdown.assert_called_once()
mock_app_env["asyncio_sleep"].assert_any_call(5)
mock_ingestor_instance.logger.info.assert_any_call(
"Main loop exit_signaled.")
@@ -255,7 +254,7 @@ async def test_main_multiple_loop_iterations(mock_app_env):
) # Checks last call or any call
assert mock_app_env["metrics_APP_LOOP_COUNT_labels_inc"].call_count == 3
assert mock_app_env["asyncio_sleep"].call_count == 3
assert mock_app_env["asyncio_sleep"].call_count == 4
assert app.metrics.APP_LOOP_DURATION.labels.call_count == 3
app.metrics.APP_LOOP_DURATION.labels.assert_called_with(pod_id="test_pod")
@@ -299,8 +298,10 @@ async def test_run_async_main(mock_app_env, capsys):
mock_exit_signal.is_set.side_effect = [False, True]
mock_app_env["time_time"].side_effect = [10.0, 11.0]
# Instead of calling run_async_main() which creates a new event loop,
# we test the main() function directly since that's what run_async_main() would call
with pytest.raises(OsExitCalled) as excinfo:
app.run_async_main()
await app.main()
assert excinfo.value.code == 0
# Verify the main function was called through the event loop