12
12
# See the License for the specific language governing permissions and
13
13
# limitations under the License.
14
14
15
+ from unittest .mock import patch
16
+
15
17
from synapse .config .cache import CacheConfig , add_resizable_cache
16
18
from synapse .util .caches .lrucache import LruCache
17
19
18
20
from tests .unittest import TestCase
19
21
20
22
23
+ # Patch the global _CACHES so that each test runs against its own state.
24
+ @patch ("synapse.config.cache._CACHES" , new_callable = dict )
21
25
class CacheConfigTests (TestCase ):
22
26
def setUp (self ):
23
27
# Reset caches before each test
@@ -26,7 +30,7 @@ def setUp(self):
26
30
def tearDown (self ):
27
31
self .config .reset ()
28
32
29
- def test_individual_caches_from_environ (self ):
33
+ def test_individual_caches_from_environ (self , _caches ):
30
34
"""
31
35
Individual cache factors will be loaded from the environment.
32
36
"""
@@ -39,7 +43,7 @@ def test_individual_caches_from_environ(self):
39
43
40
44
self .assertEqual (dict (self .config .cache_factors ), {"something_or_other" : 2.0 })
41
45
42
- def test_config_overrides_environ (self ):
46
+ def test_config_overrides_environ (self , _caches ):
43
47
"""
44
48
Individual cache factors defined in the environment will take precedence
45
49
over those in the config.
@@ -56,7 +60,7 @@ def test_config_overrides_environ(self):
56
60
{"foo" : 1.0 , "bar" : 3.0 , "something_or_other" : 2.0 },
57
61
)
58
62
59
- def test_individual_instantiated_before_config_load (self ):
63
+ def test_individual_instantiated_before_config_load (self , _caches ):
60
64
"""
61
65
If a cache is instantiated before the config is read, it will be given
62
66
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):
72
76
73
77
self .assertEqual (cache .max_size , 300 )
74
78
75
- def test_individual_instantiated_after_config_load (self ):
79
+ def test_individual_instantiated_after_config_load (self , _caches ):
76
80
"""
77
81
If a cache is instantiated after the config is read, it will be
78
82
immediately resized to the correct size given the per_cache_factor if
@@ -85,7 +89,7 @@ def test_individual_instantiated_after_config_load(self):
85
89
add_resizable_cache ("foo" , cache_resize_callback = cache .set_cache_factor )
86
90
self .assertEqual (cache .max_size , 200 )
87
91
88
- def test_global_instantiated_before_config_load (self ):
92
+ def test_global_instantiated_before_config_load (self , _caches ):
89
93
"""
90
94
If a cache is instantiated before the config is read, it will be given
91
95
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):
100
104
101
105
self .assertEqual (cache .max_size , 400 )
102
106
103
- def test_global_instantiated_after_config_load (self ):
107
+ def test_global_instantiated_after_config_load (self , _caches ):
104
108
"""
105
109
If a cache is instantiated after the config is read, it will be
106
110
immediately resized to the correct size given the global factor if there
@@ -113,7 +117,7 @@ def test_global_instantiated_after_config_load(self):
113
117
add_resizable_cache ("foo" , cache_resize_callback = cache .set_cache_factor )
114
118
self .assertEqual (cache .max_size , 150 )
115
119
116
- def test_cache_with_asterisk_in_name (self ):
120
+ def test_cache_with_asterisk_in_name (self , _caches ):
117
121
"""Some caches have asterisks in their name, test that they are set correctly."""
118
122
119
123
config = {
@@ -139,7 +143,7 @@ def test_cache_with_asterisk_in_name(self):
139
143
add_resizable_cache ("*cache_c*" , cache_resize_callback = cache_c .set_cache_factor )
140
144
self .assertEqual (cache_c .max_size , 200 )
141
145
142
- def test_apply_cache_factor_from_config (self ):
146
+ def test_apply_cache_factor_from_config (self , _caches ):
143
147
"""Caches can disable applying cache factor updates, mainly used by
144
148
event cache size.
145
149
"""
0 commit comments