Skip to content

Commit 72c25a8

Browse files
authored
Filter out irrelevant logs when checking for expected logs (#983)
Because of some issues with asyncio and caplog, maybe pytest-asyncio, that reports some pending tasks from an unrelated test, we need to filter out the logs from the relevant loggers when checking for the expected logs. Related to #982.
2 parents 2faab71 + 32abab1 commit 72c25a8

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

tests/actor/test_actor.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -206,8 +206,9 @@ async def test_restart_on_unhandled_exception(
206206
restart_limit: The restart limit to use.
207207
caplog: The log capture fixture.
208208
"""
209-
caplog.set_level("DEBUG", logger="frequenz.sdk.actor._actor")
210-
caplog.set_level("DEBUG", logger="frequenz.sdk.actor._run_utils")
209+
relevant_loggers = {"frequenz.sdk.actor._actor", "frequenz.sdk.actor._run_utils"}
210+
for logger in relevant_loggers:
211+
caplog.set_level("DEBUG", logger=logger)
211212

212213
channel: Broadcast[int] = Broadcast(name="channel")
213214

@@ -270,9 +271,14 @@ async def test_restart_on_unhandled_exception(
270271
(*RUN_INFO, "All 1 actor(s) finished."),
271272
]
272273
)
273-
print("expected_log:", expected_log)
274274
print("caplog.record_tuples:", caplog.record_tuples)
275-
assert caplog.record_tuples == expected_log
275+
# This is an ugly hack. There seem to be some issues with asyncio and caplog, maybe
276+
# pytest-asyncio, that reports some pending tasks from an unrelated test when tested
277+
# inside QEMU (suggesting also some timing issue when things run very slow).
278+
filtered_logs = [r for r in caplog.record_tuples if r[0] in relevant_loggers]
279+
print("filtered_logs:", filtered_logs)
280+
print("expected_log:", expected_log)
281+
assert filtered_logs == expected_log
276282

277283

278284
async def test_does_not_restart_on_normal_exit(

0 commit comments

Comments
 (0)