Statistics
| Branch: | Tag: | Revision:

root / test / ganeti.mcpu_unittest.py @ d357f531

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 407339d0 Michael Hanselmann
30 e3200b18 Michael Hanselmann
class TestLockAttemptTimeoutStrategy(unittest.TestCase):
31 407339d0 Michael Hanselmann
  def testConstants(self):
32 e3200b18 Michael Hanselmann
    tpa = mcpu._LockAttemptTimeoutStrategy._TIMEOUT_PER_ATTEMPT
33 e3200b18 Michael Hanselmann
    self.assert_(len(tpa) > 10)
34 e3200b18 Michael Hanselmann
    self.assert_(sum(tpa) >= 150.0)
35 407339d0 Michael Hanselmann
36 407339d0 Michael Hanselmann
  def testSimple(self):
37 e3200b18 Michael Hanselmann
    strat = mcpu._LockAttemptTimeoutStrategy(_random_fn=lambda: 0.5,
38 e3200b18 Michael Hanselmann
                                             _time_fn=lambda: 0.0)
39 407339d0 Michael Hanselmann
40 e3200b18 Michael Hanselmann
    self.assertEqual(strat._attempt, 0)
41 407339d0 Michael Hanselmann
42 407339d0 Michael Hanselmann
    prev = None
43 a6db1af2 Michael Hanselmann
    for i in range(len(mcpu._LockAttemptTimeoutStrategy._TIMEOUT_PER_ATTEMPT)):
44 407339d0 Michael Hanselmann
      timeout = strat.CalcRemainingTimeout()
45 407339d0 Michael Hanselmann
      self.assert_(timeout is not None)
46 407339d0 Michael Hanselmann
47 407339d0 Michael Hanselmann
      self.assert_(timeout <= 10.0)
48 a6db1af2 Michael Hanselmann
      self.assert_(timeout >= 0.0)
49 407339d0 Michael Hanselmann
      self.assert_(prev is None or timeout >= prev)
50 407339d0 Michael Hanselmann
51 e3200b18 Michael Hanselmann
      strat = strat.NextAttempt()
52 a6db1af2 Michael Hanselmann
      self.assertEqual(strat._attempt, i + 1)
53 407339d0 Michael Hanselmann
54 407339d0 Michael Hanselmann
      prev = timeout
55 407339d0 Michael Hanselmann
56 a6db1af2 Michael Hanselmann
    for _ in range(10):
57 a6db1af2 Michael Hanselmann
      self.assert_(strat.CalcRemainingTimeout() is None)
58 407339d0 Michael Hanselmann
59 407339d0 Michael Hanselmann
60 407339d0 Michael Hanselmann
if __name__ == "__main__":
61 407339d0 Michael Hanselmann
  unittest.main()