Skip to content

Commit 52e181d

Browse files
committed
rptest/util: give check_consistently a distinct error type
Raise ConditionNotHeldError, a subclass of AssertionError, so callers can catch the specific failure while still treating it as an assertion.
1 parent 7880fc6 commit 52e181d

2 files changed

Lines changed: 18 additions & 5 deletions

File tree

tests/rptest/tests/util_test.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from ducktape.mark.resource import cluster
1313
from ducktape.tests.test import Test
1414

15-
from rptest.util import check_consistently, expect_exception
15+
from rptest.util import ConditionNotHeldError, check_consistently, expect_exception
1616

1717

1818
class CheckConsistentlyTest(Test):
@@ -56,7 +56,7 @@ def false_from_start() -> bool:
5656
calls += 1
5757
return False
5858

59-
with expect_exception(AssertionError, lambda _: True):
59+
with expect_exception(ConditionNotHeldError, lambda _: True):
6060
check_consistently(false_from_start, duration_sec=5, interval_sec=0.01)
6161
assert calls == 1
6262

@@ -68,6 +68,11 @@ def flips() -> bool:
6868
calls += 1
6969
return calls < 3
7070

71-
with expect_exception(AssertionError, lambda _: True):
71+
with expect_exception(ConditionNotHeldError, lambda _: True):
7272
check_consistently(flips, duration_sec=5, interval_sec=0.001)
7373
assert calls == 3
74+
75+
@cluster(num_nodes=0)
76+
def test_check_failure_is_still_an_assertion_error(self) -> None:
77+
with expect_exception(AssertionError, lambda _: True):
78+
check_consistently(lambda: False, duration_sec=5, interval_sec=0.01)

tests/rptest/util.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,12 @@ def wait_until_with_progress_check(
218218
) from last_exception
219219

220220

221+
class ConditionNotHeldError(AssertionError):
222+
"""Raised when `check_consistently` observes the checked condition returning false."""
223+
224+
pass
225+
226+
221227
def check_consistently(
222228
condition: Callable[[], bool],
223229
duration_sec: float,
@@ -232,7 +238,7 @@ def check_consistently(
232238
asserts that something good eventually happens (it blocks until a
233239
condition becomes true), whereas `check_consistently` asserts that nothing
234240
bad happens (it blocks while the condition *remains* true, raising
235-
AssertionError the moment it returns false).
241+
`ConditionNotHeldError` the moment it returns false).
236242
237243
`condition` is polled every `interval_sec`; the `duration_sec` window is
238244
measured from after the first poll, so at least `duration_sec` elapses
@@ -258,7 +264,9 @@ def check_consistently(
258264
def poll() -> None:
259265
if not condition():
260266
msg = err_msg() if callable(err_msg) else err_msg
261-
raise AssertionError(msg or f"condition did not hold for {duration_sec}s")
267+
raise ConditionNotHeldError(
268+
msg or f"condition did not hold for {duration_sec}s"
269+
)
262270

263271
poll()
264272
stop = time.monotonic() + duration_sec

0 commit comments

Comments
 (0)