-
Notifications
You must be signed in to change notification settings - Fork 68
FIX: orphaned commits + TEST: GC stateful #1940
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
ianhi
wants to merge
5
commits into
main
Choose a base branch
from
ian/gc-subset-test
base: main
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.
+220
−3
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a185b19
test: add test_subsets helper for focused stateful test runs
ianhi 4d25fd2
fix: expire_v2 orphan parent bug + focused expire tests
ianhi dc9c928
clean up
ianhi 265dc7f
add precondition
ianhi 55128db
Merge branch 'main' into ian/gc-subset-test
ianhi 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
117 changes: 117 additions & 0 deletions
117
icechunk-python/python/icechunk/testing/stateful_subsets.py
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 |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| """Helpers for running Hypothesis stateful tests on rule subsets. | ||
|
|
||
| Hypothesis doesn't provide an API for testing specific rule subsets | ||
| (see https://github.com/HypothesisWorks/hypothesis/issues/4682). | ||
|
|
||
| This module provides a workaround: dynamically subclass a state machine and | ||
| override excluded rules with plain methods, which strips the ``@rule`` marker | ||
| so Hypothesis ignores them. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import inspect | ||
| from collections.abc import Sequence | ||
|
|
||
| from hypothesis import settings as Settings | ||
| from hypothesis.stateful import ( | ||
| RuleBasedStateMachine, | ||
| run_state_machine_as_test, | ||
| ) | ||
|
|
||
| # Private Hypothesis constant — stable, needed to discover which methods are rules. | ||
| RULE_MARKER = "hypothesis_stateful_rule" | ||
| PRECONDITIONS_MARKER = "hypothesis_stateful_preconditions" | ||
|
|
||
|
|
||
| def test_subsets( | ||
| machine_cls: type[RuleBasedStateMachine], | ||
| *, | ||
| with_rules: Sequence[tuple[set[str], Settings | int]] = (), | ||
| without_rules: Sequence[tuple[set[str], Settings | int]] = (), | ||
| ) -> None: | ||
| """Run *machine_cls* multiple times, each time with a different rule subset. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| machine_cls : type[RuleBasedStateMachine] | ||
| The base state machine class. | ||
| with_rules : Sequence[tuple[set[str], Settings | int]] | ||
| Each entry is ``(rule_names_to_keep, settings_or_max_examples)``. | ||
| All rules **not** in the set are disabled for that run. | ||
| without_rules : Sequence[tuple[set[str], Settings | int]] | ||
| Each entry is ``(rule_names_to_remove, settings_or_max_examples)``. | ||
| The listed rules are disabled; everything else is kept. | ||
|
|
||
| Raises | ||
| ------ | ||
| ValueError | ||
| If a rule name doesn't exist on the machine. | ||
|
|
||
| Examples | ||
| -------- | ||
| Run only GC-related rules with 50 examples:: | ||
|
|
||
| test_subsets( | ||
| MyMachine, | ||
| with_rules=[ | ||
| ({"commit", "expire", "gc"}, settings(max_examples=50)), | ||
| ], | ||
| ) | ||
| """ | ||
| all_names = { | ||
| name | ||
| for name, f in inspect.getmembers(machine_cls) | ||
| if getattr(f, RULE_MARKER, None) is not None | ||
| } | ||
|
|
||
| for keep, s in with_rules: | ||
| _validate(keep, all_names) | ||
| _run_subset( | ||
| machine_cls, all_names, remove=all_names - keep, settings=_to_settings(s) | ||
| ) | ||
|
|
||
| for remove, s in without_rules: | ||
| _validate(remove, all_names) | ||
| _run_subset(machine_cls, all_names, remove=remove, settings=_to_settings(s)) | ||
|
|
||
|
|
||
| def _validate(names: set[str], all_names: set[str]) -> None: | ||
| unknown = names - all_names | ||
| if unknown: | ||
| raise ValueError(f"Unknown rules: {unknown}. Available: {sorted(all_names)}") | ||
|
|
||
|
|
||
| def _to_settings(s: Settings | int) -> Settings: | ||
| if isinstance(s, int): | ||
| return Settings(parent=Settings(), max_examples=s) | ||
| return s | ||
|
|
||
|
|
||
| def _run_subset( | ||
| machine_cls: type[RuleBasedStateMachine], | ||
| all_names: set[str], | ||
| remove: set[str], | ||
| settings: Settings, | ||
| ) -> None: | ||
| """Create a subclass with *remove* rules disabled, then run it.""" | ||
| if not remove: | ||
| raise ValueError("remove is empty — this would run the full machine unchanged") | ||
|
|
||
| def strip_rule(name: str): | ||
| """Walk the __wrapped__ chain until we find a function without the rule marker.""" | ||
| func = getattr(machine_cls, name) | ||
| while hasattr(func, RULE_MARKER) or hasattr(func, PRECONDITIONS_MARKER): | ||
| if not hasattr(func, "__wrapped__"): | ||
| raise ValueError( | ||
| f"{name} has {RULE_MARKER} or {PRECONDITIONS_MARKER} " | ||
| f"but no __wrapped__ — cannot strip rule" | ||
| ) | ||
| func = func.__wrapped__ | ||
| return func | ||
|
|
||
| overrides = {name: strip_rule(name) for name in remove} | ||
| kept = sorted(all_names - remove) | ||
| subset_name = f"{machine_cls.__name__}[{'+'.join(kept)}]" | ||
| subset_cls = type(subset_name, (machine_cls,), overrides) | ||
| run_state_machine_as_test(subset_cls, settings=settings) |
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
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.
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.
In #1932 i change the expectation so we assert that ancestry ends at a node with
parent_id == None.