Index: build/android/pylib/device/decorators_test.py |
diff --git a/build/android/pylib/device/decorators_test.py b/build/android/pylib/device/decorators_test.py |
index 53bb281401958b152967ff5799028e56e3c4244a..08694135a22f39a3ade30fc6bfe55dcc2e75c8e0 100644 |
--- a/build/android/pylib/device/decorators_test.py |
+++ b/build/android/pylib/device/decorators_test.py |
@@ -8,12 +8,22 @@ Unit tests for decorators.py. |
# pylint: disable=W0613 |
+import os |
+import sys |
import time |
import traceback |
import unittest |
+from pylib import constants |
from pylib.device import decorators |
from pylib.device import device_errors |
+from pylib.utils import reraiser_thread |
+ |
+# TODO(jbudorick) Remove once the DeviceUtils implementations are no longer |
+# backed by AndroidCommands / android_testrunner. |
+sys.path.append(os.path.join(constants.DIR_SOURCE_ROOT, 'third_party', |
+ 'android_testrunner')) |
+import errors as old_errors |
_DEFAULT_TIMEOUT = 30 |
_DEFAULT_RETRIES = 3 |
@@ -69,6 +79,39 @@ class DecoratorsTest(unittest.TestCase): |
self.assertEquals(expected_timeout, actual_timeout) |
self.assertEquals(expected_retries, actual_retries) |
+ def testFunctionDecoratorTranslatesOldExceptions(self): |
+ """Tests that the explicit decorator translates old exceptions.""" |
+ @decorators.WithTimeoutAndRetries |
+ def alwaysRaisesProvidedException(exception, timeout=None, retries=None): |
+ raise exception |
+ |
+ exception_desc = 'Old response timeout error' |
+ with self.assertRaises(device_errors.CommandTimeoutError) as e: |
+ alwaysRaisesProvidedException( |
+ old_errors.WaitForResponseTimedOutError(exception_desc), |
+ timeout=10, retries=1) |
+ self.assertEquals(exception_desc, str(e.exception)) |
+ |
+ exception_desc = 'Old device error' |
+ with self.assertRaises(device_errors.DeviceUnreachableError) as e: |
+ alwaysRaisesProvidedException( |
+ old_errors.DeviceUnresponsiveError(exception_desc), |
+ timeout=10, retries=1) |
+ self.assertEquals(exception_desc, str(e.exception)) |
+ |
+ def testFunctionDecoratorTranslatesReraiserExceptions(self): |
+ """Tests that the explicit decorator translates reraiser exceptions.""" |
+ @decorators.WithTimeoutAndRetries |
+ def alwaysRaisesProvidedException(exception, timeout=None, retries=None): |
+ raise exception |
+ |
+ exception_desc = 'Reraiser thread timeout error' |
+ with self.assertRaises(device_errors.CommandTimeoutError) as e: |
+ alwaysRaisesProvidedException( |
+ reraiser_thread.TimeoutError(exception_desc), |
+ timeout=10, retries=1) |
+ self.assertEquals(exception_desc, str(e.exception)) |
+ |
def testDefaultsFunctionDecoratorDoesTimeouts(self): |
"""Tests that the defaults decorator handles timeout logic.""" |
DecoratorsTest._decorated_function_called_count = 0 |
@@ -109,6 +152,52 @@ class DecoratorsTest(unittest.TestCase): |
alwaysRaisesCommandFailedError(retries=5) |
self.assertEquals(6, DecoratorsTest._decorated_function_called_count) |
+ def testDefaultsFunctionDecoratorPassesValues(self): |
+ """Tests that the defaults decorator passes timeout and retries kwargs.""" |
+ @decorators.WithTimeoutAndRetriesDefaults(30, 10) |
+ def alwaysReturnsTimeouts(timeout=None, retries=None): |
+ return timeout |
+ |
+ self.assertEquals(30, alwaysReturnsTimeouts()) |
+ self.assertEquals(120, alwaysReturnsTimeouts(timeout=120)) |
+ |
+ @decorators.WithTimeoutAndRetriesDefaults(30, 10) |
+ def alwaysReturnsRetries(timeout=None, retries=None): |
+ return retries |
+ |
+ self.assertEquals(10, alwaysReturnsRetries()) |
+ self.assertEquals(1, alwaysReturnsRetries(retries=1)) |
+ |
+ def testDefaultsFunctionDecoratorTranslatesOldExceptions(self): |
+ """Tests that the explicit decorator translates old exceptions.""" |
+ @decorators.WithTimeoutAndRetriesDefaults(30, 10) |
+ def alwaysRaisesProvidedException(exception, timeout=None, retries=None): |
+ raise exception |
+ |
+ exception_desc = 'Old response timeout error' |
+ with self.assertRaises(device_errors.CommandTimeoutError) as e: |
+ alwaysRaisesProvidedException( |
+ old_errors.WaitForResponseTimedOutError(exception_desc)) |
+ self.assertEquals(exception_desc, str(e.exception)) |
+ |
+ exception_desc = 'Old device error' |
+ with self.assertRaises(device_errors.DeviceUnreachableError) as e: |
+ alwaysRaisesProvidedException( |
+ old_errors.DeviceUnresponsiveError(exception_desc)) |
+ self.assertEquals(exception_desc, str(e.exception)) |
+ |
+ def testDefaultsFunctionDecoratorTranslatesReraiserExceptions(self): |
+ """Tests that the explicit decorator translates reraiser exceptions.""" |
+ @decorators.WithTimeoutAndRetriesDefaults(30, 10) |
+ def alwaysRaisesProvidedException(exception, timeout=None, retries=None): |
+ raise exception |
+ |
+ exception_desc = 'Reraiser thread timeout error' |
+ with self.assertRaises(device_errors.CommandTimeoutError) as e: |
+ alwaysRaisesProvidedException( |
+ reraiser_thread.TimeoutError(exception_desc)) |
+ self.assertEquals(exception_desc, str(e.exception)) |
+ |
def testExplicitFunctionDecoratorDoesTimeouts(self): |
"""Tests that the explicit decorator handles timeout logic.""" |
DecoratorsTest._decorated_function_called_count = 0 |
@@ -137,6 +226,36 @@ class DecoratorsTest(unittest.TestCase): |
alwaysRaisesCommandFailedError() |
self.assertEquals(11, DecoratorsTest._decorated_function_called_count) |
+ def testExplicitDecoratorTranslatesOldExceptions(self): |
+ """Tests that the explicit decorator translates old exceptions.""" |
+ @decorators.WithExplicitTimeoutAndRetries(30, 10) |
+ def alwaysRaisesProvidedException(exception): |
+ raise exception |
+ |
+ exception_desc = 'Old response timeout error' |
+ with self.assertRaises(device_errors.CommandTimeoutError) as e: |
+ alwaysRaisesProvidedException( |
+ old_errors.WaitForResponseTimedOutError(exception_desc)) |
+ self.assertEquals(exception_desc, str(e.exception)) |
+ |
+ exception_desc = 'Old device error' |
+ with self.assertRaises(device_errors.DeviceUnreachableError) as e: |
+ alwaysRaisesProvidedException( |
+ old_errors.DeviceUnresponsiveError(exception_desc)) |
+ self.assertEquals(exception_desc, str(e.exception)) |
+ |
+ def testExplicitDecoratorTranslatesReraiserExceptions(self): |
+ """Tests that the explicit decorator translates reraiser exceptions.""" |
+ @decorators.WithExplicitTimeoutAndRetries(30, 10) |
+ def alwaysRaisesProvidedException(exception): |
+ raise exception |
+ |
+ exception_desc = 'Reraiser thread timeout error' |
+ with self.assertRaises(device_errors.CommandTimeoutError) as e: |
+ alwaysRaisesProvidedException( |
+ reraiser_thread.TimeoutError(exception_desc)) |
+ self.assertEquals(exception_desc, str(e.exception)) |
+ |
class _MethodDecoratorTestObject(object): |
"""An object suitable for testing the method decorator.""" |
@@ -165,6 +284,26 @@ class DecoratorsTest(unittest.TestCase): |
raise device_errors.CommandFailedError(['testCommand'], |
'testCommand failed') |
+ # pylint: disable=R0201 |
+ |
+ @decorators.WithTimeoutAndRetriesFromInstance( |
+ 'default_timeout', 'default_retries') |
+ def alwaysReturnsTimeout(self, timeout=None, retries=None): |
+ return timeout |
+ |
+ @decorators.WithTimeoutAndRetriesFromInstance( |
+ 'default_timeout', 'default_retries') |
+ def alwaysReturnsRetries(self, timeout=None, retries=None): |
+ return retries |
+ |
+ @decorators.WithTimeoutAndRetriesFromInstance( |
+ 'default_timeout', 'default_retries') |
+ def alwaysRaisesProvidedException(self, exception, timeout=None, |
+ retries=None): |
+ raise exception |
+ |
+ # pylint: enable=R0201 |
+ |
def testMethodDecoratorDoesTimeout(self): |
"""Tests that the method decorator handles timeout logic.""" |
@@ -181,7 +320,7 @@ class DecoratorsTest(unittest.TestCase): |
self.assertEquals(1, test_obj.function_call_counters['alwaysTimesOut']) |
def testMethodDecoratorDoesRetries(self): |
- """ Tests that the method decorator handles retries logic.""" |
+ """Tests that the method decorator handles retries logic.""" |
test_obj = self._MethodDecoratorTestObject(self) |
with self.assertRaises(device_errors.CommandFailedError): |
try: |
@@ -192,8 +331,39 @@ class DecoratorsTest(unittest.TestCase): |
self.assertEquals( |
11, test_obj.function_call_counters['alwaysRaisesCommandFailedError']) |
+ def testMethodDecoratorPassesValues(self): |
+ """Tests that the method decorator passes timeout and retries kwargs.""" |
+ test_obj = self._MethodDecoratorTestObject( |
+ self, default_timeout=42, default_retries=31) |
+ self.assertEquals(42, test_obj.alwaysReturnsTimeout()) |
+ self.assertEquals(41, test_obj.alwaysReturnsTimeout(timeout=41)) |
+ self.assertEquals(31, test_obj.alwaysReturnsRetries()) |
+ self.assertEquals(32, test_obj.alwaysReturnsRetries(retries=32)) |
-if __name__ == '__main__': |
- unittest.main() |
+ def testMethodDecoratorTranslatesOldExceptions(self): |
+ test_obj = self._MethodDecoratorTestObject(self) |
+ |
+ exception_desc = 'Old response timeout error' |
+ with self.assertRaises(device_errors.CommandTimeoutError) as e: |
+ test_obj.alwaysRaisesProvidedException( |
+ old_errors.WaitForResponseTimedOutError(exception_desc)) |
+ self.assertEquals(exception_desc, str(e.exception)) |
+ |
+ exception_desc = 'Old device error' |
+ with self.assertRaises(device_errors.DeviceUnreachableError) as e: |
+ test_obj.alwaysRaisesProvidedException( |
+ old_errors.DeviceUnresponsiveError(exception_desc)) |
+ self.assertEquals(exception_desc, str(e.exception)) |
+ def testMethodDecoratorTranslatesReraiserExceptions(self): |
+ test_obj = self._MethodDecoratorTestObject(self) |
+ |
+ exception_desc = 'Reraiser thread timeout error' |
+ with self.assertRaises(device_errors.CommandTimeoutError) as e: |
+ test_obj.alwaysRaisesProvidedException( |
+ reraiser_thread.TimeoutError(exception_desc)) |
+ self.assertEquals(exception_desc, str(e.exception)) |
+ |
+if __name__ == '__main__': |
+ unittest.main(verbosity=2) |