-
Notifications
You must be signed in to change notification settings - Fork 763
rptest/util: add check_consistently() safety helper #31013
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
nvartolomei
wants to merge
2
commits into
redpanda-data:dev
Choose a base branch
from
nvartolomei:nv/rptest-consistently
base: dev
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.
+145
−3
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
Some comments aren't visible on the classic Files Changed page.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| # Copyright 2026 Redpanda Data, Inc. | ||
| # | ||
| # Use of this software is governed by the Business Source License | ||
| # included in the file licenses/BSL.md | ||
| # | ||
| # As of the Change Date specified in that file, in accordance with | ||
| # the Business Source License, use of this software will be governed | ||
| # by the Apache License, Version 2.0 | ||
|
|
||
| import time | ||
|
|
||
| from ducktape.mark.resource import cluster | ||
| from ducktape.tests.test import Test | ||
|
|
||
| from rptest.util import ConditionNotHeldError, check_consistently, expect_exception | ||
|
|
||
|
|
||
| class CheckConsistentlyTest(Test): | ||
| @cluster(num_nodes=0) | ||
| def test_holds_across_window(self) -> None: | ||
| # A condition that stays true returns without raising, polled repeatedly. | ||
| calls = 0 | ||
|
|
||
| def cond() -> bool: | ||
| nonlocal calls | ||
| calls += 1 | ||
| return True | ||
|
|
||
| check_consistently(cond, duration_sec=0.1, interval_sec=0.005) | ||
| assert calls >= 2 | ||
|
|
||
| @cluster(num_nodes=0) | ||
| def test_slow_poll_forces_a_check_past_the_deadline(self) -> None: | ||
| calls = 0 | ||
|
|
||
| def cond() -> bool: | ||
| nonlocal calls | ||
| calls += 1 | ||
| if calls == 2: | ||
| # Begins inside the window but returns well after it. | ||
| time.sleep(0.5) | ||
| return True | ||
|
|
||
| check_consistently(cond, duration_sec=0.1, interval_sec=0) | ||
| # The slow second poll started before the deadline, so success requires | ||
| # a third poll -- one that begins after the deadline. | ||
| assert calls == 3 | ||
|
|
||
| @cluster(num_nodes=0) | ||
| def test_raises_when_condition_becomes_false(self) -> None: | ||
| # False from the start: fails on the first poll, before sleeping. | ||
| calls = 0 | ||
|
|
||
| def false_from_start() -> bool: | ||
| nonlocal calls | ||
| calls += 1 | ||
| return False | ||
|
|
||
| with expect_exception(ConditionNotHeldError, lambda _: True): | ||
| check_consistently(false_from_start, duration_sec=5, interval_sec=0.01) | ||
| assert calls == 1 | ||
|
|
||
| # Flips to false mid-window: raises as soon as it does. | ||
| calls = 0 | ||
|
|
||
| def flips() -> bool: | ||
| nonlocal calls | ||
| calls += 1 | ||
| return calls < 3 | ||
|
|
||
| with expect_exception(ConditionNotHeldError, lambda _: True): | ||
| check_consistently(flips, duration_sec=5, interval_sec=0.001) | ||
| assert calls == 3 | ||
|
|
||
| @cluster(num_nodes=0) | ||
| def test_check_failure_is_still_an_assertion_error(self) -> None: | ||
| with expect_exception(AssertionError, lambda _: True): | ||
| check_consistently(lambda: False, duration_sec=5, interval_sec=0.01) | ||
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
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.