test_ttlcache.py 2.8 KB

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