test_ttlcache.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. # Copyright 2019 New Vector Ltd
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. from unittest.mock import Mock
  15. from synapse.util.caches.ttlcache import TTLCache
  16. from tests import unittest
  17. class CacheTestCase(unittest.TestCase):
  18. def setUp(self):
  19. self.mock_timer = Mock(side_effect=lambda: 100.0)
  20. self.cache = TTLCache("test_cache", self.mock_timer)
  21. def test_get(self):
  22. """simple set/get tests"""
  23. self.cache.set("one", "1", 10)
  24. self.cache.set("two", "2", 20)
  25. self.cache.set("three", "3", 30)
  26. self.assertEqual(len(self.cache), 3)
  27. self.assertTrue("one" in self.cache)
  28. self.assertEqual(self.cache.get("one"), "1")
  29. self.assertEqual(self.cache["one"], "1")
  30. self.assertEqual(self.cache.get_with_expiry("one"), ("1", 110, 10))
  31. self.assertEqual(self.cache._metrics.hits, 3)
  32. self.assertEqual(self.cache._metrics.misses, 0)
  33. self.cache.set("two", "2.5", 20)
  34. self.assertEqual(self.cache["two"], "2.5")
  35. self.assertEqual(self.cache._metrics.hits, 4)
  36. # non-existent-item tests
  37. self.assertEqual(self.cache.get("four", "4"), "4")
  38. self.assertIs(self.cache.get("four", None), None)
  39. with self.assertRaises(KeyError):
  40. self.cache["four"]
  41. with self.assertRaises(KeyError):
  42. self.cache.get("four")
  43. with self.assertRaises(KeyError):
  44. self.cache.get_with_expiry("four")
  45. self.assertEqual(self.cache._metrics.hits, 4)
  46. self.assertEqual(self.cache._metrics.misses, 5)
  47. def test_expiry(self):
  48. self.cache.set("one", "1", 10)
  49. self.cache.set("two", "2", 20)
  50. self.cache.set("three", "3", 30)
  51. self.assertEqual(len(self.cache), 3)
  52. self.assertEqual(self.cache["one"], "1")
  53. self.assertEqual(self.cache["two"], "2")
  54. # enough for the first entry to expire, but not the rest
  55. self.mock_timer.side_effect = lambda: 110.0
  56. self.assertEqual(len(self.cache), 2)
  57. self.assertFalse("one" in self.cache)
  58. self.assertEqual(self.cache["two"], "2")
  59. self.assertEqual(self.cache["three"], "3")
  60. self.assertEqual(self.cache.get_with_expiry("two"), ("2", 120, 20))
  61. self.assertEqual(self.cache._metrics.hits, 5)
  62. self.assertEqual(self.cache._metrics.misses, 0)