From 33a8228f89f5983d9a3789d906a072d8d3ace203 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Pag=C3=A8s?= Date: Thu, 18 Jun 2015 09:10:49 +0200 Subject: [PATCH] Do not raise errors in BaseSession.wait_for_tasks for handled tasks By default (and only way for now), a handled task is a task that have a on_done callback defined. Such tasks do not report errors when we wait for them from the session, as it is likely that we used that callback to handle the error on our own. --- rcontrol/core.py | 22 +++++++++++++++++----- tests/test_core.py | 4 +++- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/rcontrol/core.py b/rcontrol/core.py index c0417bd..cadab6c 100644 --- a/rcontrol/core.py +++ b/rcontrol/core.py @@ -61,7 +61,8 @@ class Task(object): accessible via the **session** attribute on the instance. :param on_done: if not None, should be a callback that takes the instance task as the parameter. It is called when the task is - done (finished or timed out). + done (finished or timed out). If defined, :meth:`error_handled` + will return True. """ def __init__(self, session, on_done=None): self.session = session @@ -78,6 +79,16 @@ def _unregister(self): if self.__on_done: self.__on_done(self) + def error_handled(self): + """ + Return True if the error must **not** be reported while using + :meth:`BaseSession.wait_for_tasks`. + + By default, the error is handled if **on_done** was specified + in the constructor. + """ + return bool(self.__on_done) + @abc.abstractmethod def is_running(self): """ @@ -171,7 +182,7 @@ def _unregister_task(self, task): except ValueError: pass # this should not happen # keep silent error - if not task.explicit_wait: + if not task.error_handled() and not task.explicit_wait: error = task.error() if error: self._silent_errors.append(error) @@ -220,9 +231,10 @@ def wait_for_tasks(self, raise_if_error=True): break for task in tasks: task.wait(raise_if_error=False) - error = task.error() - if error: - errors.append(error) + if not task.error_handled(): + error = task.error() + if error: + errors.append(error) with self._lock: # now clean the silent errors self._silent_errors = [] diff --git a/tests/test_core.py b/tests/test_core.py index dbb100c..d68a9a5 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -42,7 +42,9 @@ class TestableBaseSession(core.BaseSession): def create_task(**kwargs): - return Mock(spec=core.Task, explicit_wait=False, **kwargs) + kwargs.setdefault('explicit_wait', False) + kwargs.setdefault('error_handled', Mock(return_value=False)) + return Mock(spec=core.Task, **kwargs) class TestBaseSession(unittest.TestCase):