From 2ce4d1ff64d0237f5fb60cf8b9ffd0ba30e238fe Mon Sep 17 00:00:00 2001 From: Rafal Jankowski Date: Tue, 3 Sep 2024 18:38:18 +0200 Subject: [PATCH] chore: Flaky test fixed of ErrorsMonitor --- tests/unit/test_errors_monitor.py | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/tests/unit/test_errors_monitor.py b/tests/unit/test_errors_monitor.py index 0a8cf7f8..3b52cd74 100644 --- a/tests/unit/test_errors_monitor.py +++ b/tests/unit/test_errors_monitor.py @@ -1,3 +1,5 @@ +from threading import Event +from typing import Optional from unittest.mock import Mock from neptune_scale.core.components.errors_tracking import ( @@ -10,18 +12,28 @@ def test_errors_monitor(): # given callback = Mock() + # Synchronization event + callback_called = Event() + + # Modify the callback to set the event when called + def callback_with_event(exception: BaseException, last_called: Optional[float]) -> None: + callback(exception, last_called) + callback_called.set() + # and errors_queue = ErrorsQueue() - errors_monitor = ErrorsMonitor(errors_queue=errors_queue, on_error_callback=callback) + errors_monitor = ErrorsMonitor(errors_queue=errors_queue, on_error_callback=callback_with_event) + errors_monitor.start() # when errors_queue.put(ValueError("error1")) errors_queue.flush() - - # and - errors_monitor.start() - errors_monitor.work() errors_monitor.wake_up() + + # then + assert callback_called.wait(timeout=5), "Callback was not called within the timeout" + + # and - cleanup errors_monitor.interrupt() errors_monitor.join(timeout=5)