1"""Utility functions for handling async events.
2"""
3
4
5from mobly import asserts
6from mobly.controllers.android_device_lib import callback_handler
7
8
9def AssertAsyncSuccess(callback):
10  """Wrapping the result of an asynchronous Rpc for convenience.
11
12  This wrapper handles the event posted by the corresponding `FutureCallback`
13  object signalling whether the async operation was successful or not with
14  `onSuccess` and `onFailure` callbacks.
15
16  Args:
17    callback: CallbackHandler, the handler that's used to poll events triggered
18    by the async Rpc call.
19
20  Returns:
21    dictionary, the event that carries the onSuccess signal.
22  """
23  try:
24    status_event = callback.waitAndGet('onSuccess', 30)
25  except callback_handler.TimeoutError:
26    try:
27      failure_event = callback.waitAndGet('onFailure', 30)
28      asserts.fail('Operation failed: %s' % failure_event.data)
29    except callback_handler.TimeoutError:
30      asserts.fail('Timed out waiting for status event.')
31  return status_event
32
33