From ea0288c4d01fd3cfebb3f4461134c19f03f6c153 Mon Sep 17 00:00:00 2001 From: Umair Khan Date: Tue, 18 Apr 2017 16:42:12 +0500 Subject: [PATCH] testing: Create DB only in init_worker for parallel mode. This commit has no bearing on serial mode performance. For parallel mode, this will reduce the overhead of one instance of DB destruction/creation. --- zerver/lib/test_runner.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/zerver/lib/test_runner.py b/zerver/lib/test_runner.py index 9503e83651..58790f8158 100644 --- a/zerver/lib/test_runner.py +++ b/zerver/lib/test_runner.py @@ -403,8 +403,8 @@ class Runner(DiscoverRunner): def setup_test_environment(self, *args, **kwargs): # type: (*Any, **Any) -> Any settings.DATABASES['default']['NAME'] = settings.BACKEND_DATABASE_TEMPLATE - destroy_test_databases(self.database_id) - create_test_databases(self.database_id) + # We create/destroy the test databases in run_tests to avoid + # duplicate work when running in parallel mode. return super(Runner, self).setup_test_environment(*args, **kwargs) def teardown_test_environment(self, *args, **kwargs): @@ -430,6 +430,17 @@ class Runner(DiscoverRunner): print(" StreamMessagesTest.test_message_to_stream") print() sys.exit(1) + + if self.parallel == 1: + # We are running in serial mode so create the databases here. + # For parallel mode, the databases are created in init_worker. + # We don't want to create and destroy DB in setup_test_environment + # because it will be called for both serial and parallel modes. + # However, at this point we know in which mode we would be running + # since that decision has already been made in build_suite(). + destroy_test_databases(self.database_id) + create_test_databases(self.database_id) + # We have to do the next line to avoid flaky scenarios where we # run a single test and getting an SA connection causes data from # a Django connection to be rolled back mid-test.