Skip to content

Commit

Permalink
Do not raise errors in BaseSession.wait_for_tasks for handled tasks
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
parkouss committed Jun 18, 2015
1 parent efe91aa commit 33a8228
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
22 changes: 17 additions & 5 deletions rcontrol/core.py
Expand Up @@ -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
Expand All @@ -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):
"""
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 = []
Expand Down
4 changes: 3 additions & 1 deletion tests/test_core.py
Expand Up @@ -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):
Expand Down

0 comments on commit 33a8228

Please sign in to comment.