Adopt 'src' layout and add 'testing' extras
This commit is contained in:
4
src/xdist/scheduler/__init__.py
Normal file
4
src/xdist/scheduler/__init__.py
Normal file
@@ -0,0 +1,4 @@
|
||||
from xdist.scheduler.each import EachScheduling # noqa
|
||||
from xdist.scheduler.load import LoadScheduling # noqa
|
||||
from xdist.scheduler.loadfile import LoadFileScheduling # noqa
|
||||
from xdist.scheduler.loadscope import LoadScopeScheduling # noqa
|
||||
132
src/xdist/scheduler/each.py
Normal file
132
src/xdist/scheduler/each.py
Normal file
@@ -0,0 +1,132 @@
|
||||
from py.log import Producer
|
||||
|
||||
from xdist.workermanage import parse_spec_config
|
||||
from xdist.report import report_collection_diff
|
||||
|
||||
|
||||
class EachScheduling(object):
|
||||
"""Implement scheduling of test items on all nodes
|
||||
|
||||
If a node gets added after the test run is started then it is
|
||||
assumed to replace a node which got removed before it finished
|
||||
its collection. In this case it will only be used if a node
|
||||
with the same spec got removed earlier.
|
||||
|
||||
Any nodes added after the run is started will only get items
|
||||
assigned if a node with a matching spec was removed before it
|
||||
finished all its pending items. The new node will then be
|
||||
assigned the remaining items from the removed node.
|
||||
"""
|
||||
|
||||
def __init__(self, config, log=None):
|
||||
self.config = config
|
||||
self.numnodes = len(parse_spec_config(config))
|
||||
self.node2collection = {}
|
||||
self.node2pending = {}
|
||||
self._started = []
|
||||
self._removed2pending = {}
|
||||
if log is None:
|
||||
self.log = Producer("eachsched")
|
||||
else:
|
||||
self.log = log.eachsched
|
||||
self.collection_is_completed = False
|
||||
|
||||
@property
|
||||
def nodes(self):
|
||||
"""A list of all nodes in the scheduler."""
|
||||
return list(self.node2pending.keys())
|
||||
|
||||
@property
|
||||
def tests_finished(self):
|
||||
if not self.collection_is_completed:
|
||||
return False
|
||||
if self._removed2pending:
|
||||
return False
|
||||
for pending in self.node2pending.values():
|
||||
if len(pending) >= 2:
|
||||
return False
|
||||
return True
|
||||
|
||||
@property
|
||||
def has_pending(self):
|
||||
"""Return True if there are pending test items
|
||||
|
||||
This indicates that collection has finished and nodes are
|
||||
still processing test items, so this can be thought of as
|
||||
"the scheduler is active".
|
||||
"""
|
||||
for pending in self.node2pending.values():
|
||||
if pending:
|
||||
return True
|
||||
return False
|
||||
|
||||
def add_node(self, node):
|
||||
assert node not in self.node2pending
|
||||
self.node2pending[node] = []
|
||||
|
||||
def add_node_collection(self, node, collection):
|
||||
"""Add the collected test items from a node
|
||||
|
||||
Collection is complete once all nodes have submitted their
|
||||
collection. In this case its pending list is set to an empty
|
||||
list. When the collection is already completed this
|
||||
submission is from a node which was restarted to replace a
|
||||
dead node. In this case we already assign the pending items
|
||||
here. In either case ``.schedule()`` will instruct the
|
||||
node to start running the required tests.
|
||||
"""
|
||||
assert node in self.node2pending
|
||||
if not self.collection_is_completed:
|
||||
self.node2collection[node] = list(collection)
|
||||
self.node2pending[node] = []
|
||||
if len(self.node2collection) >= self.numnodes:
|
||||
self.collection_is_completed = True
|
||||
elif self._removed2pending:
|
||||
for deadnode in self._removed2pending:
|
||||
if deadnode.gateway.spec == node.gateway.spec:
|
||||
dead_collection = self.node2collection[deadnode]
|
||||
if collection != dead_collection:
|
||||
msg = report_collection_diff(
|
||||
dead_collection,
|
||||
collection,
|
||||
deadnode.gateway.id,
|
||||
node.gateway.id,
|
||||
)
|
||||
self.log(msg)
|
||||
return
|
||||
pending = self._removed2pending.pop(deadnode)
|
||||
self.node2pending[node] = pending
|
||||
break
|
||||
|
||||
def mark_test_complete(self, node, item_index, duration=0):
|
||||
self.node2pending[node].remove(item_index)
|
||||
|
||||
def remove_node(self, node):
|
||||
# KeyError if we didn't get an add_node() yet
|
||||
pending = self.node2pending.pop(node)
|
||||
if not pending:
|
||||
return
|
||||
crashitem = self.node2collection[node][pending.pop(0)]
|
||||
if pending:
|
||||
self._removed2pending[node] = pending
|
||||
return crashitem
|
||||
|
||||
def schedule(self):
|
||||
"""Schedule the test items on the nodes
|
||||
|
||||
If the node's pending list is empty it is a new node which
|
||||
needs to run all the tests. If the pending list is already
|
||||
populated (by ``.add_node_collection()``) then it replaces a
|
||||
dead node and we only need to run those tests.
|
||||
"""
|
||||
assert self.collection_is_completed
|
||||
for node, pending in self.node2pending.items():
|
||||
if node in self._started:
|
||||
continue
|
||||
if not pending:
|
||||
pending[:] = range(len(self.node2collection[node]))
|
||||
node.send_runtest_all()
|
||||
node.shutdown()
|
||||
else:
|
||||
node.send_runtest_some(pending)
|
||||
self._started.append(node)
|
||||
286
src/xdist/scheduler/load.py
Normal file
286
src/xdist/scheduler/load.py
Normal file
@@ -0,0 +1,286 @@
|
||||
from itertools import cycle
|
||||
|
||||
from py.log import Producer
|
||||
from _pytest.runner import CollectReport
|
||||
|
||||
from xdist.workermanage import parse_spec_config
|
||||
from xdist.report import report_collection_diff
|
||||
|
||||
|
||||
class LoadScheduling(object):
|
||||
"""Implement load scheduling across nodes.
|
||||
|
||||
This distributes the tests collected across all nodes so each test
|
||||
is run just once. All nodes collect and submit the test suite and
|
||||
when all collections are received it is verified they are
|
||||
identical collections. Then the collection gets divided up in
|
||||
chunks and chunks get submitted to nodes. Whenever a node finishes
|
||||
an item, it calls ``.mark_test_complete()`` which will trigger the
|
||||
scheduler to assign more tests if the number of pending tests for
|
||||
the node falls below a low-watermark.
|
||||
|
||||
When created, ``numnodes`` defines how many nodes are expected to
|
||||
submit a collection. This is used to know when all nodes have
|
||||
finished collection or how large the chunks need to be created.
|
||||
|
||||
Attributes:
|
||||
|
||||
:numnodes: The expected number of nodes taking part. The actual
|
||||
number of nodes will vary during the scheduler's lifetime as
|
||||
nodes are added by the DSession as they are brought up and
|
||||
removed either because of a dead node or normal shutdown. This
|
||||
number is primarily used to know when the initial collection is
|
||||
completed.
|
||||
|
||||
:node2collection: Map of nodes and their test collection. All
|
||||
collections should always be identical.
|
||||
|
||||
:node2pending: Map of nodes and the indices of their pending
|
||||
tests. The indices are an index into ``.pending`` (which is
|
||||
identical to their own collection stored in
|
||||
``.node2collection``).
|
||||
|
||||
:collection: The one collection once it is validated to be
|
||||
identical between all the nodes. It is initialised to None
|
||||
until ``.schedule()`` is called.
|
||||
|
||||
:pending: List of indices of globally pending tests. These are
|
||||
tests which have not yet been allocated to a chunk for a node
|
||||
to process.
|
||||
|
||||
:log: A py.log.Producer instance.
|
||||
|
||||
:config: Config object, used for handling hooks.
|
||||
"""
|
||||
|
||||
def __init__(self, config, log=None):
|
||||
self.numnodes = len(parse_spec_config(config))
|
||||
self.node2collection = {}
|
||||
self.node2pending = {}
|
||||
self.pending = []
|
||||
self.collection = None
|
||||
if log is None:
|
||||
self.log = Producer("loadsched")
|
||||
else:
|
||||
self.log = log.loadsched
|
||||
self.config = config
|
||||
|
||||
@property
|
||||
def nodes(self):
|
||||
"""A list of all nodes in the scheduler."""
|
||||
return list(self.node2pending.keys())
|
||||
|
||||
@property
|
||||
def collection_is_completed(self):
|
||||
"""Boolean indication initial test collection is complete.
|
||||
|
||||
This is a boolean indicating all initial participating nodes
|
||||
have finished collection. The required number of initial
|
||||
nodes is defined by ``.numnodes``.
|
||||
"""
|
||||
return len(self.node2collection) >= self.numnodes
|
||||
|
||||
@property
|
||||
def tests_finished(self):
|
||||
"""Return True if all tests have been executed by the nodes."""
|
||||
if not self.collection_is_completed:
|
||||
return False
|
||||
if self.pending:
|
||||
return False
|
||||
for pending in self.node2pending.values():
|
||||
if len(pending) >= 2:
|
||||
return False
|
||||
return True
|
||||
|
||||
@property
|
||||
def has_pending(self):
|
||||
"""Return True if there are pending test items
|
||||
|
||||
This indicates that collection has finished and nodes are
|
||||
still processing test items, so this can be thought of as
|
||||
"the scheduler is active".
|
||||
"""
|
||||
if self.pending:
|
||||
return True
|
||||
for pending in self.node2pending.values():
|
||||
if pending:
|
||||
return True
|
||||
return False
|
||||
|
||||
def add_node(self, node):
|
||||
"""Add a new node to the scheduler.
|
||||
|
||||
From now on the node will be allocated chunks of tests to
|
||||
execute.
|
||||
|
||||
Called by the ``DSession.worker_workerready`` hook when it
|
||||
successfully bootstraps a new node.
|
||||
"""
|
||||
assert node not in self.node2pending
|
||||
self.node2pending[node] = []
|
||||
|
||||
def add_node_collection(self, node, collection):
|
||||
"""Add the collected test items from a node
|
||||
|
||||
The collection is stored in the ``.node2collection`` map.
|
||||
Called by the ``DSession.worker_collectionfinish`` hook.
|
||||
"""
|
||||
assert node in self.node2pending
|
||||
if self.collection_is_completed:
|
||||
# A new node has been added later, perhaps an original one died.
|
||||
# .schedule() should have
|
||||
# been called by now
|
||||
assert self.collection
|
||||
if collection != self.collection:
|
||||
other_node = next(iter(self.node2collection.keys()))
|
||||
msg = report_collection_diff(
|
||||
self.collection, collection, other_node.gateway.id, node.gateway.id
|
||||
)
|
||||
self.log(msg)
|
||||
return
|
||||
self.node2collection[node] = list(collection)
|
||||
|
||||
def mark_test_complete(self, node, item_index, duration=0):
|
||||
"""Mark test item as completed by node
|
||||
|
||||
The duration it took to execute the item is used as a hint to
|
||||
the scheduler.
|
||||
|
||||
This is called by the ``DSession.worker_testreport`` hook.
|
||||
"""
|
||||
self.node2pending[node].remove(item_index)
|
||||
self.check_schedule(node, duration=duration)
|
||||
|
||||
def check_schedule(self, node, duration=0):
|
||||
"""Maybe schedule new items on the node
|
||||
|
||||
If there are any globally pending nodes left then this will
|
||||
check if the given node should be given any more tests. The
|
||||
``duration`` of the last test is optionally used as a
|
||||
heuristic to influence how many tests the node is assigned.
|
||||
"""
|
||||
if node.shutting_down:
|
||||
return
|
||||
|
||||
if self.pending:
|
||||
# how many nodes do we have?
|
||||
num_nodes = len(self.node2pending)
|
||||
# if our node goes below a heuristic minimum, fill it out to
|
||||
# heuristic maximum
|
||||
items_per_node_min = max(2, len(self.pending) // num_nodes // 4)
|
||||
items_per_node_max = max(2, len(self.pending) // num_nodes // 2)
|
||||
node_pending = self.node2pending[node]
|
||||
if len(node_pending) < items_per_node_min:
|
||||
if duration >= 0.1 and len(node_pending) >= 2:
|
||||
# seems the node is doing long-running tests
|
||||
# and has enough items to continue
|
||||
# so let's rather wait with sending new items
|
||||
return
|
||||
num_send = items_per_node_max - len(node_pending)
|
||||
self._send_tests(node, num_send)
|
||||
else:
|
||||
node.shutdown()
|
||||
|
||||
self.log("num items waiting for node:", len(self.pending))
|
||||
|
||||
def remove_node(self, node):
|
||||
"""Remove a node from the scheduler
|
||||
|
||||
This should be called either when the node crashed or at
|
||||
shutdown time. In the former case any pending items assigned
|
||||
to the node will be re-scheduled. Called by the
|
||||
``DSession.worker_workerfinished`` and
|
||||
``DSession.worker_errordown`` hooks.
|
||||
|
||||
Return the item which was being executing while the node
|
||||
crashed or None if the node has no more pending items.
|
||||
|
||||
"""
|
||||
pending = self.node2pending.pop(node)
|
||||
if not pending:
|
||||
return
|
||||
|
||||
# The node crashed, reassing pending items
|
||||
crashitem = self.collection[pending.pop(0)]
|
||||
self.pending.extend(pending)
|
||||
for node in self.node2pending:
|
||||
self.check_schedule(node)
|
||||
return crashitem
|
||||
|
||||
def schedule(self):
|
||||
"""Initiate distribution of the test collection
|
||||
|
||||
Initiate scheduling of the items across the nodes. If this
|
||||
gets called again later it behaves the same as calling
|
||||
``.check_schedule()`` on all nodes so that newly added nodes
|
||||
will start to be used.
|
||||
|
||||
This is called by the ``DSession.worker_collectionfinish`` hook
|
||||
if ``.collection_is_completed`` is True.
|
||||
"""
|
||||
assert self.collection_is_completed
|
||||
|
||||
# Initial distribution already happened, reschedule on all nodes
|
||||
if self.collection is not None:
|
||||
for node in self.nodes:
|
||||
self.check_schedule(node)
|
||||
return
|
||||
|
||||
# XXX allow nodes to have different collections
|
||||
if not self._check_nodes_have_same_collection():
|
||||
self.log("**Different tests collected, aborting run**")
|
||||
return
|
||||
|
||||
# Collections are identical, create the index of pending items.
|
||||
self.collection = list(self.node2collection.values())[0]
|
||||
self.pending[:] = range(len(self.collection))
|
||||
if not self.collection:
|
||||
return
|
||||
|
||||
# Send a batch of tests to run. If we don't have at least two
|
||||
# tests per node, we have to send them all so that we can send
|
||||
# shutdown signals and get all nodes working.
|
||||
initial_batch = max(len(self.pending) // 4, 2 * len(self.nodes))
|
||||
|
||||
# distribute tests round-robin up to the batch size
|
||||
# (or until we run out)
|
||||
nodes = cycle(self.nodes)
|
||||
for i in range(initial_batch):
|
||||
self._send_tests(next(nodes), 1)
|
||||
|
||||
if not self.pending:
|
||||
# initial distribution sent all tests, start node shutdown
|
||||
for node in self.nodes:
|
||||
node.shutdown()
|
||||
|
||||
def _send_tests(self, node, num):
|
||||
tests_per_node = self.pending[:num]
|
||||
if tests_per_node:
|
||||
del self.pending[:num]
|
||||
self.node2pending[node].extend(tests_per_node)
|
||||
node.send_runtest_some(tests_per_node)
|
||||
|
||||
def _check_nodes_have_same_collection(self):
|
||||
"""Return True if all nodes have collected the same items.
|
||||
|
||||
If collections differ, this method returns False while logging
|
||||
the collection differences and posting collection errors to
|
||||
pytest_collectreport hook.
|
||||
"""
|
||||
node_collection_items = list(self.node2collection.items())
|
||||
first_node, col = node_collection_items[0]
|
||||
same_collection = True
|
||||
for node, collection in node_collection_items[1:]:
|
||||
msg = report_collection_diff(
|
||||
col, collection, first_node.gateway.id, node.gateway.id
|
||||
)
|
||||
if msg:
|
||||
same_collection = False
|
||||
self.log(msg)
|
||||
if self.config is not None:
|
||||
rep = CollectReport(
|
||||
node.gateway.id, "failed", longrepr=msg, result=[]
|
||||
)
|
||||
self.config.hook.pytest_collectreport(report=rep)
|
||||
|
||||
return same_collection
|
||||
52
src/xdist/scheduler/loadfile.py
Normal file
52
src/xdist/scheduler/loadfile.py
Normal file
@@ -0,0 +1,52 @@
|
||||
from .loadscope import LoadScopeScheduling
|
||||
from py.log import Producer
|
||||
|
||||
|
||||
class LoadFileScheduling(LoadScopeScheduling):
|
||||
"""Implement load scheduling across nodes, but grouping test test file.
|
||||
|
||||
This distributes the tests collected across all nodes so each test is run
|
||||
just once. All nodes collect and submit the list of tests and when all
|
||||
collections are received it is verified they are identical collections.
|
||||
Then the collection gets divided up in work units, grouped by test file,
|
||||
and those work units get submitted to nodes. Whenever a node finishes an
|
||||
item, it calls ``.mark_test_complete()`` which will trigger the scheduler
|
||||
to assign more work units if the number of pending tests for the node falls
|
||||
below a low-watermark.
|
||||
|
||||
When created, ``numnodes`` defines how many nodes are expected to submit a
|
||||
collection. This is used to know when all nodes have finished collection.
|
||||
|
||||
This class behaves very much like LoadScopeScheduling, but with a file-level scope.
|
||||
"""
|
||||
|
||||
def __init__(self, config, log=None):
|
||||
super(LoadFileScheduling, self).__init__(config, log)
|
||||
if log is None:
|
||||
self.log = Producer("loadfilesched")
|
||||
else:
|
||||
self.log = log.loadfilesched
|
||||
|
||||
def _split_scope(self, nodeid):
|
||||
"""Determine the scope (grouping) of a nodeid.
|
||||
|
||||
There are usually 3 cases for a nodeid::
|
||||
|
||||
example/loadsuite/test/test_beta.py::test_beta0
|
||||
example/loadsuite/test/test_delta.py::Delta1::test_delta0
|
||||
example/loadsuite/epsilon/__init__.py::epsilon.epsilon
|
||||
|
||||
#. Function in a test module.
|
||||
#. Method of a class in a test module.
|
||||
#. Doctest in a function in a package.
|
||||
|
||||
This function will group tests with the scope determined by splitting
|
||||
the first ``::`` from the left. That is, test will be grouped in a
|
||||
single work unit when they reside in the same file.
|
||||
In the above example, scopes will be::
|
||||
|
||||
example/loadsuite/test/test_beta.py
|
||||
example/loadsuite/test/test_delta.py
|
||||
example/loadsuite/epsilon/__init__.py
|
||||
"""
|
||||
return nodeid.split("::", 1)[0]
|
||||
409
src/xdist/scheduler/loadscope.py
Normal file
409
src/xdist/scheduler/loadscope.py
Normal file
@@ -0,0 +1,409 @@
|
||||
from collections import OrderedDict
|
||||
|
||||
from _pytest.runner import CollectReport
|
||||
from py.log import Producer
|
||||
from xdist.report import report_collection_diff
|
||||
from xdist.workermanage import parse_spec_config
|
||||
|
||||
|
||||
class LoadScopeScheduling(object):
|
||||
"""Implement load scheduling across nodes, but grouping test by scope.
|
||||
|
||||
This distributes the tests collected across all nodes so each test is run
|
||||
just once. All nodes collect and submit the list of tests and when all
|
||||
collections are received it is verified they are identical collections.
|
||||
Then the collection gets divided up in work units, grouped by test scope,
|
||||
and those work units get submitted to nodes. Whenever a node finishes an
|
||||
item, it calls ``.mark_test_complete()`` which will trigger the scheduler
|
||||
to assign more work units if the number of pending tests for the node falls
|
||||
below a low-watermark.
|
||||
|
||||
When created, ``numnodes`` defines how many nodes are expected to submit a
|
||||
collection. This is used to know when all nodes have finished collection.
|
||||
|
||||
Attributes:
|
||||
|
||||
:numnodes: The expected number of nodes taking part. The actual number of
|
||||
nodes will vary during the scheduler's lifetime as nodes are added by
|
||||
the DSession as they are brought up and removed either because of a dead
|
||||
node or normal shutdown. This number is primarily used to know when the
|
||||
initial collection is completed.
|
||||
|
||||
:collection: The final list of tests collected by all nodes once it is
|
||||
validated to be identical between all the nodes. It is initialised to
|
||||
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.
|
||||
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,
|
||||
(...)
|
||||
},
|
||||
(...)
|
||||
}
|
||||
|
||||
:assigned_work: Ordered dictionary that maps worker nodes with their
|
||||
assigned work units.
|
||||
|
||||
::
|
||||
|
||||
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,
|
||||
(...)
|
||||
},
|
||||
(...)
|
||||
},
|
||||
(...)
|
||||
}
|
||||
|
||||
:registered_collections: Ordered dictionary that maps worker nodes with
|
||||
their collection of tests gathered during test discovery.
|
||||
|
||||
::
|
||||
|
||||
registered_collections = {
|
||||
'<worker node A>': [
|
||||
'<full>/<path>/<to>/test_module.py::test_case1',
|
||||
'<full>/<path>/<to>/test_module.py::test_case2',
|
||||
],
|
||||
(...)
|
||||
}
|
||||
|
||||
:log: A py.log.Producer instance.
|
||||
|
||||
:config: Config object, used for handling hooks.
|
||||
"""
|
||||
|
||||
def __init__(self, config, log=None):
|
||||
self.numnodes = len(parse_spec_config(config))
|
||||
self.collection = None
|
||||
|
||||
self.workqueue = OrderedDict()
|
||||
self.assigned_work = OrderedDict()
|
||||
self.registered_collections = OrderedDict()
|
||||
|
||||
if log is None:
|
||||
self.log = Producer("loadscopesched")
|
||||
else:
|
||||
self.log = log.loadscopesched
|
||||
|
||||
self.config = config
|
||||
|
||||
@property
|
||||
def nodes(self):
|
||||
"""A list of all active nodes in the scheduler."""
|
||||
return list(self.assigned_work.keys())
|
||||
|
||||
@property
|
||||
def collection_is_completed(self):
|
||||
"""Boolean indication initial test collection is complete.
|
||||
|
||||
This is a boolean indicating all initial participating nodes have
|
||||
finished collection. The required number of initial nodes is defined
|
||||
by ``.numnodes``.
|
||||
"""
|
||||
return len(self.registered_collections) >= self.numnodes
|
||||
|
||||
@property
|
||||
def tests_finished(self):
|
||||
"""Return True if all tests have been executed by the nodes."""
|
||||
if not self.collection_is_completed:
|
||||
return False
|
||||
|
||||
if self.workqueue:
|
||||
return False
|
||||
|
||||
for assigned_unit in self.assigned_work.values():
|
||||
if self._pending_of(assigned_unit) >= 2:
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
@property
|
||||
def has_pending(self):
|
||||
"""Return True if there are pending test items.
|
||||
|
||||
This indicates that collection has finished and nodes are still
|
||||
processing test items, so this can be thought of as
|
||||
"the scheduler is active".
|
||||
"""
|
||||
if self.workqueue:
|
||||
return True
|
||||
|
||||
for assigned_unit in self.assigned_work.values():
|
||||
if self._pending_of(assigned_unit) > 0:
|
||||
return True
|
||||
|
||||
return False
|
||||
|
||||
def add_node(self, node):
|
||||
"""Add a new node to the scheduler.
|
||||
|
||||
From now on the node will be assigned work units to be executed.
|
||||
|
||||
Called by the ``DSession.worker_workerready`` hook when it successfully
|
||||
bootstraps a new node.
|
||||
"""
|
||||
assert node not in self.assigned_work
|
||||
self.assigned_work[node] = OrderedDict()
|
||||
|
||||
def remove_node(self, node):
|
||||
"""Remove a node from the scheduler.
|
||||
|
||||
This should be called either when the node crashed or at shutdown time.
|
||||
In the former case any pending items assigned to the node will be
|
||||
re-scheduled.
|
||||
|
||||
Called by the hooks:
|
||||
|
||||
- ``DSession.worker_workerfinished``.
|
||||
- ``DSession.worker_errordown``.
|
||||
|
||||
Return the item being executed while the node crashed or None if the
|
||||
node has no more pending items.
|
||||
"""
|
||||
workload = self.assigned_work.pop(node)
|
||||
if not self._pending_of(workload):
|
||||
return None
|
||||
|
||||
# The node crashed, identify test that crashed
|
||||
for work_unit in workload.values():
|
||||
for nodeid, completed in work_unit.items():
|
||||
if not completed:
|
||||
crashitem = nodeid
|
||||
break
|
||||
else:
|
||||
continue
|
||||
break
|
||||
else:
|
||||
raise RuntimeError(
|
||||
"Unable to identify crashitem on a workload with pending items"
|
||||
)
|
||||
|
||||
# Made uncompleted work unit available again
|
||||
self.workqueue.update(workload)
|
||||
|
||||
for node in self.assigned_work:
|
||||
self._reschedule(node)
|
||||
|
||||
return crashitem
|
||||
|
||||
def add_node_collection(self, node, collection):
|
||||
"""Add the collected test items from a node.
|
||||
|
||||
The collection is stored in the ``.registered_collections`` dictionary.
|
||||
|
||||
Called by the hook:
|
||||
|
||||
- ``DSession.worker_collectionfinish``.
|
||||
"""
|
||||
|
||||
# Check that add_node() was called on the node before
|
||||
assert node in self.assigned_work
|
||||
|
||||
# A new node has been added later, perhaps an original one died.
|
||||
if self.collection_is_completed:
|
||||
|
||||
# Assert that .schedule() should have been called by now
|
||||
assert self.collection
|
||||
|
||||
# Check that the new collection matches the official collection
|
||||
if collection != self.collection:
|
||||
|
||||
other_node = next(iter(self.registered_collections.keys()))
|
||||
|
||||
msg = report_collection_diff(
|
||||
self.collection, collection, other_node.gateway.id, node.gateway.id
|
||||
)
|
||||
self.log(msg)
|
||||
return
|
||||
|
||||
self.registered_collections[node] = list(collection)
|
||||
|
||||
def mark_test_complete(self, node, item_index, duration=0):
|
||||
"""Mark test item as completed by node.
|
||||
|
||||
Called by the hook:
|
||||
|
||||
- ``DSession.worker_testreport``.
|
||||
"""
|
||||
nodeid = self.registered_collections[node][item_index]
|
||||
scope = self._split_scope(nodeid)
|
||||
|
||||
self.assigned_work[node][scope][nodeid] = True
|
||||
self._reschedule(node)
|
||||
|
||||
def _assign_work_unit(self, node):
|
||||
"""Assign a work unit to a node."""
|
||||
assert self.workqueue
|
||||
|
||||
# Grab a unit of work
|
||||
scope, work_unit = self.workqueue.popitem(last=False)
|
||||
|
||||
# Keep track of the assigned work
|
||||
assigned_to_node = self.assigned_work.setdefault(node, default=OrderedDict())
|
||||
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
|
||||
]
|
||||
|
||||
node.send_runtest_some(nodeids_indexes)
|
||||
|
||||
def _split_scope(self, nodeid):
|
||||
"""Determine the scope (grouping) of a nodeid.
|
||||
|
||||
There are usually 3 cases for a nodeid::
|
||||
|
||||
example/loadsuite/test/test_beta.py::test_beta0
|
||||
example/loadsuite/test/test_delta.py::Delta1::test_delta0
|
||||
example/loadsuite/epsilon/__init__.py::epsilon.epsilon
|
||||
|
||||
#. Function in a test module.
|
||||
#. Method of a class in a test module.
|
||||
#. Doctest in a function in a package.
|
||||
|
||||
This function will group tests with the scope determined by splitting
|
||||
the first ``::`` from the right. That is, classes will be grouped in a
|
||||
single work unit, and functions from a test module will be grouped by
|
||||
their module. In the above example, scopes will be::
|
||||
|
||||
example/loadsuite/test/test_beta.py
|
||||
example/loadsuite/test/test_delta.py::Delta1
|
||||
example/loadsuite/epsilon/__init__.py
|
||||
"""
|
||||
return nodeid.rsplit("::", 1)[0]
|
||||
|
||||
def _pending_of(self, workload):
|
||||
"""Return the number of pending tests in a workload."""
|
||||
pending = sum(list(scope.values()).count(False) for scope in workload.values())
|
||||
return pending
|
||||
|
||||
def _reschedule(self, node):
|
||||
"""Maybe schedule new items on the node.
|
||||
|
||||
If there are any globally pending work units left then this will check
|
||||
if the given node should be given any more tests.
|
||||
"""
|
||||
|
||||
# Do not add more work to a node shutting down
|
||||
if node.shutting_down:
|
||||
return
|
||||
|
||||
# Check that more work is available
|
||||
if not self.workqueue:
|
||||
node.shutdown()
|
||||
return
|
||||
|
||||
self.log("Number of units waiting for node:", len(self.workqueue))
|
||||
|
||||
# Check that the node is almost depleted of work
|
||||
# 2: Heuristic of minimum tests to enqueue more work
|
||||
if self._pending_of(self.assigned_work[node]) > 2:
|
||||
return
|
||||
|
||||
# Pop one unit of work and assign it
|
||||
self._assign_work_unit(node)
|
||||
|
||||
def schedule(self):
|
||||
"""Initiate distribution of the test collection.
|
||||
|
||||
Initiate scheduling of the items across the nodes. If this gets called
|
||||
again later it behaves the same as calling ``._reschedule()`` on all
|
||||
nodes so that newly added nodes will start to be used.
|
||||
|
||||
If ``.collection_is_completed`` is True, this is called by the hook:
|
||||
|
||||
- ``DSession.worker_collectionfinish``.
|
||||
"""
|
||||
assert self.collection_is_completed
|
||||
|
||||
# Initial distribution already happened, reschedule on all nodes
|
||||
if self.collection is not None:
|
||||
for node in self.nodes:
|
||||
self._reschedule(node)
|
||||
return
|
||||
|
||||
# Check that all nodes collected the same tests
|
||||
if not self._check_nodes_have_same_collection():
|
||||
self.log("**Different tests collected, aborting run**")
|
||||
return
|
||||
|
||||
# Collections are identical, create the final list of items
|
||||
self.collection = list(next(iter(self.registered_collections.values())))
|
||||
if not self.collection:
|
||||
return
|
||||
|
||||
# Determine chunks of work (scopes)
|
||||
for nodeid in self.collection:
|
||||
scope = self._split_scope(nodeid)
|
||||
work_unit = self.workqueue.setdefault(scope, default=OrderedDict())
|
||||
work_unit[nodeid] = False
|
||||
|
||||
# Avoid having more workers than work
|
||||
extra_nodes = len(self.nodes) - len(self.workqueue)
|
||||
|
||||
if extra_nodes > 0:
|
||||
self.log("Shuting down {0} nodes".format(extra_nodes))
|
||||
|
||||
for _ in range(extra_nodes):
|
||||
unused_node, assigned = self.assigned_work.popitem(last=True)
|
||||
|
||||
self.log("Shuting down unused node {0}".format(unused_node))
|
||||
unused_node.shutdown()
|
||||
|
||||
# Assign initial workload
|
||||
for node in self.nodes:
|
||||
self._assign_work_unit(node)
|
||||
|
||||
# Ensure nodes start with at least two work units if possible (#277)
|
||||
for node in self.nodes:
|
||||
self._reschedule(node)
|
||||
|
||||
# Initial distribution sent all tests, start node shutdown
|
||||
if not self.workqueue:
|
||||
for node in self.nodes:
|
||||
node.shutdown()
|
||||
|
||||
def _check_nodes_have_same_collection(self):
|
||||
"""Return True if all nodes have collected the same items.
|
||||
|
||||
If collections differ, this method returns False while logging
|
||||
the collection differences and posting collection errors to
|
||||
pytest_collectreport hook.
|
||||
"""
|
||||
node_collection_items = list(self.registered_collections.items())
|
||||
first_node, col = node_collection_items[0]
|
||||
same_collection = True
|
||||
|
||||
for node, collection in node_collection_items[1:]:
|
||||
msg = report_collection_diff(
|
||||
col, collection, first_node.gateway.id, node.gateway.id
|
||||
)
|
||||
if not msg:
|
||||
continue
|
||||
|
||||
same_collection = False
|
||||
self.log(msg)
|
||||
|
||||
if self.config is None:
|
||||
continue
|
||||
|
||||
rep = CollectReport(node.gateway.id, "failed", longrepr=msg, result=[])
|
||||
self.config.hook.pytest_collectreport(report=rep)
|
||||
|
||||
return same_collection
|
||||
Reference in New Issue
Block a user