Statistics
| Branch: | Tag: | Revision:

root / test / ganeti.cache_unittest.py @ 13699e58

History | View | Annotate | Download (3.6 kB)

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
"""Script for testing ganeti.cache"""
22

    
23
import testutils
24
import unittest
25

    
26
from ganeti import cache
27

    
28

    
29
class ReturnStub:
30
  def __init__(self, values):
31
    self.values = values
32

    
33
  def __call__(self):
34
    assert self.values
35
    return self.values.pop(0)
36

    
37

    
38
class SimpleCacheTest(unittest.TestCase):
39
  def setUp(self):
40
    self.cache = cache.SimpleCache()
41

    
42
  def testNoKey(self):
43
    self.assertEqual(self.cache.GetMulti(["i-dont-exist", "neither-do-i", "no"]),
44
                     [None, None, None])
45

    
46
  def testCache(self):
47
    value = 0xc0ffee
48
    self.assert_(self.cache.Store("i-exist", value))
49
    self.assertEqual(self.cache.GetMulti(["i-exist"]), [value])
50

    
51
  def testMixed(self):
52
    value = 0xb4dc0de
53
    self.assert_(self.cache.Store("i-exist", value))
54
    self.assertEqual(self.cache.GetMulti(["i-exist", "i-dont"]), [value, None])
55

    
56
  def testTtl(self):
57
    my_times = ReturnStub([0, 1, 1, 2, 3, 5])
58
    ttl_cache = cache.SimpleCache(_time_fn=my_times)
59
    self.assert_(ttl_cache.Store("test-expire", 0xdeadbeef, ttl=2))
60

    
61
    # At this point time will return 2, 1 (start) + 2 (ttl) = 3, still valid
62
    self.assertEqual(ttl_cache.Get("test-expire"), 0xdeadbeef)
63

    
64
    # At this point time will return 3, 1 (start) + 2 (ttl) = 3, still valid
65
    self.assertEqual(ttl_cache.Get("test-expire"), 0xdeadbeef)
66

    
67
    # We are at 5, < 3, invalid
68
    self.assertEqual(ttl_cache.Get("test-expire"), None)
69
    self.assertFalse(my_times.values)
70

    
71
  def testCleanup(self):
72
    my_times = ReturnStub([0, 1, 1, 2, 2, 3, 3, 5, 5,
73
                           21 + cache.SimpleCache.CLEANUP_ROUND,
74
                           34 + cache.SimpleCache.CLEANUP_ROUND,
75
                           55 + cache.SimpleCache.CLEANUP_ROUND * 2,
76
                           89 + cache.SimpleCache.CLEANUP_ROUND * 3])
77
    # Index 0
78
    ttl_cache = cache.SimpleCache(_time_fn=my_times)
79
    # Index 1, 2
80
    self.assert_(ttl_cache.Store("foobar", 0x1dea, ttl=6))
81
    # Index 3, 4
82
    self.assert_(ttl_cache.Store("baz", 0xc0dea55, ttl=11))
83
    # Index 6, 7
84
    self.assert_(ttl_cache.Store("long-foobar", "pretty long",
85
                                 ttl=(22 + cache.SimpleCache.CLEANUP_ROUND)))
86
    # Index 7, 8
87
    self.assert_(ttl_cache.Store("foobazbar", "alive forever"))
88

    
89
    self.assertEqual(set(ttl_cache.cache.keys()),
90
                     set(["foobar", "baz", "long-foobar", "foobazbar"]))
91
    ttl_cache.Cleanup()
92
    self.assertEqual(set(ttl_cache.cache.keys()),
93
                     set(["long-foobar", "foobazbar"]))
94
    ttl_cache.Cleanup()
95
    self.assertEqual(set(ttl_cache.cache.keys()),
96
                     set(["long-foobar", "foobazbar"]))
97
    ttl_cache.Cleanup()
98
    self.assertEqual(set(ttl_cache.cache.keys()), set(["foobazbar"]))
99
    ttl_cache.Cleanup()
100
    self.assertEqual(set(ttl_cache.cache.keys()), set(["foobazbar"]))
101

    
102

    
103
if __name__ == "__main__":
104
  testutils.GanetiTestProgram()