@@ -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 )
9574class 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
0 commit comments