Skip to content

Commit 9c2d1cb

Browse files
adamzapsarahboyce
authored andcommitted
Fixed #36083 -- Called check command in ParallelTestSuite workers.
Workers created by ParallelTestSuite were not running system checks. In general this is fine, but system checks can have side effects that the tests expect. This patch runs system checks inside of _init_worker, which is only called by ParallelTestSuite.
1 parent 500bd42 commit 9c2d1cb

File tree

2 files changed

+62
-2
lines changed

2 files changed

+62
-2
lines changed

django/test/runner.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ def _init_worker(
439439
used_aliases=None,
440440
):
441441
"""
442-
Switch to databases dedicated to this worker.
442+
Switch to databases dedicated to this worker and run system checks.
443443
444444
This helper lives at module-level because of the multiprocessing module's
445445
requirements.
@@ -463,6 +463,7 @@ def _init_worker(
463463
process_setup(*process_setup_args)
464464
django.setup()
465465
setup_test_environment(debug=debug_mode)
466+
call_command("check", verbosity=0, databases=used_aliases)
466467

467468
db_aliases = used_aliases if used_aliases is not None else connections
468469
for alias in db_aliases:

tests/test_runner/test_parallel.py

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1+
import ctypes
2+
import multiprocessing
13
import pickle
24
import sys
35
import unittest
6+
from unittest import mock
47
from unittest.case import TestCase
58
from unittest.result import TestResult
69
from unittest.suite import TestSuite, _ErrorHolder
710

811
from django.test import SimpleTestCase
9-
from django.test.runner import ParallelTestSuite, RemoteTestResult
12+
from django.test.runner import ParallelTestSuite, RemoteTestResult, _init_worker
1013

1114
try:
1215
import tblib.pickling_support
@@ -213,6 +216,62 @@ def test_add_duration(self):
213216
self.assertEqual(result.collectedDurations, [("None", 2.3)])
214217

215218

219+
@mock.patch("django.test.runner.connections")
220+
@mock.patch("django.test.runner.call_command")
221+
@mock.patch("django.test.runner.setup_test_environment")
222+
@mock.patch("django.setup")
223+
@mock.patch("multiprocessing.get_start_method")
224+
class InitWorkerTests(SimpleTestCase):
225+
def test_calls_setup_functions_when_start_method_is_spawn(
226+
self,
227+
mock_get_start_method,
228+
mock_setup,
229+
mock_setup_test_environment,
230+
mock_call_command,
231+
_,
232+
):
233+
mock_get_start_method.return_value = "spawn"
234+
counter = multiprocessing.Value(ctypes.c_int, 0)
235+
_init_worker(counter, initial_settings={"default": {}}, serialized_contents={})
236+
237+
mock_setup.assert_called_once()
238+
mock_setup_test_environment.assert_called_once()
239+
mock_call_command.assert_called_once_with(
240+
"check", verbosity=mock.ANY, databases=mock.ANY
241+
)
242+
243+
def test_does_not_call_setup_functions_when_start_method_is_not_spawn(
244+
self,
245+
mock_get_start_method,
246+
mock_setup,
247+
mock_setup_test_environment,
248+
mock_call_command,
249+
_,
250+
):
251+
mock_get_start_method.return_value = "fork"
252+
_init_worker(multiprocessing.Value(ctypes.c_int, 0))
253+
254+
mock_setup.assert_not_called()
255+
mock_setup_test_environment.assert_not_called()
256+
mock_call_command.assert_not_called()
257+
258+
def test_sets_up_worker_connection_with_incremented_worker_id(
259+
self,
260+
mock_get_start_method,
261+
mock_setup,
262+
mock_setup_test_environment,
263+
mock_call_command,
264+
mock_connections,
265+
):
266+
mock_get_start_method.return_value = "fork"
267+
connection_mock = mock.MagicMock()
268+
mock_connections.__iter__.return_value = {"default": connection_mock}
269+
mock_connections.__getitem__.return_value = connection_mock
270+
_init_worker(multiprocessing.Value(ctypes.c_int, 0))
271+
272+
connection_mock.creation.setup_worker_connection.assert_called_once_with(1)
273+
274+
216275
class ParallelTestSuiteTest(SimpleTestCase):
217276
@unittest.skipUnless(tblib is not None, "requires tblib to be installed")
218277
def test_handle_add_error_before_first_test(self):

0 commit comments

Comments
 (0)