mirror of
https://github.com/zulip/zulip.git
synced 2025-11-01 04:23:46 +00:00
test_runner: Fix unnecessary sleep(1) in database teardown.
Apparently, Django's _destroy_test_db has a mostly unnecessary sleep(1) before dropping the database, which obviously wastes a bunch of time in the single-test runtime of their database teardown logic. We work around this by monkey-patching that function to not do the sleep.
This commit is contained in:
@@ -9,7 +9,7 @@ from unittest import loader, runner # type: ignore # Mypy cannot pick these up
|
|||||||
from unittest.result import TestResult
|
from unittest.result import TestResult
|
||||||
|
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.db import connections
|
from django.db import connections, ProgrammingError
|
||||||
from django.test import TestCase
|
from django.test import TestCase
|
||||||
from django.test import runner as django_runner
|
from django.test import runner as django_runner
|
||||||
from django.test.runner import DiscoverRunner
|
from django.test.runner import DiscoverRunner
|
||||||
@@ -246,6 +246,17 @@ def run_subsuite(args):
|
|||||||
process_instrumented_calls(partial(result.addInstrumentation, None)) # type: ignore
|
process_instrumented_calls(partial(result.addInstrumentation, None)) # type: ignore
|
||||||
return subsuite_index, result.events
|
return subsuite_index, result.events
|
||||||
|
|
||||||
|
# Monkey-patch database creation to fix unnecessary sleep(1)
|
||||||
|
from django.db.backends.postgresql.creation import DatabaseCreation
|
||||||
|
def _replacement_destroy_test_db(self, test_database_name, verbosity):
|
||||||
|
# type: (Any, str, Any) -> None
|
||||||
|
"""Replacement for Django's _destroy_test_db that removes the
|
||||||
|
unnecessary sleep(1)."""
|
||||||
|
with self.connection._nodb_connection.cursor() as cursor:
|
||||||
|
cursor.execute("DROP DATABASE %s"
|
||||||
|
% self.connection.ops.quote_name(test_database_name))
|
||||||
|
DatabaseCreation._destroy_test_db = _replacement_destroy_test_db
|
||||||
|
|
||||||
def destroy_test_databases(database_id=None):
|
def destroy_test_databases(database_id=None):
|
||||||
# type: (Optional[int]) -> None
|
# type: (Optional[int]) -> None
|
||||||
"""
|
"""
|
||||||
@@ -256,7 +267,7 @@ def destroy_test_databases(database_id=None):
|
|||||||
connection = connections[alias]
|
connection = connections[alias]
|
||||||
try:
|
try:
|
||||||
connection.creation.destroy_test_db(number=database_id)
|
connection.creation.destroy_test_db(number=database_id)
|
||||||
except Exception:
|
except ProgrammingError:
|
||||||
# DB doesn't exist. No need to do anything.
|
# DB doesn't exist. No need to do anything.
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user