OLD | NEW |
---|---|
(Empty) | |
1 # 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 # found in the LICENSE file. | |
4 | |
5 """Unittests for timeout_and_retry.py.""" | |
6 | |
7 import time | |
8 import unittest | |
9 | |
10 import reraiser_thread | |
11 import timeout_retry | |
12 | |
13 | |
14 class TestException(Exception): | |
15 pass | |
16 | |
17 | |
18 def _NeverEnding(tries=[0]): | |
19 tries[0] += 1 | |
20 while True: | |
21 pass | |
22 | |
23 | |
24 def _CountTries(tries): | |
25 tries[0] += 1 | |
26 raise TestException | |
27 | |
28 | |
29 class TestRun(unittest.TestCase): | |
30 """Tests for timeout_retry.Run.""" | |
31 | |
32 def testRun(self): | |
33 self.assertTrue(timeout_retry.Run( | |
34 lambda x: x, 30, 3, [True], {})) | |
35 | |
36 def testTimeout(self): | |
37 tries = [0] | |
38 start = time.time() | |
39 self.assertRaises(reraiser_thread.TimeoutError, | |
40 timeout_retry.Run, lambda: _NeverEnding(tries), 0, 3) | |
41 self.assertLess(time.time() - start, 2) | |
frankf
2013/11/14 23:26:05
this seems flaky. Is this guranteed to be true und
craigdh
2013/11/15 00:26:30
It is probably theoretically possible it could fai
frankf
2013/11/15 23:08:47
If this is an infinite loop, then it should be suf
craigdh
2013/11/15 23:41:57
Ok, that's reasonable.
| |
42 self.assertEqual(tries[0], 3) | |
43 | |
44 def testRetries(self): | |
45 tries = [0] | |
46 self.assertRaises(TestException, | |
47 timeout_retry.Run, lambda: _CountTries(tries), 30, 3) | |
48 self.assertEqual(tries[0], 3) | |
49 | |
50 def testReturnValue(self): | |
51 self.assertTrue(timeout_retry.Run(lambda: True, 30, 3)) | |
52 | |
53 | |
54 if __name__ == '__main__': | |
55 unittest.main() | |
OLD | NEW |