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

Commit 6799a42

Browse files
committed
Comments
1 parent 59261a4 commit 6799a42

File tree

3 files changed

+28
-8
lines changed

3 files changed

+28
-8
lines changed

synapse/storage/engines/sqlite.py

+4
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ def __init__(self, database_config: Mapping[str, Any]):
3434
":memory:",
3535
)
3636

37+
# A connection to a database that has already been prepared, to use as a
38+
# base for an in-memory connection. This is used during unit tests to
39+
# speed up setting up the DB.
3740
self._prepped_conn: Optional[sqlite3.Connection] = database_config.get(
3841
"_TEST_PREPPED_CONN"
3942
)
@@ -86,6 +89,7 @@ def on_new_connection(self, db_conn: "LoggingDatabaseConnection") -> None:
8689

8790
if self._is_in_memory:
8891
if self._prepped_conn is not None:
92+
# Initialise the new DB from the pre-prepared DB.
8993
assert isinstance(db_conn.conn, sqlite3.Connection)
9094
self._prepped_conn.backup(db_conn.conn)
9195
else:

tests/server.py

+11-6
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,9 @@
107107
# the type of thing that can be passed into `make_request` in the headers list
108108
CustomHeaderType = Tuple[Union[str, bytes], Union[str, bytes]]
109109

110-
PREPPED_DB_CONN: Optional[LoggingDatabaseConnection] = None
110+
# A pre-prepared SQLite DB that is used as a template when creating new SQLite
111+
# DB each test run. This dramatically speeds up test set up when using SQLite.
112+
PREPPED_SQlITE_DB_CONN: Optional[LoggingDatabaseConnection] = None
111113

112114

113115
class TimedOutException(Exception):
@@ -904,18 +906,21 @@ def setup_test_homeserver(
904906
"args": {"database": test_db_location, "cp_min": 1, "cp_max": 1},
905907
}
906908

907-
global PREPPED_DB_CONN
908-
if PREPPED_DB_CONN is None:
909+
# Check if we have set up a DB that we can use as a template.
910+
global PREPPED_SQlITE_DB_CONN
911+
if PREPPED_SQlITE_DB_CONN is None:
909912
temp_engine = create_engine(database_config)
910-
PREPPED_DB_CONN = LoggingDatabaseConnection(
913+
PREPPED_SQlITE_DB_CONN = LoggingDatabaseConnection(
911914
sqlite3.connect(":memory:"), temp_engine, "PREPPED_CONN"
912915
)
913916

914917
database = DatabaseConnectionConfig("master", database_config)
915918
config.database.databases = [database]
916-
prepare_database(PREPPED_DB_CONN, create_engine(database_config), config)
919+
prepare_database(
920+
PREPPED_SQlITE_DB_CONN, create_engine(database_config), config
921+
)
917922

918-
database_config["_TEST_PREPPED_CONN"] = PREPPED_DB_CONN
923+
database_config["_TEST_PREPPED_CONN"] = PREPPED_SQlITE_DB_CONN
919924

920925
if "db_txn_limit" in kwargs:
921926
database_config["txn_limit"] = kwargs["db_txn_limit"]

tests/unittest.py

+13-2
Original file line numberDiff line numberDiff line change
@@ -163,11 +163,22 @@ def tearDown(orig: Callable[[], R]) -> R:
163163

164164
return orig()
165165

166+
# We want to force a GC to workaround problems with deferreds leaking
167+
# logcontexts when they are GCed (see the logcontext docs).
168+
#
169+
# The easiest way to do this would be to do a full GC after each test
170+
# run, but that is very expensive. Instead, we disable GC for the
171+
# duration of the test so that we only need to run a gen-0 GC, which is
172+
# a lot quicker.
173+
174+
@around(self)
175+
def setUp(orig: Callable[[], R]) -> R:
176+
gc.disable()
177+
return orig()
178+
166179
@around(self)
167180
def tearDown(orig: Callable[[], R]) -> R:
168181
ret = orig()
169-
# force a GC to workaround problems with deferreds leaking logcontexts when
170-
# they are GCed (see the logcontext docs)
171182
gc.collect(0)
172183
set_current_context(SENTINEL_CONTEXT)
173184

0 commit comments

Comments
 (0)