-
Notifications
You must be signed in to change notification settings - Fork 277
Expand file tree
/
Copy pathtest_loadscope.py
More file actions
97 lines (72 loc) · 2.72 KB
/
Copy pathtest_loadscope.py
File metadata and controls
97 lines (72 loc) · 2.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
"""Tests for LoadScopeScheduling."""
from __future__ import annotations
from collections.abc import Sequence
from typing import TYPE_CHECKING
import execnet
import pytest
from xdist.scheduler import LoadScopeScheduling
from xdist.workermanage import WorkerController
if TYPE_CHECKING:
BaseOfMockGateway = execnet.Gateway
BaseOfMockNode = WorkerController
else:
BaseOfMockGateway = object
BaseOfMockNode = object
class MockGateway(BaseOfMockGateway):
_count = 0
def __init__(self) -> None:
self.id = str(MockGateway._count)
MockGateway._count += 1
class MockNode(BaseOfMockNode):
def __init__(self) -> None:
self.sent: list[int] = []
self.gateway = MockGateway()
self._shutdown = False
def send_runtest_some(self, indices: Sequence[int]) -> None:
self.sent.extend(indices)
def shutdown(self) -> None:
self._shutdown = True
@property
def shutting_down(self) -> bool:
return self._shutdown
@pytest.fixture(autouse=True)
def reset_mock_gateway_counter() -> None:
MockGateway._count = 0
class TestLoadScopeScheduling:
def test_replacement_worker_with_mismatched_collection_is_skipped(
self, pytester: pytest.Pytester
) -> None:
"""Regression test for https://github.com/pytest-dev/pytest-xdist/issues/1189"""
config = pytester.parseconfig("--tx=2*popen")
sched = LoadScopeScheduling(config)
node1, node2 = MockNode(), MockNode()
sched.add_node(node1)
sched.add_node(node2)
collection = [
"test_mod.py::test_a",
"test_mod.py::test_b",
"test_other.py::test_c",
"test_other.py::test_d",
]
sched.add_node_collection(node1, collection)
sched.add_node_collection(node2, collection)
sched.schedule()
# Simulate node1 crashing
sched.remove_node(node1)
# Replacement worker collects different tests (e.g., due to test file changes)
replacement_node = MockNode()
sched.add_node(replacement_node)
different_collection = [
"test_mod.py::test_a",
"test_mod.py::test_b",
"test_mod.py::test_NEW", # Different
"test_other.py::test_d",
]
sched.add_node_collection(replacement_node, different_collection)
# Replacement node should not be in registered_collections due to mismatch
assert replacement_node not in sched.registered_collections
assert replacement_node in sched.assigned_work
# schedule() should skip unregistered nodes rather than crashing
sched.schedule()
assert replacement_node.sent == []
assert node2 in sched.registered_collections