OLD | NEW |
| (Empty) |
1 #!/usr/bin/python | |
2 # Copyright 2013 The Chromium Authors. All rights reserved. | |
3 # Use of this source code is governed by a BSD-style license that can be | |
4 # found in the LICENSE file. | |
5 | |
6 """Unittests for timeout_and_retry.py.""" | |
7 | |
8 import logging | |
9 import time | |
10 import unittest | |
11 | |
12 from devil.utils import reraiser_thread | |
13 from devil.utils import timeout_retry | |
14 | |
15 | |
16 _DEFAULT_TIMEOUT = .1 | |
17 | |
18 | |
19 class TestException(Exception): | |
20 pass | |
21 | |
22 | |
23 def _CountTries(tries): | |
24 tries[0] += 1 | |
25 raise TestException | |
26 | |
27 | |
28 class TestRun(unittest.TestCase): | |
29 """Tests for timeout_retry.Run.""" | |
30 | |
31 def testRun(self): | |
32 self.assertTrue(timeout_retry.Run( | |
33 lambda x: x, 30, 3, [True], {})) | |
34 | |
35 def testTimeout(self): | |
36 tries = [0] | |
37 def _sleep(): | |
38 tries[0] += 1 | |
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) | |
45 | |
46 def testRetries(self): | |
47 tries = [0] | |
48 self.assertRaises( | |
49 TestException, timeout_retry.Run, lambda: _CountTries(tries), | |
50 _DEFAULT_TIMEOUT, 3, error_log_func=logging.debug) | |
51 self.assertEqual(tries[0], 4) | |
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 | |
60 def testReturnValue(self): | |
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)) | |
74 | |
75 | |
76 if __name__ == '__main__': | |
77 unittest.main() | |
OLD | NEW |