diff --git a/changelog/1212.bugfix b/changelog/1212.bugfix new file mode 100644 index 00000000..27c957f2 --- /dev/null +++ b/changelog/1212.bugfix @@ -0,0 +1,2 @@ +Preserve duplicate collected test occurrences in ``loadscope``, ``loadfile``, and ``loadgroup`` scheduling, including when unfinished work is reassigned after a worker restart. +Dispatch work by collection index instead of repeatedly searching for each test's nodeid. diff --git a/src/xdist/scheduler/loadscope.py b/src/xdist/scheduler/loadscope.py index 69099d99..138f1990 100644 --- a/src/xdist/scheduler/loadscope.py +++ b/src/xdist/scheduler/loadscope.py @@ -40,16 +40,16 @@ class LoadScopeScheduling: None until ``.schedule()`` is called. :workqueue: Ordered dictionary that maps all available scopes with their - associated tests (nodeid). Nodeids are in turn associated with their - completion status. One entry of the workqueue is called a work unit. + associated test collection indices. Indices are in turn associated with + their completion status. One entry of the workqueue is called a work unit. In turn, a collection of work unit is called a workload. :: workqueue = { '///test_module.py': { - '///test_module.py::test_case1': False, - '///test_module.py::test_case2': False, + 0: False, + 1: False, (...) }, (...) @@ -63,8 +63,8 @@ class LoadScopeScheduling: assigned_work = { '': { '///test_module.py': { - '///test_module.py::test_case1': False, - '///test_module.py::test_case2': False, + 0: False, + 1: False, (...) }, (...) @@ -94,8 +94,8 @@ def __init__(self, config: pytest.Config, log: Producer | None = None) -> None: self.numnodes = len(parse_tx_spec_config(config)) self.collection: list[str] | None = None - self.workqueue: OrderedDict[str, dict[str, bool]] = OrderedDict() - self.assigned_work: dict[WorkerController, dict[str, dict[str, bool]]] = {} + self.workqueue: OrderedDict[str, dict[int, bool]] = OrderedDict() + self.assigned_work: dict[WorkerController, dict[str, dict[int, bool]]] = {} self.registered_collections: dict[WorkerController, list[str]] = {} if log is None: @@ -183,10 +183,11 @@ def remove_node(self, node: WorkerController) -> str | None: return None # The node crashed, identify test that crashed + assert self.collection is not None for work_unit in workload.values(): - for nodeid, completed in work_unit.items(): + for item_index, completed in work_unit.items(): if not completed: - crashitem = nodeid + crashitem = self.collection[item_index] break else: continue @@ -261,7 +262,7 @@ def mark_test_complete( nodeid = self.registered_collections[node][item_index] scope = self._split_scope(nodeid) - self.assigned_work[node][scope][nodeid] = True + self.assigned_work[node][scope][item_index] = True self._reschedule(node) def mark_test_pending(self, item: str) -> NoReturn: @@ -286,11 +287,8 @@ def _assign_work_unit(self, node: WorkerController) -> None: assigned_to_node[scope] = work_unit # Ask the node to execute the workload - worker_collection = self.registered_collections[node] nodeids_indexes = [ - worker_collection.index(nodeid) - for nodeid, completed in work_unit.items() - if not completed + item_index for item_index, completed in work_unit.items() if not completed ] if not nodeids_indexes: # Raise since this is an internal error that may result in a hanging worker @@ -325,7 +323,7 @@ def _split_scope(self, nodeid: str) -> str: """ return nodeid.rsplit("::", 1)[0] - def _pending_of(self, workload: dict[str, dict[str, bool]]) -> int: + def _pending_of(self, workload: dict[str, dict[int, bool]]) -> int: """Return the number of pending tests in a workload.""" pending = sum(list(scope.values()).count(False) for scope in workload.values()) return pending @@ -396,21 +394,22 @@ def schedule(self) -> None: return # Determine chunks of work (scopes) - unsorted_workqueue: dict[str, dict[str, bool]] = {} - for nodeid in self.collection: + unsorted_workqueue: dict[str, dict[int, bool]] = {} + for item_index, nodeid in enumerate(self.collection): scope = self._split_scope(nodeid) work_unit = unsorted_workqueue.setdefault(scope, {}) - work_unit[nodeid] = False + # Nodeids need not be unique. Preserve each collected occurrence. + work_unit[item_index] = False if self.config.option.loadscopereorder: # Insert tests scopes into work queue ordered by number of tests. - for scope, nodeids in sorted( + for scope, work_unit in sorted( unsorted_workqueue.items(), key=lambda item: -len(item[1]) ): - self.workqueue[scope] = nodeids + self.workqueue[scope] = work_unit else: - for scope, nodeids in unsorted_workqueue.items(): - self.workqueue[scope] = nodeids + for scope, work_unit in unsorted_workqueue.items(): + self.workqueue[scope] = work_unit # A replacement for a worker that crashed before scheduling may still be collecting nodes = [node for node in self.nodes if node in self.registered_collections] diff --git a/testing/acceptance_test.py b/testing/acceptance_test.py index 8fe47de9..2d76ec0e 100644 --- a/testing/acceptance_test.py +++ b/testing/acceptance_test.py @@ -1337,6 +1337,17 @@ def pytest_collection_modifyitems(): result.stdout.fnmatch_lines(["*RuntimeError: Some runtime error*"]) +@pytest.mark.parametrize("dist", ["loadscope", "loadfile", "loadgroup"]) +def test_scope_keeps_duplicate_occurrences( + pytester: pytest.Pytester, dist: str +) -> None: + test_file = pytester.makepyfile("def test_selected_occurrence(): pass") + result = pytester.runpytest_subprocess( + "-n2", f"--dist={dist}", "--keep-duplicates", test_file, test_file + ) + result.assert_outcomes(passed=2) + + class TestLoadScope: def test_by_module(self, pytester: pytest.Pytester) -> None: test_file = """ diff --git a/testing/test_dsession.py b/testing/test_dsession.py index ee0d2884..b5256d57 100644 --- a/testing/test_dsession.py +++ b/testing/test_dsession.py @@ -18,6 +18,8 @@ from xdist.dsession import WorkerStatus from xdist.report import report_collection_diff from xdist.scheduler import EachScheduling +from xdist.scheduler import LoadFileScheduling +from xdist.scheduler import LoadGroupScheduling from xdist.scheduler import LoadScheduling from xdist.scheduler import LoadScopeScheduling from xdist.scheduler import WorkStealingScheduling @@ -575,6 +577,51 @@ def pytest_collectreport(self, report: pytest.CollectReport) -> None: assert "Different tests were collected between" in reports[0].longrepr +@pytest.mark.parametrize( + "scheduler", [LoadScopeScheduling, LoadFileScheduling, LoadGroupScheduling] +) +class TestScopeSchedulingOccurrences: + def test_duplicate_completion( + self, pytester: pytest.Pytester, scheduler: type[LoadScopeScheduling] + ) -> None: + sched = scheduler(pytester.parseconfig("--tx=popen")) + node = MockNode() + sched.add_node(node) + sched.add_node_collection(node, ["a.py::test_same"] * 2) + + sched.schedule() + assert node.sent == [0, 1] + sched.mark_test_complete(node, 0) + assert bool(sched.has_pending) + sched.mark_test_complete(node, 1) + assert not sched.has_pending + assert sched.tests_finished + + def test_duplicate_restart( + self, pytester: pytest.Pytester, scheduler: type[LoadScopeScheduling] + ) -> None: + sched = scheduler(pytester.parseconfig("--tx=popen")) + collection = ["a.py::test_same"] * 3 + node = MockNode() + sched.add_node(node) + sched.add_node_collection(node, collection) + sched.schedule() + assert node.sent == [0, 1, 2] + sched.mark_test_complete(node, 0) + + assert sched.remove_node(node) == "a.py::test_same" + replacement = MockNode() + sched.add_node(replacement) + sched.add_node_collection(replacement, collection) + sched.schedule() + assert replacement.sent == [1, 2] + sched.mark_test_complete(replacement, 1) + assert bool(sched.has_pending) + sched.mark_test_complete(replacement, 2) + assert not sched.has_pending + assert sched.tests_finished + + class TestDistReporter: @pytest.mark.parametrize("isatty", [False, True]) @pytest.mark.parametrize("no_header", [False, True])