-
Notifications
You must be signed in to change notification settings - Fork 101
feat: add --max-suite-retries option to cap total reruns across suite #332
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
Open
Borda
wants to merge
9
commits into
pytest-dev:master
Choose a base branch
from
Borda:feat/298
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
c43d05e
feat: add --max-suite-retries option to cap total reruns across suite
Borda d7e415f
Merge branch 'master' into feat/298
icemac 8b3e330
fix: use atomic try-increment-if-below-cap for suite rerun counter
Borda 0aeec55
fix: use public group.addoption for --max-suite-retries
Borda c48a198
lint: auto-fix violations after resolve cycle
Borda 9817e4b
refine: validate --max-suite-retries >= 0 at config time
Borda 9e46bcc
refine: read _suite_rerun_count under lock in get_suite_reruns
Borda e75460a
test: use assert_outcomes() in test_max_suite_retries_caps_total_reruns
Borda abdf2ea
lint: type annotations + mypy config for suite-retries additions
Borda File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,6 +10,7 @@ | |
| import traceback | ||
| import warnings | ||
| from contextlib import suppress | ||
| from typing import Any | ||
|
|
||
| import pytest | ||
| from _pytest.outcomes import fail | ||
|
|
@@ -120,6 +121,15 @@ def pytest_addoption(parser): | |
| "'rerun test summary info' section, which is emitted automatically " | ||
| "when this flag is set.", | ||
| ) | ||
| group.addoption( | ||
| "--max-suite-retries", | ||
| action="store", | ||
| dest="max_suite_retries", | ||
| type=int, | ||
| default=None, | ||
| help="Maximum total number of reruns across the entire test suite. " | ||
| "Once this limit is reached, no further reruns will occur.", | ||
| ) | ||
|
Comment on lines
+124
to
+132
Contributor
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'd also prefer to call it |
||
|
|
||
| arg_type = "string" | ||
| parser.addini("reruns", RERUNS_DESC, type=arg_type) | ||
|
|
@@ -134,6 +144,11 @@ def check_options(config): | |
| if config.option.reruns != 0: | ||
| if config.option.usepdb: # a core option | ||
| raise pytest.UsageError("--reruns incompatible with --pdb") | ||
| if ( | ||
| config.option.max_suite_retries is not None | ||
| and config.option.max_suite_retries < 0 | ||
| ): | ||
| raise pytest.UsageError("--max-suite-retries must be >= 0") | ||
|
Comment on lines
144
to
+151
|
||
|
|
||
|
|
||
| def _get_marker(item): | ||
|
|
@@ -427,9 +442,32 @@ def pytest_handlecrashitem(self, crashitem, report, sched): | |
| # and failures (set after each failure or crash) | ||
| # accessible from both the master and worker | ||
| class StatusDB: | ||
| def __init__(self): | ||
| self.delim = b"\n" | ||
| self.hmap = {} | ||
| def __init__(self) -> None: | ||
| self.delim: bytes = b"\n" | ||
| self.hmap: dict[str, str] = {} | ||
| self._suite_rerun_count: int = 0 | ||
| self._suite_lock: threading.Lock = threading.Lock() | ||
|
|
||
| def increment_suite_reruns(self) -> int: | ||
| """Atomically increment the suite-wide rerun counter; return new total.""" | ||
| with self._suite_lock: | ||
| self._suite_rerun_count += 1 | ||
| return self._suite_rerun_count | ||
|
|
||
| def try_increment_suite_reruns(self, max_cap: int) -> bool: | ||
| with self._suite_lock: | ||
| if self._suite_rerun_count < max_cap: | ||
| self._suite_rerun_count += 1 | ||
| return True | ||
| return False | ||
|
|
||
| def get_suite_reruns(self) -> int: | ||
| """Return the current suite-wide rerun count. | ||
|
|
||
| Reads under lock for thread safety. | ||
| """ | ||
| with self._suite_lock: | ||
| return self._suite_rerun_count | ||
|
|
||
| def _hash(self, crashitem: str) -> str: | ||
| if crashitem not in self.hmap: | ||
|
|
@@ -486,12 +524,12 @@ def _sock_send(self, conn, msg: str): | |
|
|
||
|
|
||
| class ServerStatusDB(SocketDB): | ||
| def __init__(self): | ||
| def __init__(self) -> None: | ||
| super().__init__() | ||
| self.sock.bind(("127.0.0.1", 0)) | ||
| self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) | ||
|
|
||
| self.rerunfailures_db = {} | ||
| self.rerunfailures_db: dict[str, dict[str, int]] = {} | ||
| t = threading.Thread(target=self.run_server, daemon=True) | ||
| t.start() | ||
|
|
||
|
|
@@ -514,6 +552,19 @@ def run_connection(self, conn): | |
| self._set(i, k, int(v)) | ||
| elif op == "get": | ||
| self._sock_send(conn, str(self._get(i, k))) | ||
| elif op == "inc": | ||
| with self._suite_lock: | ||
| new_v = self._get(i, k) + 1 | ||
| self._set(i, k, new_v) | ||
| self._sock_send(conn, str(new_v)) | ||
|
icemac marked this conversation as resolved.
|
||
| elif op == "try_inc": | ||
| with self._suite_lock: | ||
| current = self._get(i, k) | ||
| if current < int(v): | ||
| self._set(i, k, current + 1) | ||
| self._sock_send(conn, "1") | ||
| else: | ||
| self._sock_send(conn, "0") | ||
|
|
||
| def _set(self, i: str, k: str, v: int): | ||
| if i not in self.rerunfailures_db: | ||
|
|
@@ -526,6 +577,25 @@ def _get(self, i: str, k: str) -> int: | |
| except KeyError: | ||
| return 0 | ||
|
|
||
| def increment_suite_reruns(self) -> int: | ||
| """Atomically increment the suite-wide rerun counter; return new total.""" | ||
| with self._suite_lock: | ||
| new_v = self._get("__suite__", "r") + 1 | ||
| self._set("__suite__", "r", new_v) | ||
| return new_v | ||
|
|
||
| def try_increment_suite_reruns(self, max_cap: int) -> bool: | ||
| with self._suite_lock: | ||
| current = self._get("__suite__", "r") | ||
| if current < max_cap: | ||
| self._set("__suite__", "r", current + 1) | ||
| return True | ||
| return False | ||
|
|
||
| def get_suite_reruns(self) -> int: | ||
| """Return the current suite-wide rerun count.""" | ||
| return self._get("__suite__", "r") | ||
|
|
||
|
|
||
| class ClientStatusDB(SocketDB): | ||
| def __init__(self, sock_port): | ||
|
|
@@ -539,8 +609,23 @@ def _get(self, i: str, k: str) -> int: | |
| self._sock_send(self.sock, "|".join(("get", i, k, ""))) | ||
| return int(self._sock_recv(self.sock)) | ||
|
|
||
| def increment_suite_reruns(self) -> int: | ||
| """Atomically increment the suite-wide rerun counter; return new total.""" | ||
| self._sock_send(self.sock, "|".join(("inc", "__suite__", "r", ""))) | ||
| return int(self._sock_recv(self.sock)) | ||
|
|
||
| suspended_finalizers = {} | ||
| def try_increment_suite_reruns(self, max_cap: int) -> bool: | ||
| self._sock_send( | ||
| self.sock, "|".join(("try_inc", "__suite__", "r", str(max_cap))) | ||
| ) | ||
| return self._sock_recv(self.sock) == "1" | ||
|
|
||
| def get_suite_reruns(self) -> int: | ||
| """Return the current suite-wide rerun count.""" | ||
| return self._get("__suite__", "r") | ||
|
|
||
|
|
||
| suspended_finalizers: dict[Any, Any] = {} | ||
|
|
||
|
|
||
| def pytest_runtest_teardown(item, nextitem): | ||
|
|
@@ -638,6 +723,13 @@ def pytest_runtest_protocol(item, nextitem): | |
| item.ihook.pytest_runtest_logreport(report=report) | ||
| else: | ||
| # failure detected and reruns not exhausted, since i < reruns | ||
| max_suite_reruns = item.session.config.option.max_suite_retries | ||
| if max_suite_reruns is not None: | ||
| if not db.try_increment_suite_reruns(max_suite_reruns): | ||
| # suite-wide limit exhausted — log as final failure | ||
| item.ihook.pytest_runtest_logreport(report=report) | ||
| continue | ||
|
Borda marked this conversation as resolved.
Comment on lines
+726
to
+731
|
||
|
|
||
| report.outcome = "rerun" | ||
| time.sleep(delay) | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.