Nir Soffer has uploaded a new change for review.

Change subject: tests: Improve utils.retry tests
......................................................................

tests: Improve utils.retry tests

We did not had tests for the timeout mechanism.

This patch adds 4 tests, revealing issues in the current code:

- Test that we bail out when the deadline is reached (broken).
- Test the special case when running the operation did not time out, but
  we don't have time for sleep (broken).
- Test the special case when deadline has passed when the operation was
  finished (ok).
- Test the special case of zero timeout (ok).

Change-Id: I62062f36e20d263b1aed64135345d496cb7cfd61
Signed-off-by: Nir Soffer <[email protected]>
---
M tests/utilsTests.py
1 file changed, 81 insertions(+), 1 deletion(-)


  git pull ssh://gerrit.ovirt.org:29418/vdsm refs/changes/99/46399/1

diff --git a/tests/utilsTests.py b/tests/utilsTests.py
index b5b90ad..d213de5 100644
--- a/tests/utilsTests.py
+++ b/tests/utilsTests.py
@@ -40,13 +40,27 @@
 from vdsm import utils
 
 from vmTestsData import VM_STATUS_DUMP
+from monkeypatch import MonkeyPatch
 from testlib import permutations, expandPermutations
 from testlib import VdsmTestCase as TestCaseBase
 from testValidation import checkSudo
-from testValidation import stresstest
+from testValidation import brokentest, stresstest
 from multiprocessing import Process
 
 EXT_SLEEP = "sleep"
+
+
+class FakeTime(object):
+
+    def __init__(self, now):
+        self.now = now
+
+    def __call__(self):
+        return self.now
+
+
+def fake_sleep(seconds):
+    utils.monotonic_time.now += seconds
 
 
 class RetryTests(TestCaseBase):
@@ -72,6 +86,72 @@
         # Make sure we had the proper amount of iterations before failing
         self.assertEquals(counter[0], limit)
 
+    @brokentest("deadline is not respected")
+    @MonkeyPatch(utils, 'monotonic_time', FakeTime(0))
+    @MonkeyPatch(time, 'sleep', fake_sleep)
+    def testTimeoutDeadlineReached(self):
+        # time  action
+        # 0     first attempt
+        # 1     sleep
+        # 2     second attempt
+        # 3     bail out (3 == deadline)
+
+        def operation():
+            time.sleep(1)
+            raise RuntimeError
+
+        self.assertRaises(RuntimeError, utils.retry, operation,
+                          timeout=3, sleep=1)
+        self.assertEqual(utils.monotonic_time.now, 3)
+
+    @brokentest("sleep is not considered in deadline calculation")
+    @MonkeyPatch(utils, 'monotonic_time', FakeTime(0))
+    @MonkeyPatch(time, 'sleep', fake_sleep)
+    def testTimeoutNoTimeForSleep(self):
+        # time  action
+        # 0     first attempt
+        # 1     bail out (1 + 1 == deadline)
+
+        def operation():
+            time.sleep(1)
+            raise RuntimeError
+
+        self.assertRaises(RuntimeError, utils.retry, operation,
+                          timeout=2, sleep=1)
+        self.assertEqual(utils.monotonic_time.now, 1)
+
+    @MonkeyPatch(utils, 'monotonic_time', FakeTime(0))
+    @MonkeyPatch(time, 'sleep', fake_sleep)
+    def testTimeoutSleepOnce(self):
+        # time  action
+        # 0     first attempt
+        # 2     sleep
+        # 3     second attempt
+        # 5     bail out (5 > deadline)
+        counter = [0]
+
+        def operation():
+            time.sleep(2)
+            counter[0] += 1
+            raise RuntimeError
+
+        self.assertRaises(RuntimeError, utils.retry, operation,
+                          timeout=4, sleep=1)
+        self.assertEqual(counter[0], 2)
+        self.assertEqual(utils.monotonic_time.now, 5)
+
+    def testTimeoutZero(self):
+        counter = [0]
+
+        def operation():
+            counter[0] += 1
+            raise RuntimeError
+
+        tries = 10
+        self.assertRaises(RuntimeError, utils.retry, operation,
+                          tries=tries, timeout=0.0, sleep=0.0)
+        self.assertEqual(counter[0], tries)
+
 
 class PidStatTests(TestCaseBase):
     def test(self):


-- 
To view, visit https://gerrit.ovirt.org/46399
To unsubscribe, visit https://gerrit.ovirt.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I62062f36e20d263b1aed64135345d496cb7cfd61
Gerrit-PatchSet: 1
Gerrit-Project: vdsm
Gerrit-Branch: master
Gerrit-Owner: Nir Soffer <[email protected]>
_______________________________________________
vdsm-patches mailing list
[email protected]
https://lists.fedorahosted.org/mailman/listinfo/vdsm-patches

Reply via email to