| OLD | NEW |
| (Empty) | |
| 1 # Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. |
| 4 |
| 5 """ |
| 6 Unit tests for the contents of device_utils.py (mostly DeviceUtils). |
| 7 """ |
| 8 # pylint: disable=W0212 |
| 9 # pylint: disable=W0613 |
| 10 |
| 11 import time |
| 12 import unittest |
| 13 from pylib.device import adb_wrapper |
| 14 from pylib.device import device_utils |
| 15 |
| 16 |
| 17 class DeviceUtilsTest(unittest.TestCase): |
| 18 |
| 19 _decorated_function_called_count = 0 |
| 20 |
| 21 def testFunctionDecoratorSetsTimeout(self): |
| 22 """ Tests that the WithTimeoutAndRetries decorator sets |timeout| |
| 23 appropriately. |
| 24 """ |
| 25 @device_utils.WithTimeoutAndRetries(device_utils._DEFAULT_TIMEOUT, |
| 26 device_utils._DEFAULT_RETRIES) |
| 27 def alwaysReturnTimeoutVal(timeout=None, retries=None): |
| 28 return timeout |
| 29 |
| 30 self.assertEquals(device_utils._DEFAULT_TIMEOUT, alwaysReturnTimeoutVal()) |
| 31 self.assertEquals(8, alwaysReturnTimeoutVal(timeout=8)) |
| 32 |
| 33 def testFunctionDecoratorSetsRetries(self): |
| 34 """ Tests that the WithTimeoutAndRetries decorator sets |retries| |
| 35 appropriately. |
| 36 """ |
| 37 @device_utils.WithTimeoutAndRetries(device_utils._DEFAULT_TIMEOUT, |
| 38 device_utils._DEFAULT_RETRIES) |
| 39 def alwaysReturnRetriesVal(timeout=None, retries=None): |
| 40 return retries |
| 41 |
| 42 self.assertEquals(device_utils._DEFAULT_RETRIES, alwaysReturnRetriesVal()) |
| 43 self.assertEquals(1, alwaysReturnRetriesVal(retries=1)) |
| 44 |
| 45 def testFunctionDecoratorDoesTimeouts(self): |
| 46 """ Tests that the WithTimeoutAndRetries decorator handles the timeout |
| 47 logic. |
| 48 """ |
| 49 DeviceUtilsTest._decorated_function_called_count = 0 |
| 50 @device_utils.WithTimeoutAndRetries(device_utils._DEFAULT_TIMEOUT, |
| 51 device_utils._DEFAULT_RETRIES) |
| 52 def alwaysTimesOut(timeout=None, retries=None): |
| 53 DeviceUtilsTest._decorated_function_called_count += 1 |
| 54 time.sleep(100 * timeout) |
| 55 |
| 56 start_time = time.time() |
| 57 with self.assertRaises(adb_wrapper.CommandTimeoutError): |
| 58 alwaysTimesOut(timeout=1, retries=0) |
| 59 elapsed_time = time.time() - start_time |
| 60 self.assertTrue(elapsed_time >= 1) |
| 61 self.assertEquals(1, DeviceUtilsTest._decorated_function_called_count) |
| 62 |
| 63 def testFunctionDecoratorDoesRetries(self): |
| 64 """ Tests that the WithTimeoutAndRetries decorator handles the retries |
| 65 logic. |
| 66 """ |
| 67 DeviceUtilsTest._decorated_function_called_count = 0 |
| 68 @device_utils.WithTimeoutAndRetries(device_utils._DEFAULT_TIMEOUT, |
| 69 device_utils._DEFAULT_RETRIES) |
| 70 def alwaysRaisesCommandFailedError(timeout=None, retries=None): |
| 71 DeviceUtilsTest._decorated_function_called_count += 1 |
| 72 raise adb_wrapper.CommandFailedError(['testCommand'], |
| 73 'testCommand failed') |
| 74 |
| 75 with self.assertRaises(adb_wrapper.CommandFailedError): |
| 76 alwaysRaisesCommandFailedError(retries=10) |
| 77 self.assertEquals(11, DeviceUtilsTest._decorated_function_called_count) |
| 78 |
| 79 class _MethodDecoratorTestObject(object): |
| 80 """ An object suitable for testing the |
| 81 WithTimeoutAndRetriesFromInstance method decorator. |
| 82 """ |
| 83 |
| 84 def __init__(self, test_case, default_timeout=device_utils._DEFAULT_TIMEOUT, |
| 85 default_retries=device_utils._DEFAULT_RETRIES): |
| 86 self._test_case = test_case |
| 87 self._default_timeout = default_timeout |
| 88 self._default_retries = default_retries |
| 89 self.function_call_counters = { |
| 90 'alwaysRaisesCommandFailedError': 0, |
| 91 'alwaysTimesOut': 0, |
| 92 } |
| 93 |
| 94 # pylint: disable=R0201 |
| 95 |
| 96 @device_utils.WithTimeoutAndRetriesFromInstance |
| 97 def alwaysReturnTimeoutVal(self, timeout=None, retries=None): |
| 98 return timeout |
| 99 |
| 100 @device_utils.WithTimeoutAndRetriesFromInstance |
| 101 def alwaysReturnRetriesVal(self, timeout=None, retries=None): |
| 102 return retries |
| 103 |
| 104 # pylint: enable=R0201 |
| 105 |
| 106 @device_utils.WithTimeoutAndRetriesFromInstance |
| 107 def alwaysTimesOut(self, timeout=None, retries=None): |
| 108 self.function_call_counters['alwaysTimesOut'] += 1 |
| 109 time.sleep(100 * timeout) |
| 110 self._test_case.assertFalse(True, msg='Failed to time out?') |
| 111 |
| 112 @device_utils.WithTimeoutAndRetriesFromInstance |
| 113 def alwaysRaisesCommandFailedError(self, timeout=None, retries=None): |
| 114 self.function_call_counters['alwaysRaisesCommandFailedError'] += 1 |
| 115 raise adb_wrapper.CommandFailedError(['testCommand'], |
| 116 'testCommand failed') |
| 117 |
| 118 def testMethodDecoratorSetsTimeout(self): |
| 119 """ Tests that the WithTimeoutAndRetriesFromInstance decorator sets |
| 120 |timeout| appropriately. |
| 121 """ |
| 122 expected_default_timeout = 37 |
| 123 expected_custom_timeout = 38 |
| 124 test_obj = self._MethodDecoratorTestObject( |
| 125 self, default_timeout=expected_default_timeout) |
| 126 self.assertEquals( |
| 127 expected_default_timeout, test_obj.alwaysReturnTimeoutVal()) |
| 128 self.assertEquals( |
| 129 expected_custom_timeout, |
| 130 test_obj.alwaysReturnTimeoutVal(timeout=expected_custom_timeout)) |
| 131 |
| 132 def testMethodDecoratorSetsRetries(self): |
| 133 """ Tests that the WithTimeoutAndRetriesFromInstance decorator sets |
| 134 |retries| appropriately. |
| 135 """ |
| 136 expected_default_retries = 5 |
| 137 expected_custom_retries = 6 |
| 138 test_obj = self._MethodDecoratorTestObject( |
| 139 self, default_retries=expected_default_retries) |
| 140 self.assertEquals( |
| 141 expected_default_retries, test_obj.alwaysReturnRetriesVal()) |
| 142 self.assertEquals( |
| 143 expected_custom_retries, |
| 144 test_obj.alwaysReturnRetriesVal(retries=expected_custom_retries)) |
| 145 |
| 146 def testMethodDecoratorDoesTimeout(self): |
| 147 """ Tests that the WithTimeoutAndRetriesFromInstance decorator handles |
| 148 the timeout logic. |
| 149 """ |
| 150 test_obj = self._MethodDecoratorTestObject(self) |
| 151 start_time = time.time() |
| 152 with self.assertRaises(adb_wrapper.CommandTimeoutError): |
| 153 test_obj.alwaysTimesOut(timeout=1, retries=0) |
| 154 elapsed_time = time.time() - start_time |
| 155 self.assertTrue(elapsed_time >= 1) |
| 156 self.assertEquals(1, test_obj.function_call_counters['alwaysTimesOut']) |
| 157 |
| 158 def testMethodDecoratorDoesRetries(self): |
| 159 """ Tests that the WithTimeoutAndRetriesFromInstance decorator handles |
| 160 the retries logic. |
| 161 """ |
| 162 test_obj = self._MethodDecoratorTestObject(self) |
| 163 with self.assertRaises(adb_wrapper.CommandFailedError): |
| 164 test_obj.alwaysRaisesCommandFailedError(retries=10) |
| 165 self.assertEquals( |
| 166 11, test_obj.function_call_counters['alwaysRaisesCommandFailedError']) |
| 167 |
| 168 |
| 169 if __name__ == '__main__': |
| 170 unittest.main() |
| 171 |
| OLD | NEW |