1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
|
import ctypes
import multiprocessing
import os
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,
_claim_database_clone_slot,
_release_dead_worker_slots,
)
from . import models
try:
import tblib.pickling_support
except ImportError:
tblib = None
def _test_error_exc_info():
try:
raise ValueError("woops")
except ValueError:
return sys.exc_info()
class ExceptionThatFailsUnpickling(Exception):
"""
After pickling, this class fails unpickling with an error about incorrect
arguments passed to __init__().
"""
def __init__(self, arg):
super().__init__()
def __reduce__(self):
# tblib 3.2+ makes exception subclasses picklable by default.
# Return (cls, ()) so the constructor fails on unpickle, preserving
# the needed behavior for test_pickle_errors_detection.
return (self.__class__, ())
class ParallelTestRunnerTest(SimpleTestCase):
"""
End-to-end tests of the parallel test runner.
These tests are only meaningful when running tests in parallel using
the --parallel option, though it doesn't hurt to run them not in
parallel.
"""
def test_subtest(self):
"""
Passing subtests work.
"""
for i in range(2):
with self.subTest(index=i):
self.assertEqual(i, i)
def test_system_checks(self):
self.assertEqual(models.Person.system_check_run_count, 1)
class SampleFailingSubtest(SimpleTestCase):
# This method name doesn't begin with "test" to prevent test discovery
# from seeing it.
def dummy_test(self):
"""
A dummy test for testing subTest failures.
"""
for i in range(3):
with self.subTest(index=i):
self.assertEqual(i, 1)
# This method name doesn't begin with "test" to prevent test discovery
# from seeing it.
def pickle_error_test(self):
with self.subTest("TypeError: cannot pickle memoryview object"):
self.x = memoryview(b"")
self.fail("expected failure")
class SampleErrorTest(SimpleTestCase):
@classmethod
def setUpClass(cls):
raise ValueError("woops")
super().setUpClass()
# This method name doesn't begin with "test" to prevent test discovery
# from seeing it.
def dummy_test(self):
raise AssertionError("SampleErrorTest.dummy_test() was called")
class RemoteTestResultTest(SimpleTestCase):
def test_was_successful_no_events(self):
result = RemoteTestResult()
self.assertIs(result.wasSuccessful(), True)
def test_was_successful_one_success(self):
result = RemoteTestResult()
test = None
result.startTest(test)
try:
result.addSuccess(test)
finally:
result.stopTest(test)
self.assertIs(result.wasSuccessful(), True)
def test_was_successful_one_expected_failure(self):
result = RemoteTestResult()
test = None
result.startTest(test)
try:
result.addExpectedFailure(test, _test_error_exc_info())
finally:
result.stopTest(test)
self.assertIs(result.wasSuccessful(), True)
def test_was_successful_one_skip(self):
result = RemoteTestResult()
test = None
result.startTest(test)
try:
result.addSkip(test, "Skipped")
finally:
result.stopTest(test)
self.assertIs(result.wasSuccessful(), True)
@unittest.skipUnless(tblib is not None, "requires tblib to be installed")
def test_was_successful_one_error(self):
result = RemoteTestResult()
test = None
result.startTest(test)
try:
result.addError(test, _test_error_exc_info())
finally:
result.stopTest(test)
self.assertIs(result.wasSuccessful(), False)
@unittest.skipUnless(tblib is not None, "requires tblib to be installed")
def test_was_successful_one_failure(self):
result = RemoteTestResult()
test = None
result.startTest(test)
try:
result.addFailure(test, _test_error_exc_info())
finally:
result.stopTest(test)
self.assertIs(result.wasSuccessful(), False)
@unittest.skipUnless(tblib is not None, "requires tblib to be installed")
def test_add_error_before_first_test(self):
result = RemoteTestResult()
test_id = "test_foo (tests.test_foo.FooTest.test_foo)"
test = _ErrorHolder(test_id)
# Call addError() without a call to startTest().
result.addError(test, _test_error_exc_info())
(event,) = result.events
self.assertEqual(event[0], "addError")
self.assertEqual(event[1], -1)
self.assertEqual(event[2], test_id)
error_type, _, _ = event[3]
self.assertEqual(error_type, ValueError)
self.assertIs(result.wasSuccessful(), False)
def test_picklable(self):
result = RemoteTestResult()
loaded_result = pickle.loads(pickle.dumps(result))
self.assertEqual(result.events, loaded_result.events)
def test_pickle_errors_detection(self):
picklable_error = RuntimeError("This is fine")
not_unpicklable_error = ExceptionThatFailsUnpickling("arg")
result = RemoteTestResult()
result._confirm_picklable(picklable_error)
# The exception can be pickled but not unpickled.
pickle.dumps(not_unpicklable_error)
msg = "__init__() missing 1 required positional argument"
with self.assertRaisesMessage(TypeError, msg):
result._confirm_picklable(not_unpicklable_error)
@unittest.skipUnless(tblib is not None, "requires tblib to be installed")
def test_unpicklable_subtest(self):
result = RemoteTestResult()
subtest_test = SampleFailingSubtest(methodName="pickle_error_test")
subtest_test.run(result=result)
events = result.events
subtest_event = events[1]
assertion_error = subtest_event[3]
self.assertEqual(str(assertion_error[1]), "expected failure")
@unittest.skipUnless(tblib is not None, "requires tblib to be installed")
def test_add_failing_subtests(self):
"""
Failing subtests are added correctly using addSubTest().
"""
# Manually run a test with failing subtests to prevent the failures
# from affecting the actual test run.
result = RemoteTestResult()
subtest_test = SampleFailingSubtest(methodName="dummy_test")
subtest_test.run(result=result)
events = result.events
self.assertEqual(len(events), 5)
self.assertIs(result.wasSuccessful(), False)
event = events[1]
self.assertEqual(event[0], "addSubTest")
self.assertEqual(
str(event[2]),
"dummy_test (test_runner.test_parallel.SampleFailingSubtest.dummy_test) "
"(index=0)",
)
self.assertEqual(repr(event[3][1]), "AssertionError('0 != 1')")
event = events[2]
self.assertEqual(repr(event[3][1]), "AssertionError('2 != 1')")
def test_add_duration(self):
result = RemoteTestResult()
result.addDuration(None, 2.3)
self.assertEqual(result.collectedDurations, [("None", 2.3)])
class DatabaseCloneSlotTests(SimpleTestCase):
def make_slots(self, *owners):
slots = multiprocessing.Array(ctypes.c_longlong, len(owners))
for index, owner in enumerate(owners):
slots[index] = owner
return slots
def test_claims_first_free_slot(self):
slots = self.make_slots(101, 0, 0)
self.assertEqual(_claim_database_clone_slot(slots), 2)
self.assertEqual(slots[1], os.getpid())
def test_reclaims_slot_owned_by_own_pid(self):
# The OS may reuse the pid of an exited worker whose slot was not
# released yet.
slots = self.make_slots(101, os.getpid(), 0)
self.assertEqual(_claim_database_clone_slot(slots), 2)
def test_prefers_own_slot_over_earlier_free_slot(self):
# A pid already owning a slot must reclaim it rather than take an
# earlier free slot, otherwise a pid reused by the OS could hold two
# slots at once and strand the old one.
slots = self.make_slots(0, os.getpid())
self.assertEqual(_claim_database_clone_slot(slots), 2)
# The earlier free slot is left untouched for another worker.
self.assertEqual(slots[0], 0)
def test_times_out_when_no_slot_is_free(self):
slots = self.make_slots(101, 102)
msg = "could not acquire a test database clone"
with self.assertRaisesMessage(RuntimeError, msg):
_claim_database_clone_slot(slots, timeout=0)
def test_release_dead_worker_slots(self):
alive_worker = mock.Mock(pid=103)
slots = self.make_slots(101, 0, 103)
with mock.patch.object(
multiprocessing, "active_children", return_value=[alive_worker]
):
_release_dead_worker_slots(slots)
self.assertEqual(list(slots), [0, 0, 103])
def test_released_slot_is_claimed_by_replacement_worker(self):
dead_worker_pid = 101
slots = self.make_slots(dead_worker_pid, 102)
with mock.patch.object(
multiprocessing, "active_children", return_value=[mock.Mock(pid=102)]
):
_release_dead_worker_slots(slots)
self.assertEqual(_claim_database_clone_slot(slots), 1)
self.assertEqual(slots[0], os.getpid())
class ParallelTestSuiteTest(SimpleTestCase):
@unittest.skipUnless(tblib is not None, "requires tblib to be installed")
def test_handle_add_error_before_first_test(self):
dummy_subsuites = []
pts = ParallelTestSuite(dummy_subsuites, processes=2)
result = TestResult()
remote_result = RemoteTestResult()
test = SampleErrorTest(methodName="dummy_test")
suite = TestSuite([test])
suite.run(remote_result)
for event in remote_result.events:
pts.handle_event(result, tests=list(suite), event=event)
self.assertEqual(len(result.errors), 1)
actual_test, tb_and_details_str = result.errors[0]
self.assertIsInstance(actual_test, _ErrorHolder)
self.assertEqual(
actual_test.id(), "setUpClass (test_runner.test_parallel.SampleErrorTest)"
)
self.assertIn("Traceback (most recent call last):", tb_and_details_str)
self.assertIn("ValueError: woops", tb_and_details_str)
def test_handle_add_error_during_test(self):
dummy_subsuites = []
pts = ParallelTestSuite(dummy_subsuites, processes=2)
result = TestResult()
test = TestCase()
err = _test_error_exc_info()
event = ("addError", 0, err)
pts.handle_event(result, tests=[test], event=event)
self.assertEqual(len(result.errors), 1)
actual_test, tb_and_details_str = result.errors[0]
self.assertIsInstance(actual_test, TestCase)
self.assertEqual(actual_test.id(), "unittest.case.TestCase.runTest")
self.assertIn("Traceback (most recent call last):", tb_and_details_str)
self.assertIn("ValueError: woops", tb_and_details_str)
def test_handle_add_failure(self):
dummy_subsuites = []
pts = ParallelTestSuite(dummy_subsuites, processes=2)
result = TestResult()
test = TestCase()
err = _test_error_exc_info()
event = ("addFailure", 0, err)
pts.handle_event(result, tests=[test], event=event)
self.assertEqual(len(result.failures), 1)
actual_test, tb_and_details_str = result.failures[0]
self.assertIsInstance(actual_test, TestCase)
self.assertEqual(actual_test.id(), "unittest.case.TestCase.runTest")
self.assertIn("Traceback (most recent call last):", tb_and_details_str)
self.assertIn("ValueError: woops", tb_and_details_str)
def test_handle_add_success(self):
dummy_subsuites = []
pts = ParallelTestSuite(dummy_subsuites, processes=2)
result = TestResult()
test = TestCase()
event = ("addSuccess", 0)
pts.handle_event(result, tests=[test], event=event)
self.assertEqual(len(result.errors), 0)
self.assertEqual(len(result.failures), 0)
@unittest.skipUnless(tblib is not None, "requires tblib to be installed")
def test_buffer_mode_reports_setupclass_failure(self):
test = SampleErrorTest("dummy_test")
remote_result = RemoteTestResult()
suite = TestSuite([test])
suite.run(remote_result)
pts = ParallelTestSuite([suite], processes=2, buffer=True)
pts.serialized_aliases = set()
test_result = TestResult()
test_result.buffer = True
with unittest.mock.patch("multiprocessing.Pool") as mock_pool:
def fake_next(*args, **kwargs):
test_result.shouldStop = True
return (0, remote_result.events)
mock_imap = mock_pool.return_value.__enter__.return_value.imap_unordered
mock_imap.return_value = unittest.mock.Mock(next=fake_next)
pts.run(test_result)
self.assertIn("ValueError: woops", test_result.errors[0][1])
|