OLD | NEW |
1 # Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 """ | 5 """ |
6 Unit tests for decorators.py. | 6 Unit tests for decorators.py. |
7 """ | 7 """ |
8 | 8 |
9 # pylint: disable=W0613 | 9 # pylint: disable=W0613 |
10 | 10 |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
102 | 102 |
103 with self.assertRaises(device_errors.CommandFailedError): | 103 with self.assertRaises(device_errors.CommandFailedError): |
104 alwaysRaisesCommandFailedError() | 104 alwaysRaisesCommandFailedError() |
105 self.assertEquals(11, DecoratorsTest._decorated_function_called_count) | 105 self.assertEquals(11, DecoratorsTest._decorated_function_called_count) |
106 | 106 |
107 DecoratorsTest._decorated_function_called_count = 0 | 107 DecoratorsTest._decorated_function_called_count = 0 |
108 with self.assertRaises(device_errors.CommandFailedError): | 108 with self.assertRaises(device_errors.CommandFailedError): |
109 alwaysRaisesCommandFailedError(retries=5) | 109 alwaysRaisesCommandFailedError(retries=5) |
110 self.assertEquals(6, DecoratorsTest._decorated_function_called_count) | 110 self.assertEquals(6, DecoratorsTest._decorated_function_called_count) |
111 | 111 |
| 112 def testDefaultsFunctionDecoratorPassesValues(self): |
| 113 """Tests that the defaults decorator passes timeout and retries kwargs.""" |
| 114 @decorators.WithTimeoutAndRetriesDefaults(30, 10) |
| 115 def alwaysReturnsTimeouts(timeout=None, retries=None): |
| 116 return timeout |
| 117 |
| 118 self.assertEquals(30, alwaysReturnsTimeouts()) |
| 119 self.assertEquals(120, alwaysReturnsTimeouts(timeout=120)) |
| 120 |
| 121 @decorators.WithTimeoutAndRetriesDefaults(30, 10) |
| 122 def alwaysReturnsRetries(timeout=None, retries=None): |
| 123 return retries |
| 124 |
| 125 self.assertEquals(10, alwaysReturnsRetries()) |
| 126 self.assertEquals(1, alwaysReturnsRetries(retries=1)) |
| 127 |
112 def testExplicitFunctionDecoratorDoesTimeouts(self): | 128 def testExplicitFunctionDecoratorDoesTimeouts(self): |
113 """Tests that the explicit decorator handles timeout logic.""" | 129 """Tests that the explicit decorator handles timeout logic.""" |
114 DecoratorsTest._decorated_function_called_count = 0 | 130 DecoratorsTest._decorated_function_called_count = 0 |
115 @decorators.WithExplicitTimeoutAndRetries(1, 0) | 131 @decorators.WithExplicitTimeoutAndRetries(1, 0) |
116 def alwaysTimesOut(): | 132 def alwaysTimesOut(): |
117 DecoratorsTest._decorated_function_called_count += 1 | 133 DecoratorsTest._decorated_function_called_count += 1 |
118 time.sleep(100) | 134 time.sleep(100) |
119 | 135 |
120 start_time = time.time() | 136 start_time = time.time() |
121 with self.assertRaises(device_errors.CommandTimeoutError): | 137 with self.assertRaises(device_errors.CommandTimeoutError): |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
158 time.sleep(100) | 174 time.sleep(100) |
159 self._test_case.assertFalse(True, msg='Failed to time out?') | 175 self._test_case.assertFalse(True, msg='Failed to time out?') |
160 | 176 |
161 @decorators.WithTimeoutAndRetriesFromInstance( | 177 @decorators.WithTimeoutAndRetriesFromInstance( |
162 'default_timeout', 'default_retries') | 178 'default_timeout', 'default_retries') |
163 def alwaysRaisesCommandFailedError(self, timeout=None, retries=None): | 179 def alwaysRaisesCommandFailedError(self, timeout=None, retries=None): |
164 self.function_call_counters['alwaysRaisesCommandFailedError'] += 1 | 180 self.function_call_counters['alwaysRaisesCommandFailedError'] += 1 |
165 raise device_errors.CommandFailedError(['testCommand'], | 181 raise device_errors.CommandFailedError(['testCommand'], |
166 'testCommand failed') | 182 'testCommand failed') |
167 | 183 |
| 184 # pylint: disable=R0201 |
| 185 |
| 186 @decorators.WithTimeoutAndRetriesFromInstance( |
| 187 'default_timeout', 'default_retries') |
| 188 def alwaysReturnsTimeout(self, timeout=None, retries=None): |
| 189 return timeout |
| 190 |
| 191 @decorators.WithTimeoutAndRetriesFromInstance( |
| 192 'default_timeout', 'default_retries') |
| 193 def alwaysReturnsRetries(self, timeout=None, retries=None): |
| 194 return retries |
| 195 |
| 196 # pylint: enable=R0201 |
| 197 |
168 | 198 |
169 def testMethodDecoratorDoesTimeout(self): | 199 def testMethodDecoratorDoesTimeout(self): |
170 """Tests that the method decorator handles timeout logic.""" | 200 """Tests that the method decorator handles timeout logic.""" |
171 test_obj = self._MethodDecoratorTestObject(self) | 201 test_obj = self._MethodDecoratorTestObject(self) |
172 start_time = time.time() | 202 start_time = time.time() |
173 with self.assertRaises(device_errors.CommandTimeoutError): | 203 with self.assertRaises(device_errors.CommandTimeoutError): |
174 try: | 204 try: |
175 test_obj.alwaysTimesOut(timeout=1, retries=0) | 205 test_obj.alwaysTimesOut(timeout=1, retries=0) |
176 except: | 206 except: |
177 traceback.print_exc() | 207 traceback.print_exc() |
178 raise | 208 raise |
179 elapsed_time = time.time() - start_time | 209 elapsed_time = time.time() - start_time |
180 self.assertTrue(elapsed_time >= 1) | 210 self.assertTrue(elapsed_time >= 1) |
181 self.assertEquals(1, test_obj.function_call_counters['alwaysTimesOut']) | 211 self.assertEquals(1, test_obj.function_call_counters['alwaysTimesOut']) |
182 | 212 |
183 def testMethodDecoratorDoesRetries(self): | 213 def testMethodDecoratorDoesRetries(self): |
184 """ Tests that the method decorator handles retries logic.""" | 214 """Tests that the method decorator handles retries logic.""" |
185 test_obj = self._MethodDecoratorTestObject(self) | 215 test_obj = self._MethodDecoratorTestObject(self) |
186 with self.assertRaises(device_errors.CommandFailedError): | 216 with self.assertRaises(device_errors.CommandFailedError): |
187 try: | 217 try: |
188 test_obj.alwaysRaisesCommandFailedError(retries=10) | 218 test_obj.alwaysRaisesCommandFailedError(retries=10) |
189 except: | 219 except: |
190 traceback.print_exc() | 220 traceback.print_exc() |
191 raise | 221 raise |
192 self.assertEquals( | 222 self.assertEquals( |
193 11, test_obj.function_call_counters['alwaysRaisesCommandFailedError']) | 223 11, test_obj.function_call_counters['alwaysRaisesCommandFailedError']) |
194 | 224 |
| 225 def testMethodDecoratorPassesValues(self): |
| 226 """Tests that the method decorator passes timeout and retries kwargs.""" |
| 227 test_obj = self._MethodDecoratorTestObject( |
| 228 self, default_timeout=42, default_retries=31) |
| 229 self.assertEquals(42, test_obj.alwaysReturnsTimeout()) |
| 230 self.assertEquals(41, test_obj.alwaysReturnsTimeout(timeout=41)) |
| 231 self.assertEquals(31, test_obj.alwaysReturnsRetries()) |
| 232 self.assertEquals(32, test_obj.alwaysReturnsRetries(retries=32)) |
| 233 |
195 | 234 |
196 if __name__ == '__main__': | 235 if __name__ == '__main__': |
197 unittest.main() | 236 unittest.main() |
198 | 237 |
199 | 238 |
OLD | NEW |