0
0
Fork 1
mirror of https://mau.dev/maunium/synapse.git synced 2024-11-12 04:52:26 +01:00

Reset global cache state before cache tests. (#11036)

This reverts #11019 and structures the code a bit more like it was before #10985.

The global cache state must be reset before running the tests since other test
cases might have configured caching (and thus touched the global state).
This commit is contained in:
Patrick Cloke 2021-10-12 08:55:33 -04:00 committed by GitHub
parent a5871f53ed
commit 5c35074d85
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 15 deletions

1
changelog.d/11036.misc Normal file
View file

@ -0,0 +1 @@
Ensure that cache config tests do not share state.

View file

@ -12,25 +12,23 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
from unittest.mock import patch
from synapse.config.cache import CacheConfig, add_resizable_cache from synapse.config.cache import CacheConfig, add_resizable_cache
from synapse.util.caches.lrucache import LruCache from synapse.util.caches.lrucache import LruCache
from tests.unittest import TestCase from tests.unittest import TestCase
# Patch the global _CACHES so that each test runs against its own state.
@patch("synapse.config.cache._CACHES", new_callable=dict)
class CacheConfigTests(TestCase): class CacheConfigTests(TestCase):
def setUp(self): def setUp(self):
# Reset caches before each test # Reset caches before each test since there's global state involved.
self.config = CacheConfig() self.config = CacheConfig()
def tearDown(self):
self.config.reset() self.config.reset()
def test_individual_caches_from_environ(self, _caches): def tearDown(self):
# Also reset the caches after each test to leave state pristine.
self.config.reset()
def test_individual_caches_from_environ(self):
""" """
Individual cache factors will be loaded from the environment. Individual cache factors will be loaded from the environment.
""" """
@ -43,7 +41,7 @@ class CacheConfigTests(TestCase):
self.assertEqual(dict(self.config.cache_factors), {"something_or_other": 2.0}) self.assertEqual(dict(self.config.cache_factors), {"something_or_other": 2.0})
def test_config_overrides_environ(self, _caches): def test_config_overrides_environ(self):
""" """
Individual cache factors defined in the environment will take precedence Individual cache factors defined in the environment will take precedence
over those in the config. over those in the config.
@ -60,7 +58,7 @@ class CacheConfigTests(TestCase):
{"foo": 1.0, "bar": 3.0, "something_or_other": 2.0}, {"foo": 1.0, "bar": 3.0, "something_or_other": 2.0},
) )
def test_individual_instantiated_before_config_load(self, _caches): def test_individual_instantiated_before_config_load(self):
""" """
If a cache is instantiated before the config is read, it will be given If a cache is instantiated before the config is read, it will be given
the default cache size in the interim, and then resized once the config the default cache size in the interim, and then resized once the config
@ -76,7 +74,7 @@ class CacheConfigTests(TestCase):
self.assertEqual(cache.max_size, 300) self.assertEqual(cache.max_size, 300)
def test_individual_instantiated_after_config_load(self, _caches): def test_individual_instantiated_after_config_load(self):
""" """
If a cache is instantiated after the config is read, it will be If a cache is instantiated after the config is read, it will be
immediately resized to the correct size given the per_cache_factor if immediately resized to the correct size given the per_cache_factor if
@ -89,7 +87,7 @@ class CacheConfigTests(TestCase):
add_resizable_cache("foo", cache_resize_callback=cache.set_cache_factor) add_resizable_cache("foo", cache_resize_callback=cache.set_cache_factor)
self.assertEqual(cache.max_size, 200) self.assertEqual(cache.max_size, 200)
def test_global_instantiated_before_config_load(self, _caches): def test_global_instantiated_before_config_load(self):
""" """
If a cache is instantiated before the config is read, it will be given If a cache is instantiated before the config is read, it will be given
the default cache size in the interim, and then resized to the new the default cache size in the interim, and then resized to the new
@ -104,7 +102,7 @@ class CacheConfigTests(TestCase):
self.assertEqual(cache.max_size, 400) self.assertEqual(cache.max_size, 400)
def test_global_instantiated_after_config_load(self, _caches): def test_global_instantiated_after_config_load(self):
""" """
If a cache is instantiated after the config is read, it will be If a cache is instantiated after the config is read, it will be
immediately resized to the correct size given the global factor if there immediately resized to the correct size given the global factor if there
@ -117,7 +115,7 @@ class CacheConfigTests(TestCase):
add_resizable_cache("foo", cache_resize_callback=cache.set_cache_factor) add_resizable_cache("foo", cache_resize_callback=cache.set_cache_factor)
self.assertEqual(cache.max_size, 150) self.assertEqual(cache.max_size, 150)
def test_cache_with_asterisk_in_name(self, _caches): def test_cache_with_asterisk_in_name(self):
"""Some caches have asterisks in their name, test that they are set correctly.""" """Some caches have asterisks in their name, test that they are set correctly."""
config = { config = {
@ -143,7 +141,7 @@ class CacheConfigTests(TestCase):
add_resizable_cache("*cache_c*", cache_resize_callback=cache_c.set_cache_factor) add_resizable_cache("*cache_c*", cache_resize_callback=cache_c.set_cache_factor)
self.assertEqual(cache_c.max_size, 200) self.assertEqual(cache_c.max_size, 200)
def test_apply_cache_factor_from_config(self, _caches): def test_apply_cache_factor_from_config(self):
"""Caches can disable applying cache factor updates, mainly used by """Caches can disable applying cache factor updates, mainly used by
event cache size. event cache size.
""" """