Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelog/1212.bugfix
Original file line number Diff line number Diff line change
@@ -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.
45 changes: 22 additions & 23 deletions src/xdist/scheduler/loadscope.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
'<full>/<path>/<to>/test_module.py': {
'<full>/<path>/<to>/test_module.py::test_case1': False,
'<full>/<path>/<to>/test_module.py::test_case2': False,
0: False,
1: False,
(...)
},
(...)
Expand All @@ -63,8 +63,8 @@ class LoadScopeScheduling:
assigned_work = {
'<worker node A>': {
'<full>/<path>/<to>/test_module.py': {
'<full>/<path>/<to>/test_module.py::test_case1': False,
'<full>/<path>/<to>/test_module.py::test_case2': False,
0: False,
1: False,
(...)
},
(...)
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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]
Expand Down
11 changes: 11 additions & 0 deletions testing/acceptance_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = """
Expand Down
47 changes: 47 additions & 0 deletions testing/test_dsession.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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])
Expand Down