Repository: google/mobly

Issue:
Better timeout message in CallbackHandler
Right now the `CallbackHandler`'s timeout msg only includes the `callback_id`, which is an implementation detail that doesn't make too much sense for the user. We should add the name of the call where the event was supposed to originate from.


Discussion:

Resolving patch:
diff --git a/mobly/controllers/android_device_lib/callback_handler.py b/mobly/controllers/android_device_lib/callback_handler.py
--- a/mobly/controllers/android_device_lib/callback_handler.py
+++ b/mobly/controllers/android_device_lib/callback_handler.py
@@ -42,10 +42,11 @@ class CallbackHandler(object):
     }
     """
 
-    def __init__(self, callback_id, event_client, ret_value):
+    def __init__(self, callback_id, event_client, ret_value, method_name):
         self._id = callback_id
         self._event_client = event_client
         self.ret_value = ret_value
+        self._method_name = method_name
 
     def waitAndGet(self, event_name, timeout=None):
         """Blocks until an event of the specified name has been received and
@@ -69,8 +70,8 @@ def waitAndGet(self, event_name, timeout=None):
         except Exception as e:
             if "EventSnippetException: timeout." in str(e):
                 raise TimeoutError(
-                    'Timeout waiting for event "%s" of callback %s'
-                    % (event_name, self._id))
+                    'Timeout waiting for event "%s" triggered by %s (%s).'
+                    % (event_name, self._method_name, self._id))
             raise
         return event
 
diff --git a/mobly/controllers/android_device_lib/jsonrpc_client_base.py b/mobly/controllers/android_device_lib/jsonrpc_client_base.py
--- a/mobly/controllers/android_device_lib/jsonrpc_client_base.py
+++ b/mobly/controllers/android_device_lib/jsonrpc_client_base.py
@@ -292,7 +292,10 @@ def _rpc(self, method, *args):
             if self._event_client is None:
                 self._event_client = self._start_event_client()
             return callback_handler.CallbackHandler(
-                result['callback'], self._event_client, result['result'])
+                callback_id=result['callback'],
+                event_client=self._event_client,
+                ret_value=result['result'],
+                method_name=method)
         return result['result']
 
     def _is_app_running(self):


Test patch:
diff --git a/tests/mobly/controllers/android_device_lib/callback_handler_test.py b/tests/mobly/controllers/android_device_lib/callback_handler_test.py
--- a/tests/mobly/controllers/android_device_lib/callback_handler_test.py
+++ b/tests/mobly/controllers/android_device_lib/callback_handler_test.py
@@ -33,7 +33,7 @@ def test_wait_and_get_timeout(self):
         mock_event_client.eventWaitAndGet = mock.Mock(
             side_effect=jsonrpc_client_base.ApiError(java_timeout_msg))
         handler = callback_handler.CallbackHandler(MOCK_CALLBACK_ID,
-                                                   mock_event_client, None)
+                                                   mock_event_client, None, None)
         expected_msg = 'Timeout waiting for event "ha" .*'
         with self.assertRaisesRegexp(callback_handler.TimeoutError,
                                      expected_msg):