Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion mobly/controllers/android_device_lib/callback_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,10 +189,11 @@ def waitForEvent(self, event_name, predicate, timeout=DEFAULT_TIMEOUT):
break
if predicate(event):
return event
predicate_name = getattr(predicate, '__name__', repr(predicate))
raise TimeoutError(
self._ad,
'Timed out after %ss waiting for an "%s" event that satisfies the '
'predicate "%s".' % (timeout, event_name, predicate.__name__),
'predicate "%s".' % (timeout, event_name, predicate_name),
)

def getAll(self, event_name):
Expand Down
4 changes: 3 additions & 1 deletion mobly/snippet/callback_handler_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,10 +233,12 @@ def waitForEvent(
return event

custom_error = '' if message is None else f' Details: {message}.'
# Callables like functools.partial objects have no __name__.
predicate_name = getattr(predicate, '__name__', repr(predicate))
raise errors.CallbackHandlerTimeoutError(
self._device,
f'Timed out after {timeout}s waiting for an "{event_name}" event that '
f'satisfies the predicate "{predicate.__name__}".{custom_error}',
f'satisfies the predicate "{predicate_name}".{custom_error}',
)

def getAll(self, event_name):
Expand Down
19 changes: 19 additions & 0 deletions tests/mobly/snippet/callback_handler_base_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# limitations under the License.
"""Unit tests for mobly.snippet.callback_handler_base.CallbackHandlerBase."""

import functools
import unittest
from unittest import mock

Expand Down Expand Up @@ -190,6 +191,24 @@ def some_condition(_):
):
handler.waitForEvent('AsyncTaskResult', some_condition, 0.01)

def test_wait_for_event_negative_with_partial_predicate(self):
handler = FakeCallbackHandler()
handler.mock_rpc_func.callEventWaitAndGetRpc = mock.Mock(
return_value=MOCK_RAW_EVENT
)

def has_secret_number(event, number):
return event.data['secretNumber'] == number

with self.assertRaisesRegex(
errors.CallbackHandlerTimeoutError, 'satisfies the predicate'
):
handler.waitForEvent(
'AsyncTaskResult',
functools.partial(has_secret_number, number=42),
0.01,
)

def test_wait_for_event_max_timeout(self):
"""waitForEvent should not raise the timeout exceed threshold error."""
rpc_max_timeout_sec = 5
Expand Down
Loading