Make retry tests independent of actual time
[ganeti-github.git] / test / py / ganeti.utils.retry_unittest.py
1 #!/usr/bin/python
2 #
3
4 # Copyright (C) 2011 Google Inc.
5 #
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 # General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 # 02110-1301, USA.
20
21
22 """Script for testing ganeti.utils.retry"""
23
24 import unittest
25
26 from ganeti import constants
27 from ganeti import errors
28 from ganeti import utils
29
30 import testutils
31
32
33 class TestRetry(testutils.GanetiTestCase):
34 def setUp(self):
35 testutils.GanetiTestCase.setUp(self)
36 self.retries = 0
37 self.called = 0
38 self.time = 1379601882.0
39
40 def _time_fn(self):
41 return self.time
42
43 def _wait_fn(self, delay):
44 self.time += delay
45
46 @staticmethod
47 def _RaiseRetryAgain():
48 raise utils.RetryAgain()
49
50 @staticmethod
51 def _RaiseRetryAgainWithArg(args):
52 raise utils.RetryAgain(*args)
53
54 def _WrongNestedLoop(self):
55 return utils.Retry(self._RaiseRetryAgain, 0.01, 0.02)
56
57 def _RetryAndSucceed(self, retries):
58 if self.retries < retries:
59 self.retries += 1
60 raise utils.RetryAgain()
61 else:
62 return True
63
64 def _SimpleRetryAndSucceed(self, retries):
65 self.called += 1
66 if self.retries < retries:
67 self.retries += 1
68 return False
69 else:
70 return True
71
72 def testRaiseTimeout(self):
73 self.failUnlessRaises(utils.RetryTimeout, utils.Retry,
74 self._RaiseRetryAgain, 0.01, 0.02,
75 wait_fn = self._wait_fn, _time_fn = self._time_fn)
76 self.failUnlessRaises(utils.RetryTimeout, utils.Retry,
77 self._RetryAndSucceed, 0.01, 0, args=[1],
78 wait_fn = self._wait_fn, _time_fn = self._time_fn)
79 self.failUnlessEqual(self.retries, 1)
80
81 def testComplete(self):
82 self.failUnlessEqual(utils.Retry(lambda: True, 0, 1,
83 wait_fn = self._wait_fn,
84 _time_fn = self._time_fn),
85 True)
86 self.failUnlessEqual(utils.Retry(self._RetryAndSucceed, 0, 1, args=[2],
87 wait_fn = self._wait_fn,
88 _time_fn = self._time_fn),
89 True)
90 self.failUnlessEqual(self.retries, 2)
91
92 def testNestedLoop(self):
93 try:
94 self.failUnlessRaises(errors.ProgrammerError, utils.Retry,
95 self._WrongNestedLoop, 0, 1,
96 wait_fn = self._wait_fn, _time_fn = self._time_fn)
97 except utils.RetryTimeout:
98 self.fail("Didn't detect inner loop's exception")
99
100 def testTimeoutArgument(self):
101 retry_arg="my_important_debugging_message"
102 try:
103 utils.Retry(self._RaiseRetryAgainWithArg, 0.01, 0.02, args=[[retry_arg]],
104 wait_fn = self._wait_fn, _time_fn = self._time_fn)
105 except utils.RetryTimeout, err:
106 self.failUnlessEqual(err.args, (retry_arg, ))
107 else:
108 self.fail("Expected timeout didn't happen")
109
110 def testRaiseInnerWithExc(self):
111 retry_arg="my_important_debugging_message"
112 try:
113 try:
114 utils.Retry(self._RaiseRetryAgainWithArg, 0.01, 0.02,
115 args=[[errors.GenericError(retry_arg, retry_arg)]],
116 wait_fn = self._wait_fn, _time_fn = self._time_fn)
117 except utils.RetryTimeout, err:
118 err.RaiseInner()
119 else:
120 self.fail("Expected timeout didn't happen")
121 except errors.GenericError, err:
122 self.failUnlessEqual(err.args, (retry_arg, retry_arg))
123 else:
124 self.fail("Expected GenericError didn't happen")
125
126 def testRaiseInnerWithMsg(self):
127 retry_arg="my_important_debugging_message"
128 try:
129 try:
130 utils.Retry(self._RaiseRetryAgainWithArg, 0.01, 0.02,
131 args=[[retry_arg, retry_arg]],
132 wait_fn = self._wait_fn, _time_fn = self._time_fn)
133 except utils.RetryTimeout, err:
134 err.RaiseInner()
135 else:
136 self.fail("Expected timeout didn't happen")
137 except utils.RetryTimeout, err:
138 self.failUnlessEqual(err.args, (retry_arg, retry_arg))
139 else:
140 self.fail("Expected RetryTimeout didn't happen")
141
142 def testSimpleRetry(self):
143 self.assertFalse(utils.SimpleRetry(True, lambda: False, 0.01, 0.02,
144 wait_fn = self._wait_fn,
145 _time_fn = self._time_fn))
146 self.assertFalse(utils.SimpleRetry(lambda x: x, lambda: False, 0.01, 0.02,
147 wait_fn = self._wait_fn,
148 _time_fn = self._time_fn))
149 self.assertTrue(utils.SimpleRetry(True, lambda: True, 0, 1,
150 wait_fn = self._wait_fn,
151 _time_fn = self._time_fn))
152 self.assertTrue(utils.SimpleRetry(lambda x: x, lambda: True, 0, 1,
153 wait_fn = self._wait_fn,
154 _time_fn = self._time_fn))
155 self.assertTrue(utils.SimpleRetry(True, self._SimpleRetryAndSucceed, 0, 1,
156 args=[1], wait_fn = self._wait_fn,
157 _time_fn = self._time_fn))
158 self.assertEqual(self.retries, 1)
159 self.assertEqual(self.called, 2)
160 self.called = self.retries = 0
161 self.assertTrue(utils.SimpleRetry(True, self._SimpleRetryAndSucceed, 0, 1,
162 args=[2], wait_fn = self._wait_fn,
163 _time_fn = self._time_fn))
164 self.assertEqual(self.called, 3)
165
166
167 if __name__ == "__main__":
168 testutils.GanetiTestProgram()