forked from python/cpython
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
pythongh-127933: Add option to run regression tests in parallel
This adds a new command line argument, `--parallel-threads` to the regression test runner to allow it to run individual tests in multiple threads in parallel in order to find multithreading bugs.
- Loading branch information
Showing
10 changed files
with
139 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
"""Run a test case multiple times in parallel threads.""" | ||
|
||
import copy | ||
import functools | ||
import threading | ||
import unittest | ||
|
||
from unittest import TestCase | ||
|
||
|
||
class ParallelTestCase(TestCase): | ||
def __init__(self, test_case: TestCase, num_threads: int): | ||
self.test_case = test_case | ||
self.num_threads = num_threads | ||
self._testMethodName = test_case._testMethodName | ||
self._testMethodDoc = test_case._testMethodDoc | ||
|
||
def __str__(self): | ||
return f"{str(self.test_case)} [threads={self.num_threads}]" | ||
|
||
def run_worker(self, test_case: TestCase, result: unittest.Result, | ||
barrier: threading.Barrier): | ||
barrier.wait() | ||
test_case.run(result) | ||
|
||
def run(self, result=None): | ||
if result is None: | ||
result = test_case.defaultTestResult() | ||
startTestRun = getattr(result, 'startTestRun', None) | ||
stopTestRun = getattr(result, 'stopTestRun', None) | ||
if startTestRun is not None: | ||
startTestRun() | ||
else: | ||
stopTestRun = None | ||
|
||
# Called at the beginning of each test. See TestCase.run. | ||
result.startTest(self) | ||
|
||
cases = [copy.copy(self.test_case) for _ in range(self.num_threads)] | ||
results = [unittest.TestResult() for _ in range(self.num_threads)] | ||
|
||
barrier = threading.Barrier(self.num_threads) | ||
threads = [] | ||
for case, r in zip(cases, results): | ||
thread = threading.Thread(target=self.run_worker, | ||
args=(case, r, barrier), | ||
daemon=True) | ||
threads.append(thread) | ||
|
||
for thread in threads: | ||
thread.start() | ||
|
||
for threads in threads: | ||
threads.join() | ||
|
||
# Aggregate test results | ||
if all(r.wasSuccessful() for r in results): | ||
result.addSuccess(self) | ||
|
||
# Note: We can't call result.addError, result.addFailure, etc. because | ||
# we no longer the original exception, just the string format. | ||
for r in results: | ||
if len(r.errors) > 0 or len(r.failures) > 0: | ||
result._mirrorOutput = True | ||
result.errors.extend(r.errors) | ||
result.failures.extend(r.failures) | ||
result.skipped.extend(r.skipped) | ||
result.expectedFailures.extend(r.expectedFailures) | ||
result.unexpectedSuccesses.extend(r.unexpectedSuccesses) | ||
result.collectedDurations.extend(r.collectedDurations) | ||
|
||
if any(r.shouldStop for r in results): | ||
result.stop() | ||
|
||
# Test has finished running | ||
result.stopTest(self) | ||
if stopTestRun is not None: | ||
stopTestRun() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters