summaryrefslogtreecommitdiffstats
path: root/src/pybind/mgr/dashboard/tests/test_cache.py
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-21 11:54:28 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-21 11:54:28 +0000
commite6918187568dbd01842d8d1d2c808ce16a894239 (patch)
tree64f88b554b444a49f656b6c656111a145cbbaa28 /src/pybind/mgr/dashboard/tests/test_cache.py
parentInitial commit. (diff)
downloadceph-e6918187568dbd01842d8d1d2c808ce16a894239.tar.xz
ceph-e6918187568dbd01842d8d1d2c808ce16a894239.zip
Adding upstream version 18.2.2.upstream/18.2.2
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/pybind/mgr/dashboard/tests/test_cache.py')
-rw-r--r--src/pybind/mgr/dashboard/tests/test_cache.py48
1 files changed, 48 insertions, 0 deletions
diff --git a/src/pybind/mgr/dashboard/tests/test_cache.py b/src/pybind/mgr/dashboard/tests/test_cache.py
new file mode 100644
index 000000000..f767676c4
--- /dev/null
+++ b/src/pybind/mgr/dashboard/tests/test_cache.py
@@ -0,0 +1,48 @@
+
+import unittest
+
+from ..plugins.ttl_cache import CacheManager, TTLCache
+
+
+class TTLCacheTest(unittest.TestCase):
+ def test_get(self):
+ ref = 'testcache'
+ cache = TTLCache(ref, 30)
+ with self.assertRaises(KeyError):
+ val = cache['foo']
+ cache['foo'] = 'var'
+ val = cache['foo']
+ self.assertEqual(val, 'var')
+ self.assertEqual(cache.hits, 1)
+ self.assertEqual(cache.misses, 1)
+
+ def test_ttl(self):
+ ref = 'testcache'
+ cache = TTLCache(ref, 0.0000001)
+ cache['foo'] = 'var'
+ # pylint: disable=pointless-statement
+ with self.assertRaises(KeyError):
+ cache['foo']
+ self.assertEqual(cache.hits, 0)
+ self.assertEqual(cache.misses, 1)
+ self.assertEqual(cache.expired, 1)
+
+ def test_maxsize_fifo(self):
+ ref = 'testcache'
+ cache = TTLCache(ref, 30, 2)
+ cache['foo0'] = 'var0'
+ cache['foo1'] = 'var1'
+ cache['foo2'] = 'var2'
+ # pylint: disable=pointless-statement
+ with self.assertRaises(KeyError):
+ cache['foo0']
+ self.assertEqual(cache.hits, 0)
+ self.assertEqual(cache.misses, 1)
+
+
+class TTLCacheManagerTest(unittest.TestCase):
+ def test_get(self):
+ ref = 'testcache'
+ cache0 = CacheManager.get(ref)
+ cache1 = CacheManager.get(ref)
+ self.assertEqual(id(cache0), id(cache1))