test_responsecache.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. # Copyright 2021 The Matrix.org Foundation C.I.C.
  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 synapse.util.caches.response_cache import ResponseCache
  15. from tests.server import get_clock
  16. from tests.unittest import TestCase
  17. class DeferredCacheTestCase(TestCase):
  18. """
  19. A TestCase class for ResponseCache.
  20. The test-case function naming has some logic to it in it's parts, here's some notes about it:
  21. wait: Denotes tests that have an element of "waiting" before its wrapped result becomes available
  22. (Generally these just use .delayed_return instead of .instant_return in it's wrapped call.)
  23. expire: Denotes tests that test expiry after assured existence.
  24. (These have cache with a short timeout_ms=, shorter than will be tested through advancing the clock)
  25. """
  26. def setUp(self):
  27. self.reactor, self.clock = get_clock()
  28. def with_cache(self, name: str, ms: int = 0) -> ResponseCache:
  29. return ResponseCache(self.clock, name, timeout_ms=ms)
  30. @staticmethod
  31. async def instant_return(o: str) -> str:
  32. return o
  33. async def delayed_return(self, o: str) -> str:
  34. await self.clock.sleep(1)
  35. return o
  36. def test_cache_hit(self):
  37. cache = self.with_cache("keeping_cache", ms=9001)
  38. expected_result = "howdy"
  39. wrap_d = cache.wrap(0, self.instant_return, expected_result)
  40. self.assertEqual(
  41. expected_result,
  42. self.successResultOf(wrap_d),
  43. "initial wrap result should be the same",
  44. )
  45. self.assertEqual(
  46. expected_result,
  47. self.successResultOf(cache.get(0)),
  48. "cache should have the result",
  49. )
  50. def test_cache_miss(self):
  51. cache = self.with_cache("trashing_cache", ms=0)
  52. expected_result = "howdy"
  53. wrap_d = cache.wrap(0, self.instant_return, expected_result)
  54. self.assertEqual(
  55. expected_result,
  56. self.successResultOf(wrap_d),
  57. "initial wrap result should be the same",
  58. )
  59. self.assertIsNone(cache.get(0), "cache should not have the result now")
  60. def test_cache_expire(self):
  61. cache = self.with_cache("short_cache", ms=1000)
  62. expected_result = "howdy"
  63. wrap_d = cache.wrap(0, self.instant_return, expected_result)
  64. self.assertEqual(expected_result, self.successResultOf(wrap_d))
  65. self.assertEqual(
  66. expected_result,
  67. self.successResultOf(cache.get(0)),
  68. "cache should still have the result",
  69. )
  70. # cache eviction timer is handled
  71. self.reactor.pump((2,))
  72. self.assertIsNone(cache.get(0), "cache should not have the result now")
  73. def test_cache_wait_hit(self):
  74. cache = self.with_cache("neutral_cache")
  75. expected_result = "howdy"
  76. wrap_d = cache.wrap(0, self.delayed_return, expected_result)
  77. self.assertNoResult(wrap_d)
  78. # function wakes up, returns result
  79. self.reactor.pump((2,))
  80. self.assertEqual(expected_result, self.successResultOf(wrap_d))
  81. def test_cache_wait_expire(self):
  82. cache = self.with_cache("medium_cache", ms=3000)
  83. expected_result = "howdy"
  84. wrap_d = cache.wrap(0, self.delayed_return, expected_result)
  85. self.assertNoResult(wrap_d)
  86. # stop at 1 second to callback cache eviction callLater at that time, then another to set time at 2
  87. self.reactor.pump((1, 1))
  88. self.assertEqual(expected_result, self.successResultOf(wrap_d))
  89. self.assertEqual(
  90. expected_result,
  91. self.successResultOf(cache.get(0)),
  92. "cache should still have the result",
  93. )
  94. # (1 + 1 + 2) > 3.0, cache eviction timer is handled
  95. self.reactor.pump((2,))
  96. self.assertIsNone(cache.get(0), "cache should not have the result now")