Skip to content

Commit f68afca

Browse files
committed
address review points to simply
1 parent 8989a91 commit f68afca

3 files changed

Lines changed: 26 additions & 96 deletions

File tree

Lib/test/libregrtest/findtests.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,6 @@ def list_cases(tests: TestTuple, *,
9393
match_tests: TestFilter | None = None,
9494
test_dir: StrPath | None = None) -> None:
9595
support.verbose = False
96-
set_match_tests(match_tests)
9796
cases_by_module, skipped = collect_cases(tests, match_tests=match_tests,
9897
test_dir=test_dir)
9998
for cases in cases_by_module.values():

Lib/test/libregrtest/run_workers.py

Lines changed: 15 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -70,27 +70,6 @@ def stop(self):
7070
with self.lock:
7171
self.tests_iter = None
7272

73-
class GroupedMultiprocessIterator:
74-
"""Provide test groups safely across multiple worker threads."""
75-
76-
def __init__(self, groups_iter):
77-
self.lock = threading.Lock()
78-
self.groups_iter = groups_iter
79-
80-
def next_group(self):
81-
with self.lock:
82-
if self.groups_iter is None:
83-
return None
84-
try:
85-
return next(self.groups_iter)
86-
except StopIteration:
87-
return None
88-
89-
def stop(self):
90-
with self.lock:
91-
self.groups_iter = None
92-
93-
9473
@dataclasses.dataclass(slots=True, frozen=True)
9574
class MultiprocessResult:
9675
result: TestResult
@@ -421,82 +400,40 @@ def _runtest(self, test_name: TestName,
421400
return MultiprocessResult(result, stdout)
422401

423402
def run(self) -> None:
424-
if self.runtests.single_process_per_case:
425-
self._run_grouped()
426-
else:
427-
self._run_flat()
428-
429-
def _run_flat(self) -> None:
430-
"""Original behavior: one test name (module) per iteration."""
431-
assert isinstance(self.pending, MultiprocessIterator)
432403
fail_fast = self.runtests.fail_fast
433404
fail_env_changed = self.runtests.fail_env_changed
405+
single_process_per_case = self.runtests.single_process_per_case
434406
try:
435-
while not self._stopped:
407+
stop = False
408+
while not self._stopped and not stop:
436409
try:
437-
test_name = next(self.pending)
410+
module_name, case_ids = next(self.pending)
438411
except StopIteration:
439412
break
440413

441-
self.start_time = time.monotonic()
442-
self.test_name = test_name
443-
try:
444-
mp_result = self._runtest(test_name)
445-
except WorkerError as exc:
446-
mp_result = exc.mp_result
447-
finally:
448-
self.test_name = _NOT_RUNNING
449-
mp_result.result.duration = time.monotonic() - self.start_time
450-
self.output.put((False, mp_result))
451-
452-
if mp_result.result.must_stop(fail_fast, fail_env_changed):
453-
break
454-
except ExitThread:
455-
pass
456-
except BaseException:
457-
self.output.put((True, traceback.format_exc()))
458-
finally:
459-
self.output.put(WorkerThreadExited())
460-
461-
def _run_grouped(self) -> None:
462-
"""Execute all tests in a group on the same thread before moving on."""
463-
assert isinstance(self.pending, GroupedMultiprocessIterator)
464-
fail_fast = self.runtests.fail_fast
465-
fail_env_changed = self.runtests.fail_env_changed
466-
try:
467-
while not self._stopped:
468-
group = self.pending.next_group()
469-
if group is None:
470-
break
471-
472-
module_name, case_ids = group
473-
must_stop = False
414+
# All cases of a group run sequentially on this thread
474415
for test_name in case_ids:
475416
if self._stopped:
476417
break
477418
self.start_time = time.monotonic()
478419
self.test_name = test_name
479420
try:
480-
mp_result = self._runtest(test_name, module_name)
421+
mp_result = self._runtest(
422+
test_name,
423+
module_name if single_process_per_case else None)
481424
except WorkerError as exc:
482425
mp_result = exc.mp_result
483426
finally:
484427
self.test_name = _NOT_RUNNING
485-
486-
mp_result = dataclasses.replace(
487-
mp_result,
488-
result=dataclasses.replace(
489-
mp_result.result,
490-
test_name=test_name,
491-
duration=time.monotonic() - self.start_time))
492-
428+
mp_result.result.duration = time.monotonic() - self.start_time
429+
if single_process_per_case:
430+
# Report the test case, not the test module
431+
mp_result.result.test_name = test_name
493432
self.output.put((False, mp_result))
433+
494434
if mp_result.result.must_stop(fail_fast, fail_env_changed):
495-
must_stop = True
435+
stop = True
496436
break
497-
498-
if must_stop:
499-
break
500437
except ExitThread:
501438
pass
502439
except BaseException:
@@ -573,13 +510,7 @@ def __init__(self, num_workers: int, runtests: RunTests,
573510
self.live_worker_count = 0
574511

575512
self.output: queue.Queue[QueueContent] = queue.Queue()
576-
self.pending: MultiprocessIterator | GroupedMultiprocessIterator
577-
if runtests.single_process_per_case:
578-
groups_iter = runtests.iter_case_groups()
579-
self.pending = GroupedMultiprocessIterator(groups_iter)
580-
else:
581-
tests_iter = runtests.iter_tests()
582-
self.pending = MultiprocessIterator(tests_iter)
513+
self.pending = MultiprocessIterator(runtests.iter_case_groups())
583514
self.timeout = runtests.timeout
584515
if self.timeout is not None:
585516
# Rely on faulthandler to kill a worker process. This timouet is

Lib/test/test_regrtest.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828

2929
from test.libregrtest.findtests import collect_cases
3030
from test.libregrtest.filter import set_match_tests
31-
from test.libregrtest.run_workers import GroupedMultiprocessIterator
31+
from test.libregrtest.run_workers import MultiprocessIterator
3232
from test import support
3333
from test.support import import_helper
3434
from test.support import os_helper
@@ -2602,36 +2602,36 @@ def test_collect_cases_skiptest(self):
26022602
self.assertIn(testname, skipped)
26032603

26042604

2605-
class GroupedMultiprocessIteratorTestCase(unittest.TestCase):
2605+
class MultiprocessIteratorTestCase(unittest.TestCase):
26062606
def test_yields_all_groups_once(self):
26072607
groups = [("mod_a", ("mod_a.A.t1", "mod_a.A.t2")),
26082608
("mod_b", ("mod_b.B.t1",))]
2609-
it = GroupedMultiprocessIterator(iter(groups))
2609+
it = MultiprocessIterator(iter(groups))
26102610
seen = []
2611-
while (g := it.next_group()) is not None:
2611+
while (g := next(it, None)) is not None:
26122612
seen.append(g)
26132613
self.assertEqual(seen, groups)
26142614

26152615
def test_exhausted_returns_none(self):
2616-
it = GroupedMultiprocessIterator(iter([]))
2617-
self.assertIsNone(it.next_group())
2616+
it = MultiprocessIterator(iter([]))
2617+
self.assertIsNone(next(it, None))
26182618

26192619
def test_stop_halts_iteration(self):
26202620
groups = [("mod_a", ("mod_a.A.t1",)), ("mod_b", ("mod_b.B.t1",))]
2621-
it = GroupedMultiprocessIterator(iter(groups))
2622-
it.next_group()
2621+
it = MultiprocessIterator(iter(groups))
2622+
next(it, None)
26232623
it.stop()
2624-
self.assertIsNone(it.next_group())
2624+
self.assertIsNone(next(it, None))
26252625

26262626
def test_thread_safety_no_duplicate_or_lost_groups(self):
26272627
n = 200
26282628
groups = [(f"mod_{i}", (f"mod_{i}.T.t",)) for i in range(n)]
2629-
it = GroupedMultiprocessIterator(iter(groups))
2629+
it = MultiprocessIterator(iter(groups))
26302630
results = []
26312631
results_lock = threading.Lock()
26322632

26332633
def worker():
2634-
while (g := it.next_group()) is not None:
2634+
while (g := next(it, None)) is not None:
26352635
with results_lock:
26362636
results.append(g)
26372637

0 commit comments

Comments
 (0)