root / test / ganeti.mcpu_unittest.py @ 895eb320
History | View | Annotate | Download (1.8 kB)
1 | 407339d0 | Michael Hanselmann | #!/usr/bin/python
|
---|---|---|---|
2 | 407339d0 | Michael Hanselmann | #
|
3 | 407339d0 | Michael Hanselmann | |
4 | 407339d0 | Michael Hanselmann | # Copyright (C) 2009 Google Inc.
|
5 | 407339d0 | Michael Hanselmann | #
|
6 | 407339d0 | Michael Hanselmann | # This program is free software; you can redistribute it and/or modify
|
7 | 407339d0 | Michael Hanselmann | # it under the terms of the GNU General Public License as published by
|
8 | 407339d0 | Michael Hanselmann | # the Free Software Foundation; either version 2 of the License, or
|
9 | 407339d0 | Michael Hanselmann | # (at your option) any later version.
|
10 | 407339d0 | Michael Hanselmann | #
|
11 | 407339d0 | Michael Hanselmann | # This program is distributed in the hope that it will be useful, but
|
12 | 407339d0 | Michael Hanselmann | # WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 | 407339d0 | Michael Hanselmann | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
14 | 407339d0 | Michael Hanselmann | # General Public License for more details.
|
15 | 407339d0 | Michael Hanselmann | #
|
16 | 407339d0 | Michael Hanselmann | # You should have received a copy of the GNU General Public License
|
17 | 407339d0 | Michael Hanselmann | # along with this program; if not, write to the Free Software
|
18 | 407339d0 | Michael Hanselmann | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
19 | 407339d0 | Michael Hanselmann | # 02110-1301, USA.
|
20 | 407339d0 | Michael Hanselmann | |
21 | 407339d0 | Michael Hanselmann | |
22 | 407339d0 | Michael Hanselmann | """Script for unittesting the mcpu module"""
|
23 | 407339d0 | Michael Hanselmann | |
24 | 407339d0 | Michael Hanselmann | |
25 | 407339d0 | Michael Hanselmann | import unittest |
26 | 407339d0 | Michael Hanselmann | |
27 | 407339d0 | Michael Hanselmann | from ganeti import mcpu |
28 | 407339d0 | Michael Hanselmann | |
29 | 25231ec5 | Michael Hanselmann | import testutils |
30 | 25231ec5 | Michael Hanselmann | |
31 | 407339d0 | Michael Hanselmann | |
32 | e3200b18 | Michael Hanselmann | class TestLockAttemptTimeoutStrategy(unittest.TestCase): |
33 | 407339d0 | Michael Hanselmann | def testConstants(self): |
34 | e3200b18 | Michael Hanselmann | tpa = mcpu._LockAttemptTimeoutStrategy._TIMEOUT_PER_ATTEMPT |
35 | e3200b18 | Michael Hanselmann | self.assert_(len(tpa) > 10) |
36 | e3200b18 | Michael Hanselmann | self.assert_(sum(tpa) >= 150.0) |
37 | 407339d0 | Michael Hanselmann | |
38 | 407339d0 | Michael Hanselmann | def testSimple(self): |
39 | e3200b18 | Michael Hanselmann | strat = mcpu._LockAttemptTimeoutStrategy(_random_fn=lambda: 0.5, |
40 | e3200b18 | Michael Hanselmann | _time_fn=lambda: 0.0) |
41 | 407339d0 | Michael Hanselmann | |
42 | e3200b18 | Michael Hanselmann | self.assertEqual(strat._attempt, 0) |
43 | 407339d0 | Michael Hanselmann | |
44 | 407339d0 | Michael Hanselmann | prev = None
|
45 | a6db1af2 | Michael Hanselmann | for i in range(len(mcpu._LockAttemptTimeoutStrategy._TIMEOUT_PER_ATTEMPT)): |
46 | 407339d0 | Michael Hanselmann | timeout = strat.CalcRemainingTimeout() |
47 | 407339d0 | Michael Hanselmann | self.assert_(timeout is not None) |
48 | 407339d0 | Michael Hanselmann | |
49 | 407339d0 | Michael Hanselmann | self.assert_(timeout <= 10.0) |
50 | a6db1af2 | Michael Hanselmann | self.assert_(timeout >= 0.0) |
51 | 407339d0 | Michael Hanselmann | self.assert_(prev is None or timeout >= prev) |
52 | 407339d0 | Michael Hanselmann | |
53 | e3200b18 | Michael Hanselmann | strat = strat.NextAttempt() |
54 | a6db1af2 | Michael Hanselmann | self.assertEqual(strat._attempt, i + 1) |
55 | 407339d0 | Michael Hanselmann | |
56 | 407339d0 | Michael Hanselmann | prev = timeout |
57 | 407339d0 | Michael Hanselmann | |
58 | a6db1af2 | Michael Hanselmann | for _ in range(10): |
59 | a6db1af2 | Michael Hanselmann | self.assert_(strat.CalcRemainingTimeout() is None) |
60 | 407339d0 | Michael Hanselmann | |
61 | 407339d0 | Michael Hanselmann | |
62 | 407339d0 | Michael Hanselmann | if __name__ == "__main__": |
63 | 25231ec5 | Michael Hanselmann | testutils.GanetiTestProgram() |