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

Commit d35a451

Browse files
authored
Clean-up some broken/unused code in the test framework (#8514)
1 parent 9789b1f commit d35a451

File tree

3 files changed

+55
-70
lines changed

3 files changed

+55
-70
lines changed

changelog.d/8514.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Remove unused code from the test framework.

tests/server.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -367,8 +367,6 @@ def setup_test_homeserver(cleanup_func, *args, **kwargs):
367367
"""
368368
server = _sth(cleanup_func, *args, **kwargs)
369369

370-
database = server.config.database.get_single_database()
371-
372370
# Make the thread pool synchronous.
373371
clock = server.get_clock()
374372

tests/utils.py

Lines changed: 54 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,6 @@ class TestHomeServer(HomeServer):
192192
def setup_test_homeserver(
193193
cleanup_func,
194194
name="test",
195-
datastore=None,
196195
config=None,
197196
reactor=None,
198197
homeserverToUse=TestHomeServer,
@@ -249,7 +248,7 @@ def setup_test_homeserver(
249248

250249
# Create the database before we actually try and connect to it, based off
251250
# the template database we generate in setupdb()
252-
if datastore is None and isinstance(db_engine, PostgresEngine):
251+
if isinstance(db_engine, PostgresEngine):
253252
db_conn = db_engine.module.connect(
254253
database=POSTGRES_BASE_DB,
255254
user=POSTGRES_USER,
@@ -265,79 +264,66 @@ def setup_test_homeserver(
265264
cur.close()
266265
db_conn.close()
267266

268-
if datastore is None:
269-
hs = homeserverToUse(
270-
name,
271-
config=config,
272-
version_string="Synapse/tests",
273-
tls_server_context_factory=Mock(),
274-
tls_client_options_factory=Mock(),
275-
reactor=reactor,
276-
**kargs
277-
)
267+
hs = homeserverToUse(
268+
name,
269+
config=config,
270+
version_string="Synapse/tests",
271+
tls_server_context_factory=Mock(),
272+
tls_client_options_factory=Mock(),
273+
reactor=reactor,
274+
**kargs
275+
)
278276

279-
hs.setup()
280-
if homeserverToUse.__name__ == "TestHomeServer":
281-
hs.setup_background_tasks()
277+
hs.setup()
278+
if homeserverToUse.__name__ == "TestHomeServer":
279+
hs.setup_background_tasks()
282280

283-
if isinstance(db_engine, PostgresEngine):
284-
database = hs.get_datastores().databases[0]
281+
if isinstance(db_engine, PostgresEngine):
282+
database = hs.get_datastores().databases[0]
285283

286-
# We need to do cleanup on PostgreSQL
287-
def cleanup():
288-
import psycopg2
284+
# We need to do cleanup on PostgreSQL
285+
def cleanup():
286+
import psycopg2
289287

290-
# Close all the db pools
291-
database._db_pool.close()
288+
# Close all the db pools
289+
database._db_pool.close()
292290

293-
dropped = False
291+
dropped = False
294292

295-
# Drop the test database
296-
db_conn = db_engine.module.connect(
297-
database=POSTGRES_BASE_DB,
298-
user=POSTGRES_USER,
299-
host=POSTGRES_HOST,
300-
password=POSTGRES_PASSWORD,
301-
)
302-
db_conn.autocommit = True
303-
cur = db_conn.cursor()
304-
305-
# Try a few times to drop the DB. Some things may hold on to the
306-
# database for a few more seconds due to flakiness, preventing
307-
# us from dropping it when the test is over. If we can't drop
308-
# it, warn and move on.
309-
for x in range(5):
310-
try:
311-
cur.execute("DROP DATABASE IF EXISTS %s;" % (test_db,))
312-
db_conn.commit()
313-
dropped = True
314-
except psycopg2.OperationalError as e:
315-
warnings.warn(
316-
"Couldn't drop old db: " + str(e), category=UserWarning
317-
)
318-
time.sleep(0.5)
319-
320-
cur.close()
321-
db_conn.close()
322-
323-
if not dropped:
324-
warnings.warn("Failed to drop old DB.", category=UserWarning)
325-
326-
if not LEAVE_DB:
327-
# Register the cleanup hook
328-
cleanup_func(cleanup)
293+
# Drop the test database
294+
db_conn = db_engine.module.connect(
295+
database=POSTGRES_BASE_DB,
296+
user=POSTGRES_USER,
297+
host=POSTGRES_HOST,
298+
password=POSTGRES_PASSWORD,
299+
)
300+
db_conn.autocommit = True
301+
cur = db_conn.cursor()
329302

330-
else:
331-
hs = homeserverToUse(
332-
name,
333-
datastore=datastore,
334-
config=config,
335-
version_string="Synapse/tests",
336-
tls_server_context_factory=Mock(),
337-
tls_client_options_factory=Mock(),
338-
reactor=reactor,
339-
**kargs
340-
)
303+
# Try a few times to drop the DB. Some things may hold on to the
304+
# database for a few more seconds due to flakiness, preventing
305+
# us from dropping it when the test is over. If we can't drop
306+
# it, warn and move on.
307+
for x in range(5):
308+
try:
309+
cur.execute("DROP DATABASE IF EXISTS %s;" % (test_db,))
310+
db_conn.commit()
311+
dropped = True
312+
except psycopg2.OperationalError as e:
313+
warnings.warn(
314+
"Couldn't drop old db: " + str(e), category=UserWarning
315+
)
316+
time.sleep(0.5)
317+
318+
cur.close()
319+
db_conn.close()
320+
321+
if not dropped:
322+
warnings.warn("Failed to drop old DB.", category=UserWarning)
323+
324+
if not LEAVE_DB:
325+
# Register the cleanup hook
326+
cleanup_func(cleanup)
341327

342328
# bcrypt is far too slow to be doing in unit tests
343329
# Need to let the HS build an auth handler and then mess with it

0 commit comments

Comments
 (0)