-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Generalize PYTEST_CURRENT_TEST for threading
#13837
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Pytest setting :ref:`PYTEST_CURRENT_TEST <pytest current test env>` internally is now thread-safe. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,6 +16,8 @@ | |
| import os | ||
| from pathlib import Path | ||
| import sys | ||
| import threading | ||
| from threading import Thread | ||
| from typing import final | ||
| from typing import Literal | ||
| from typing import overload | ||
|
|
@@ -581,6 +583,8 @@ def __init__(self, config: Config) -> None: | |
| self._initial_parts: list[CollectionArgument] = [] | ||
| self._collection_cache: dict[nodes.Collector, CollectReport] = {} | ||
| self.items: list[nodes.Item] = [] | ||
| # track the thread in which each item started execution in | ||
| self._item_to_thread: dict[nodes.Item, Thread] = {} | ||
|
|
||
| self._bestrelpathcache: dict[Path, str] = _bestrelpath_cache(config.rootpath) | ||
|
|
||
|
|
@@ -643,6 +647,22 @@ def startpath(self) -> Path: | |
| """ | ||
| return self.config.invocation_params.dir | ||
|
|
||
| def _readable_thread_id(self, thread: Thread) -> int: | ||
| # Returns a 0-indexed id of `thread`, corresponding to the order in | ||
| # which we saw that thread start executing tests. The main thread always | ||
| # has value 0, so non-main threads start at 1, even if the first thread | ||
| # we saw was a non-main thread, or even if we never saw the main thread | ||
| # execute tests. | ||
|
|
||
| # relying on item_to_thread to be sorted for stable ordering | ||
| threads = list(self._item_to_thread.values()) | ||
| assert thread in threads | ||
|
|
||
| if thread is threading.main_thread(): | ||
| return 0 | ||
|
|
||
| return threads.index(thread) + 1 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should not invent own thread numbering schemes There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should the envvars be named There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we should require a "pytest thread" name for a thread run to be picked no guessing, no random long integers - the env names should be something people can deal with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (i considered picking the threading thread name but their default is not ux friendly for env vars) |
||
|
|
||
| def _node_location_to_relpath(self, node_path: Path) -> str: | ||
| # bestrelpath is a quite slow function. | ||
| return self._bestrelpathcache[node_path] | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this ignore rule is duplicated farther down the file