1
0

test_expiring_cache.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. # Copyright 2017 OpenMarket 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 typing import List, cast
  15. from synapse.util import Clock
  16. from synapse.util.caches.expiringcache import ExpiringCache
  17. from tests.utils import MockClock
  18. from .. import unittest
  19. class ExpiringCacheTestCase(unittest.HomeserverTestCase):
  20. def test_get_set(self) -> None:
  21. clock = MockClock()
  22. cache: ExpiringCache[str, str] = ExpiringCache(
  23. "test", cast(Clock, clock), max_len=1
  24. )
  25. cache["key"] = "value"
  26. self.assertEqual(cache.get("key"), "value")
  27. self.assertEqual(cache["key"], "value")
  28. def test_eviction(self) -> None:
  29. clock = MockClock()
  30. cache: ExpiringCache[str, str] = ExpiringCache(
  31. "test", cast(Clock, clock), max_len=2
  32. )
  33. cache["key"] = "value"
  34. cache["key2"] = "value2"
  35. self.assertEqual(cache.get("key"), "value")
  36. self.assertEqual(cache.get("key2"), "value2")
  37. cache["key3"] = "value3"
  38. self.assertEqual(cache.get("key"), None)
  39. self.assertEqual(cache.get("key2"), "value2")
  40. self.assertEqual(cache.get("key3"), "value3")
  41. def test_iterable_eviction(self) -> None:
  42. clock = MockClock()
  43. cache: ExpiringCache[str, List[int]] = ExpiringCache(
  44. "test", cast(Clock, clock), max_len=5, iterable=True
  45. )
  46. cache["key"] = [1]
  47. cache["key2"] = [2, 3]
  48. cache["key3"] = [4, 5]
  49. self.assertEqual(cache.get("key"), [1])
  50. self.assertEqual(cache.get("key2"), [2, 3])
  51. self.assertEqual(cache.get("key3"), [4, 5])
  52. cache["key4"] = [6, 7]
  53. self.assertEqual(cache.get("key"), None)
  54. self.assertEqual(cache.get("key2"), None)
  55. self.assertEqual(cache.get("key3"), [4, 5])
  56. self.assertEqual(cache.get("key4"), [6, 7])
  57. def test_time_eviction(self) -> None:
  58. clock = MockClock()
  59. cache: ExpiringCache[str, int] = ExpiringCache(
  60. "test", cast(Clock, clock), expiry_ms=1000
  61. )
  62. cache["key"] = 1
  63. clock.advance_time(0.5)
  64. cache["key2"] = 2
  65. self.assertEqual(cache.get("key"), 1)
  66. self.assertEqual(cache.get("key2"), 2)
  67. clock.advance_time(0.9)
  68. self.assertEqual(cache.get("key"), None)
  69. self.assertEqual(cache.get("key2"), 2)
  70. clock.advance_time(1)
  71. self.assertEqual(cache.get("key"), None)
  72. self.assertEqual(cache.get("key2"), None)