| OLD | NEW |
| 1 #!/usr/bin/python |
| 1 # Copyright 2013 The Chromium Authors. All rights reserved. | 2 # Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 4 # found in the LICENSE file. |
| 4 | 5 |
| 5 """Unittests for timeout_and_retry.py.""" | 6 """Unittests for timeout_and_retry.py.""" |
| 6 | 7 |
| 8 import logging |
| 9 import time |
| 7 import unittest | 10 import unittest |
| 8 | 11 |
| 9 from devil.utils import reraiser_thread | 12 from devil.utils import reraiser_thread |
| 10 from devil.utils import timeout_retry | 13 from devil.utils import timeout_retry |
| 11 | 14 |
| 12 | 15 |
| 16 _DEFAULT_TIMEOUT = .1 |
| 17 |
| 18 |
| 13 class TestException(Exception): | 19 class TestException(Exception): |
| 14 pass | 20 pass |
| 15 | 21 |
| 16 | 22 |
| 17 def _NeverEnding(tries): | |
| 18 tries[0] += 1 | |
| 19 while True: | |
| 20 pass | |
| 21 | |
| 22 | |
| 23 def _CountTries(tries): | 23 def _CountTries(tries): |
| 24 tries[0] += 1 | 24 tries[0] += 1 |
| 25 raise TestException | 25 raise TestException |
| 26 | 26 |
| 27 | 27 |
| 28 class TestRun(unittest.TestCase): | 28 class TestRun(unittest.TestCase): |
| 29 """Tests for timeout_retry.Run.""" | 29 """Tests for timeout_retry.Run.""" |
| 30 | 30 |
| 31 def testRun(self): | 31 def testRun(self): |
| 32 self.assertTrue(timeout_retry.Run( | 32 self.assertTrue(timeout_retry.Run( |
| 33 lambda x: x, 30, 3, [True], {})) | 33 lambda x: x, 30, 3, [True], {})) |
| 34 | 34 |
| 35 def testTimeout(self): | 35 def testTimeout(self): |
| 36 tries = [0] | 36 tries = [0] |
| 37 self.assertRaises(reraiser_thread.TimeoutError, | 37 def _sleep(): |
| 38 timeout_retry.Run, lambda: _NeverEnding(tries), 0, 3) | 38 tries[0] += 1 |
| 39 self.assertEqual(tries[0], 4) | 39 time.sleep(1) |
| 40 |
| 41 self.assertRaises( |
| 42 reraiser_thread.TimeoutError, timeout_retry.Run, _sleep, .0001, 1, |
| 43 error_log_func=logging.debug) |
| 44 self.assertEqual(tries[0], 2) |
| 40 | 45 |
| 41 def testRetries(self): | 46 def testRetries(self): |
| 42 tries = [0] | 47 tries = [0] |
| 43 self.assertRaises(TestException, | 48 self.assertRaises( |
| 44 timeout_retry.Run, lambda: _CountTries(tries), 30, 3) | 49 TestException, timeout_retry.Run, lambda: _CountTries(tries), |
| 50 _DEFAULT_TIMEOUT, 3, error_log_func=logging.debug) |
| 45 self.assertEqual(tries[0], 4) | 51 self.assertEqual(tries[0], 4) |
| 46 | 52 |
| 53 def testNoRetries(self): |
| 54 tries = [0] |
| 55 self.assertRaises( |
| 56 TestException, timeout_retry.Run, lambda: _CountTries(tries), |
| 57 _DEFAULT_TIMEOUT, 0, error_log_func=logging.debug) |
| 58 self.assertEqual(tries[0], 1) |
| 59 |
| 47 def testReturnValue(self): | 60 def testReturnValue(self): |
| 48 self.assertTrue(timeout_retry.Run(lambda: True, 30, 3)) | 61 self.assertTrue(timeout_retry.Run(lambda: True, _DEFAULT_TIMEOUT, 3)) |
| 62 |
| 63 def testCurrentTimeoutThreadGroup(self): |
| 64 def InnerFunc(): |
| 65 current_thread_group = timeout_retry.CurrentTimeoutThreadGroup() |
| 66 self.assertIsNotNone(current_thread_group) |
| 67 def InnerInnerFunc(): |
| 68 self.assertEqual(current_thread_group, |
| 69 timeout_retry.CurrentTimeoutThreadGroup()) |
| 70 return True |
| 71 return reraiser_thread.RunAsync((InnerInnerFunc,))[0] |
| 72 |
| 73 self.assertTrue(timeout_retry.Run(InnerFunc, _DEFAULT_TIMEOUT, 3)) |
| 49 | 74 |
| 50 | 75 |
| 51 if __name__ == '__main__': | 76 if __name__ == '__main__': |
| 52 unittest.main() | 77 unittest.main() |
| OLD | NEW |