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