Skip to content

Fixed #36083 -- Ran system checks in ParallelTestSuite workers #19070

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion django/test/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ def _init_worker(
used_aliases=None,
):
"""
Switch to databases dedicated to this worker.
Switch to databases dedicated to this worker and run system checks.
This helper lives at module-level because of the multiprocessing module's
requirements.
Expand All @@ -463,6 +463,7 @@ def _init_worker(
process_setup(*process_setup_args)
django.setup()
setup_test_environment(debug=debug_mode)
call_command("check", verbosity=0, databases=used_aliases)

db_aliases = used_aliases if used_aliases is not None else connections
for alias in db_aliases:
Expand Down
61 changes: 60 additions & 1 deletion tests/test_runner/test_parallel.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import ctypes
import multiprocessing
import pickle
import sys
import unittest
from unittest import mock
from unittest.case import TestCase
from unittest.result import TestResult
from unittest.suite import TestSuite, _ErrorHolder

from django.test import SimpleTestCase
from django.test.runner import ParallelTestSuite, RemoteTestResult
from django.test.runner import ParallelTestSuite, RemoteTestResult, _init_worker

try:
import tblib.pickling_support
Expand Down Expand Up @@ -213,6 +216,62 @@ def test_add_duration(self):
self.assertEqual(result.collectedDurations, [("None", 2.3)])


@mock.patch("django.test.runner.connections")
@mock.patch("django.test.runner.call_command")
@mock.patch("django.test.runner.setup_test_environment")
@mock.patch("django.setup")
@mock.patch("multiprocessing.get_start_method")
class InitWorkerTests(SimpleTestCase):
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The _init_worker function was not unit tested.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I appreciate that these add tests so that something would fail if we revert but I don't love that these tests just check that _init_worker is as defined. Would be good to have something slightly more meaningful if we can

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's fair. I have a feeling that _init_worker wasn't tested before because it's difficult to test. I'll try to look into a better approach, but I'm open to suggestions in the meantime.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jacobtylerwalls Would you have any thoughts or guidance on quality of the test?

def test_calls_setup_functions_when_start_method_is_spawn(
self,
mock_get_start_method,
mock_setup,
mock_setup_test_environment,
mock_call_command,
_,
):
mock_get_start_method.return_value = "spawn"
counter = multiprocessing.Value(ctypes.c_int, 0)
_init_worker(counter, initial_settings={"default": {}}, serialized_contents={})

mock_setup.assert_called_once()
mock_setup_test_environment.assert_called_once()
mock_call_command.assert_called_once_with(
"check", verbosity=mock.ANY, databases=mock.ANY
)

def test_does_not_call_setup_functions_when_start_method_is_not_spawn(
self,
mock_get_start_method,
mock_setup,
mock_setup_test_environment,
mock_call_command,
_,
):
mock_get_start_method.return_value = "fork"
_init_worker(multiprocessing.Value(ctypes.c_int, 0))

mock_setup.assert_not_called()
mock_setup_test_environment.assert_not_called()
mock_call_command.assert_not_called()

def test_sets_up_worker_connection_with_incremented_worker_id(
self,
mock_get_start_method,
mock_setup,
mock_setup_test_environment,
mock_call_command,
mock_connections,
):
mock_get_start_method.return_value = "fork"
connection_mock = mock.MagicMock()
mock_connections.__iter__.return_value = {"default": connection_mock}
mock_connections.__getitem__.return_value = connection_mock
_init_worker(multiprocessing.Value(ctypes.c_int, 0))

connection_mock.creation.setup_worker_connection.assert_called_once_with(1)


class ParallelTestSuiteTest(SimpleTestCase):
@unittest.skipUnless(tblib is not None, "requires tblib to be installed")
def test_handle_add_error_before_first_test(self):
Expand Down