Index: build/android/pylib/utils/timeout_retry_unittest.py |
diff --git a/build/android/pylib/utils/timeout_retry_unittest.py b/build/android/pylib/utils/timeout_retry_unittest.py |
new file mode 100644 |
index 0000000000000000000000000000000000000000..69688321d1d4339abe182186dfba5c7fd94cabdd |
--- /dev/null |
+++ b/build/android/pylib/utils/timeout_retry_unittest.py |
@@ -0,0 +1,55 @@ |
+# Copyright 2013 The Chromium Authors. All rights reserved. |
+# Use of this source code is governed by a BSD-style license that can be |
+# found in the LICENSE file. |
+ |
+"""Unittests for timeout_and_retry.py.""" |
+ |
+import time |
+import unittest |
+ |
+import reraiser_thread |
+import timeout_retry |
+ |
+ |
+class TestException(Exception): |
+ pass |
+ |
+ |
+def _NeverEnding(tries=[0]): |
+ tries[0] += 1 |
+ while True: |
+ pass |
+ |
+ |
+def _CountTries(tries): |
+ tries[0] += 1 |
+ raise TestException |
+ |
+ |
+class TestRun(unittest.TestCase): |
+ """Tests for timeout_retry.Run.""" |
+ |
+ def testRun(self): |
+ self.assertTrue(timeout_retry.Run( |
+ lambda x: x, 30, 3, [True], {})) |
+ |
+ def testTimeout(self): |
+ tries = [0] |
+ start = time.time() |
+ self.assertRaises(reraiser_thread.TimeoutError, |
+ timeout_retry.Run, lambda: _NeverEnding(tries), 0, 3) |
+ 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.
|
+ self.assertEqual(tries[0], 3) |
+ |
+ def testRetries(self): |
+ tries = [0] |
+ self.assertRaises(TestException, |
+ timeout_retry.Run, lambda: _CountTries(tries), 30, 3) |
+ self.assertEqual(tries[0], 3) |
+ |
+ def testReturnValue(self): |
+ self.assertTrue(timeout_retry.Run(lambda: True, 30, 3)) |
+ |
+ |
+if __name__ == '__main__': |
+ unittest.main() |