|
| 1 | +import ctypes |
| 2 | +import multiprocessing |
1 | 3 | import pickle
|
2 | 4 | import sys
|
3 | 5 | import unittest
|
| 6 | +from unittest import mock |
4 | 7 | from unittest.case import TestCase
|
5 | 8 | from unittest.result import TestResult
|
6 | 9 | from unittest.suite import TestSuite, _ErrorHolder
|
7 | 10 |
|
8 | 11 | from django.test import SimpleTestCase
|
9 |
| -from django.test.runner import ParallelTestSuite, RemoteTestResult |
| 12 | +from django.test.runner import ParallelTestSuite, RemoteTestResult, _init_worker |
10 | 13 |
|
11 | 14 | try:
|
12 | 15 | import tblib.pickling_support
|
@@ -213,6 +216,62 @@ def test_add_duration(self):
|
213 | 216 | self.assertEqual(result.collectedDurations, [("None", 2.3)])
|
214 | 217 |
|
215 | 218 |
|
| 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 | + |
216 | 275 | class ParallelTestSuiteTest(SimpleTestCase):
|
217 | 276 | @unittest.skipUnless(tblib is not None, "requires tblib to be installed")
|
218 | 277 | def test_handle_add_error_before_first_test(self):
|
|
0 commit comments