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

Add missing type hints to tests.config. #14681

Merged
merged 5 commits into from
Dec 16, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/14681.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add missing type hints.
4 changes: 1 addition & 3 deletions mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ exclude = (?x)
|tests/api/test_ratelimiting.py
|tests/app/test_openid_listener.py
|tests/appservice/test_scheduler.py
|tests/config/test_cache.py
|tests/config/test_tls.py
|tests/crypto/test_keyring.py
|tests/events/test_presence_router.py
|tests/events/test_utils.py
Expand Down Expand Up @@ -89,7 +87,7 @@ disallow_untyped_defs = False
[mypy-tests.*]
disallow_untyped_defs = False

[mypy-tests.config.test_api]
[mypy-tests.config.*]
disallow_untyped_defs = True

[mypy-tests.federation.transport.test_client]
Expand Down
9 changes: 2 additions & 7 deletions synapse/util/caches/lrucache.py
Original file line number Diff line number Diff line change
Expand Up @@ -788,26 +788,21 @@ def __len__(self) -> int:
def __contains__(self, key: KT) -> bool:
return self.contains(key)

def set_cache_factor(self, factor: float) -> bool:
DMRobertson marked this conversation as resolved.
Show resolved Hide resolved
def set_cache_factor(self, factor: float) -> None:
"""
Set the cache factor for this individual cache.

This will trigger a resize if it changes, which may require evicting
items from the cache.

Returns:
Whether the cache changed size or not.
"""
if not self.apply_cache_factor_from_config:
return False
return

new_size = int(self._original_max_size * factor)
if new_size != self.max_size:
self.max_size = new_size
if self._on_resize:
self._on_resize()
return True
return False

def __del__(self) -> None:
# We're about to be deleted, so we make sure to clear up all the nodes
Expand Down
6 changes: 3 additions & 3 deletions tests/config/test___main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@


class ConfigMainFileTestCase(ConfigFileTestCase):
def test_executes_without_an_action(self):
def test_executes_without_an_action(self) -> None:
self.generate_config()
main(["", "-c", self.config_file])

def test_read__error_if_key_not_found(self):
def test_read__error_if_key_not_found(self) -> None:
self.generate_config()
with self.assertRaises(SystemExit):
main(["", "read", "foo.bar.hello", "-c", self.config_file])

def test_read__passes_if_key_found(self):
def test_read__passes_if_key_found(self) -> None:
self.generate_config()
main(["", "read", "server.server_name", "-c", self.config_file])
4 changes: 2 additions & 2 deletions tests/config/test_background_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class BackgroundUpdateConfigTestCase(HomeserverTestCase):
# Tests that the default values in the config are correctly loaded. Note that the default
# values are loaded when the corresponding config options are commented out, which is why there isn't
# a config specified here.
def test_default_configuration(self):
def test_default_configuration(self) -> None:
background_updater = BackgroundUpdater(
self.hs, self.hs.get_datastores().main.db_pool
)
Expand All @@ -46,7 +46,7 @@ def test_default_configuration(self):
"""
)
)
def test_custom_configuration(self):
def test_custom_configuration(self) -> None:
background_updater = BackgroundUpdater(
self.hs, self.hs.get_datastores().main.db_pool
)
Expand Down
10 changes: 5 additions & 5 deletions tests/config/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@


class BaseConfigTestCase(unittest.TestCase):
def setUp(self):
def setUp(self) -> None:
# The root object needs a server property with a public_baseurl.
root = Mock()
root.server.public_baseurl = "http://test"
self.config = Config(root)

def test_loading_missing_templates(self):
def test_loading_missing_templates(self) -> None:
# Use a temporary directory that exists on the system, but that isn't likely to
# contain template files
with tempfile.TemporaryDirectory() as tmp_dir:
Expand All @@ -50,7 +50,7 @@ def test_loading_missing_templates(self):
"Template file did not contain our test string",
)

def test_loading_custom_templates(self):
def test_loading_custom_templates(self) -> None:
# Use a temporary directory that exists on the system
with tempfile.TemporaryDirectory() as tmp_dir:
# Create a temporary bogus template file
Expand Down Expand Up @@ -79,7 +79,7 @@ def test_loading_custom_templates(self):
"Template file did not contain our test string",
)

def test_multiple_custom_template_directories(self):
def test_multiple_custom_template_directories(self) -> None:
"""Tests that directories are searched in the right order if multiple custom
template directories are provided.
"""
Expand Down Expand Up @@ -137,7 +137,7 @@ def test_multiple_custom_template_directories(self):
for td in tempdirs:
td.cleanup()

def test_loading_template_from_nonexistent_custom_directory(self):
def test_loading_template_from_nonexistent_custom_directory(self) -> None:
with self.assertRaises(ConfigError):
self.config.read_templates(
["some_filename.html"], ("a_nonexistent_directory",)
Expand Down
61 changes: 31 additions & 30 deletions tests/config/test_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,28 @@
# limitations under the License.

from synapse.config.cache import CacheConfig, add_resizable_cache
from synapse.types import JsonDict
from synapse.util.caches.lrucache import LruCache

from tests.unittest import TestCase


class CacheConfigTests(TestCase):
def setUp(self):
def setUp(self) -> None:
# Reset caches before each test since there's global state involved.
self.config = CacheConfig()
self.config.reset()

def tearDown(self):
def tearDown(self) -> None:
# Also reset the caches after each test to leave state pristine.
self.config.reset()

def test_individual_caches_from_environ(self):
def test_individual_caches_from_environ(self) -> None:
"""
Individual cache factors will be loaded from the environment.
"""
config = {}
self.config._environ = {
config: JsonDict = {}
self.config._environ = { # type: ignore[assignment]
"SYNAPSE_CACHE_FACTOR_SOMETHING_OR_OTHER": "2",
"SYNAPSE_NOT_CACHE": "BLAH",
}
Expand All @@ -42,13 +43,13 @@ def test_individual_caches_from_environ(self):

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

def test_config_overrides_environ(self):
def test_config_overrides_environ(self) -> None:
"""
Individual cache factors defined in the environment will take precedence
over those in the config.
"""
config = {"caches": {"per_cache_factors": {"foo": 2, "bar": 3}}}
self.config._environ = {
config: JsonDict = {"caches": {"per_cache_factors": {"foo": 2, "bar": 3}}}
self.config._environ = { # type: ignore[assignment]
"SYNAPSE_CACHE_FACTOR_SOMETHING_OR_OTHER": "2",
"SYNAPSE_CACHE_FACTOR_FOO": 1,
}
Expand All @@ -60,104 +61,104 @@ def test_config_overrides_environ(self):
{"foo": 1.0, "bar": 3.0, "something_or_other": 2.0},
)

def test_individual_instantiated_before_config_load(self):
def test_individual_instantiated_before_config_load(self) -> None:
"""
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
is loaded.
"""
cache = LruCache(100)
cache: LruCache = LruCache(100)

add_resizable_cache("foo", cache_resize_callback=cache.set_cache_factor)
self.assertEqual(cache.max_size, 50)

config = {"caches": {"per_cache_factors": {"foo": 3}}}
config: JsonDict = {"caches": {"per_cache_factors": {"foo": 3}}}
self.config.read_config(config)
self.config.resize_all_caches()

self.assertEqual(cache.max_size, 300)

def test_individual_instantiated_after_config_load(self):
def test_individual_instantiated_after_config_load(self) -> None:
"""
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
there is one.
"""
config = {"caches": {"per_cache_factors": {"foo": 2}}}
config: JsonDict = {"caches": {"per_cache_factors": {"foo": 2}}}
self.config.read_config(config, config_dir_path="", data_dir_path="")
self.config.resize_all_caches()

cache = LruCache(100)
cache: LruCache = LruCache(100)
add_resizable_cache("foo", cache_resize_callback=cache.set_cache_factor)
self.assertEqual(cache.max_size, 200)

def test_global_instantiated_before_config_load(self):
def test_global_instantiated_before_config_load(self) -> None:
"""
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
default cache size once the config is loaded.
"""
cache = LruCache(100)
cache: LruCache = LruCache(100)
add_resizable_cache("foo", cache_resize_callback=cache.set_cache_factor)
self.assertEqual(cache.max_size, 50)

config = {"caches": {"global_factor": 4}}
config: JsonDict = {"caches": {"global_factor": 4}}
self.config.read_config(config, config_dir_path="", data_dir_path="")
self.config.resize_all_caches()

self.assertEqual(cache.max_size, 400)

def test_global_instantiated_after_config_load(self):
def test_global_instantiated_after_config_load(self) -> None:
"""
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
is no per-cache factor.
"""
config = {"caches": {"global_factor": 1.5}}
config: JsonDict = {"caches": {"global_factor": 1.5}}
self.config.read_config(config, config_dir_path="", data_dir_path="")
self.config.resize_all_caches()

cache = LruCache(100)
cache: LruCache = LruCache(100)
add_resizable_cache("foo", cache_resize_callback=cache.set_cache_factor)
self.assertEqual(cache.max_size, 150)

def test_cache_with_asterisk_in_name(self):
def test_cache_with_asterisk_in_name(self) -> None:
"""Some caches have asterisks in their name, test that they are set correctly."""

config = {
config: JsonDict = {
"caches": {
"per_cache_factors": {"*cache_a*": 5, "cache_b": 6, "cache_c": 2}
}
}
self.config._environ = {
self.config._environ = { # type: ignore[assignment]
"SYNAPSE_CACHE_FACTOR_CACHE_A": "2",
"SYNAPSE_CACHE_FACTOR_CACHE_B": 3,
"SYNAPSE_CACHE_FACTOR_CACHE_B": "3",
DMRobertson marked this conversation as resolved.
Show resolved Hide resolved
}
self.config.read_config(config, config_dir_path="", data_dir_path="")
self.config.resize_all_caches()

cache_a = LruCache(100)
cache_a: LruCache = LruCache(100)
add_resizable_cache("*cache_a*", cache_resize_callback=cache_a.set_cache_factor)
self.assertEqual(cache_a.max_size, 200)

cache_b = LruCache(100)
cache_b: LruCache = LruCache(100)
add_resizable_cache("*Cache_b*", cache_resize_callback=cache_b.set_cache_factor)
self.assertEqual(cache_b.max_size, 300)

cache_c = LruCache(100)
cache_c: LruCache = LruCache(100)
add_resizable_cache("*cache_c*", cache_resize_callback=cache_c.set_cache_factor)
self.assertEqual(cache_c.max_size, 200)

def test_apply_cache_factor_from_config(self):
def test_apply_cache_factor_from_config(self) -> None:
"""Caches can disable applying cache factor updates, mainly used by
event cache size.
"""

config = {"caches": {"event_cache_size": "10k"}}
config: JsonDict = {"caches": {"event_cache_size": "10k"}}
self.config.read_config(config, config_dir_path="", data_dir_path="")
self.config.resize_all_caches()

cache = LruCache(
cache: LruCache = LruCache(
max_size=self.config.event_cache_size,
apply_cache_factor_from_config=False,
)
Expand Down
2 changes: 1 addition & 1 deletion tests/config/test_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@


class DatabaseConfigTestCase(unittest.TestCase):
def test_database_configured_correctly(self):
def test_database_configured_correctly(self) -> None:
conf = yaml.safe_load(
DatabaseConfig().generate_config_section(data_dir_path="/data_dir_path")
)
Expand Down
8 changes: 4 additions & 4 deletions tests/config/test_generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@


class ConfigGenerationTestCase(unittest.TestCase):
def setUp(self):
def setUp(self) -> None:
self.dir = tempfile.mkdtemp()
self.file = os.path.join(self.dir, "homeserver.yaml")

def tearDown(self):
def tearDown(self) -> None:
shutil.rmtree(self.dir)

def test_generate_config_generates_files(self):
def test_generate_config_generates_files(self) -> None:
with redirect_stdout(StringIO()):
HomeServerConfig.load_or_generate_config(
"",
Expand All @@ -56,7 +56,7 @@ def test_generate_config_generates_files(self):
os.path.join(os.getcwd(), "homeserver.log"),
)

def assert_log_filename_is(self, log_config_file, expected):
def assert_log_filename_is(self, log_config_file: str, expected: str) -> None:
with open(log_config_file) as f:
config = f.read()
# find the 'filename' line
Expand Down
12 changes: 6 additions & 6 deletions tests/config/test_load.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@


class ConfigLoadingFileTestCase(ConfigFileTestCase):
def test_load_fails_if_server_name_missing(self):
def test_load_fails_if_server_name_missing(self) -> None:
self.generate_config_and_remove_lines_containing("server_name")
with self.assertRaises(ConfigError):
HomeServerConfig.load_config("", ["-c", self.config_file])
with self.assertRaises(ConfigError):
HomeServerConfig.load_or_generate_config("", ["-c", self.config_file])

def test_generates_and_loads_macaroon_secret_key(self):
def test_generates_and_loads_macaroon_secret_key(self) -> None:
self.generate_config()

with open(self.config_file) as f:
Expand Down Expand Up @@ -58,7 +58,7 @@ def test_generates_and_loads_macaroon_secret_key(self):
"was: %r" % (config2.key.macaroon_secret_key,)
)

def test_load_succeeds_if_macaroon_secret_key_missing(self):
def test_load_succeeds_if_macaroon_secret_key_missing(self) -> None:
self.generate_config_and_remove_lines_containing("macaroon")
config1 = HomeServerConfig.load_config("", ["-c", self.config_file])
config2 = HomeServerConfig.load_config("", ["-c", self.config_file])
Expand All @@ -73,7 +73,7 @@ def test_load_succeeds_if_macaroon_secret_key_missing(self):
config1.key.macaroon_secret_key, config3.key.macaroon_secret_key
)

def test_disable_registration(self):
def test_disable_registration(self) -> None:
self.generate_config()
self.add_lines_to_config(
["enable_registration: true", "disable_registration: true"]
Expand All @@ -93,15 +93,15 @@ def test_disable_registration(self):
assert config3 is not None
self.assertTrue(config3.registration.enable_registration)

def test_stats_enabled(self):
def test_stats_enabled(self) -> None:
self.generate_config_and_remove_lines_containing("enable_metrics")
self.add_lines_to_config(["enable_metrics: true"])

# The default Metrics Flags are off by default.
config = HomeServerConfig.load_config("", ["-c", self.config_file])
self.assertFalse(config.metrics.metrics_flags.known_servers)

def test_depreciated_identity_server_flag_throws_error(self):
def test_depreciated_identity_server_flag_throws_error(self) -> None:
self.generate_config()
# Needed to ensure that actual key/value pair added below don't end up on a line with a comment
self.add_lines_to_config([" "])
Expand Down
Loading