Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

Commit 7301019

Browse files
authored
Ensure each cache config test uses separate state. (#11019)
Hopefully this fixes these tests sometimes failing in CI.
1 parent e0bf34d commit 7301019

File tree

2 files changed

+13
-8
lines changed

2 files changed

+13
-8
lines changed

changelog.d/11019.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Ensure that cache config tests do not share state.

tests/config/test_cache.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,16 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
from unittest.mock import patch
16+
1517
from synapse.config.cache import CacheConfig, add_resizable_cache
1618
from synapse.util.caches.lrucache import LruCache
1719

1820
from tests.unittest import TestCase
1921

2022

23+
# Patch the global _CACHES so that each test runs against its own state.
24+
@patch("synapse.config.cache._CACHES", new_callable=dict)
2125
class CacheConfigTests(TestCase):
2226
def setUp(self):
2327
# Reset caches before each test
@@ -26,7 +30,7 @@ def setUp(self):
2630
def tearDown(self):
2731
self.config.reset()
2832

29-
def test_individual_caches_from_environ(self):
33+
def test_individual_caches_from_environ(self, _caches):
3034
"""
3135
Individual cache factors will be loaded from the environment.
3236
"""
@@ -39,7 +43,7 @@ def test_individual_caches_from_environ(self):
3943

4044
self.assertEqual(dict(self.config.cache_factors), {"something_or_other": 2.0})
4145

42-
def test_config_overrides_environ(self):
46+
def test_config_overrides_environ(self, _caches):
4347
"""
4448
Individual cache factors defined in the environment will take precedence
4549
over those in the config.
@@ -56,7 +60,7 @@ def test_config_overrides_environ(self):
5660
{"foo": 1.0, "bar": 3.0, "something_or_other": 2.0},
5761
)
5862

59-
def test_individual_instantiated_before_config_load(self):
63+
def test_individual_instantiated_before_config_load(self, _caches):
6064
"""
6165
If a cache is instantiated before the config is read, it will be given
6266
the default cache size in the interim, and then resized once the config
@@ -72,7 +76,7 @@ def test_individual_instantiated_before_config_load(self):
7276

7377
self.assertEqual(cache.max_size, 300)
7478

75-
def test_individual_instantiated_after_config_load(self):
79+
def test_individual_instantiated_after_config_load(self, _caches):
7680
"""
7781
If a cache is instantiated after the config is read, it will be
7882
immediately resized to the correct size given the per_cache_factor if
@@ -85,7 +89,7 @@ def test_individual_instantiated_after_config_load(self):
8589
add_resizable_cache("foo", cache_resize_callback=cache.set_cache_factor)
8690
self.assertEqual(cache.max_size, 200)
8791

88-
def test_global_instantiated_before_config_load(self):
92+
def test_global_instantiated_before_config_load(self, _caches):
8993
"""
9094
If a cache is instantiated before the config is read, it will be given
9195
the default cache size in the interim, and then resized to the new
@@ -100,7 +104,7 @@ def test_global_instantiated_before_config_load(self):
100104

101105
self.assertEqual(cache.max_size, 400)
102106

103-
def test_global_instantiated_after_config_load(self):
107+
def test_global_instantiated_after_config_load(self, _caches):
104108
"""
105109
If a cache is instantiated after the config is read, it will be
106110
immediately resized to the correct size given the global factor if there
@@ -113,7 +117,7 @@ def test_global_instantiated_after_config_load(self):
113117
add_resizable_cache("foo", cache_resize_callback=cache.set_cache_factor)
114118
self.assertEqual(cache.max_size, 150)
115119

116-
def test_cache_with_asterisk_in_name(self):
120+
def test_cache_with_asterisk_in_name(self, _caches):
117121
"""Some caches have asterisks in their name, test that they are set correctly."""
118122

119123
config = {
@@ -139,7 +143,7 @@ def test_cache_with_asterisk_in_name(self):
139143
add_resizable_cache("*cache_c*", cache_resize_callback=cache_c.set_cache_factor)
140144
self.assertEqual(cache_c.max_size, 200)
141145

142-
def test_apply_cache_factor_from_config(self):
146+
def test_apply_cache_factor_from_config(self, _caches):
143147
"""Caches can disable applying cache factor updates, mainly used by
144148
event cache size.
145149
"""

0 commit comments

Comments
 (0)