Skip to content

Commit 345fafb

Browse files
timofey-barminPeter-Searby
authored andcommitted
MB-61592: [cluster_tests] Add support for retry_on_assert...
... in poll_for_condition Change-Id: If01543399d8fc8a2d23edb563825380a1df78366 Reviewed-on: https://review.couchbase.org/c/ns_server/+/232960 Well-Formed: Restriction Checker Well-Formed: Build Bot <[email protected]> Tested-by: Peter Searby <[email protected]> Reviewed-by: Timofey Barmin <[email protected]>
1 parent f973605 commit 345fafb

File tree

1 file changed

+26
-7
lines changed

1 file changed

+26
-7
lines changed

cluster_tests/testlib/testlib.py

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -509,28 +509,47 @@ def get_otp_nodes(cluster):
509509

510510
def poll_for_condition(fun, sleep_time, attempts=None, timeout=None,
511511
verbose=False, msg="poll for condition",
512-
retry_value=False):
512+
retry_value=False,
513+
retry_on_assert=False):
513514

514515
assert (attempts is not None) or (timeout is not None)
515516
assert sleep_time > 0, "non-positive sleep_time specified"
516517
start_time = time.time()
517518
sleep_time_str = f"{sleep_time:.2f}s"
518519

519520
attempt_count = 0
521+
exception_obj = None
520522
while (attempts is None) or (attempt_count < attempts):
521-
if timeout is not None:
522-
assert (time.time() - start_time) < timeout, \
523-
f"{msg}: timed-out (timeout: {timeout}s)"
524-
value = fun()
523+
if timeout is not None and ((time.time() - start_time) >= timeout):
524+
error_msg = f"{msg}: timed-out (timeout: {timeout}s)"
525+
if exception_obj is None:
526+
assert False, error_msg
527+
else:
528+
maybe_print(error_msg, verbose=verbose)
529+
raise exception_obj
530+
if retry_on_assert:
531+
try:
532+
value = fun()
533+
except AssertionError as e:
534+
exception_obj = e
535+
value = retry_value
536+
else:
537+
value = fun()
525538
if value is not retry_value:
526539
maybe_print(f"Time taken for condition to complete: "
527540
f"{time.time() - start_time: .2f}s", verbose=verbose)
528541
return value
529542
maybe_print(f"Sleeping for {sleep_time_str}", verbose=verbose)
530543
time.sleep(sleep_time)
531544
attempt_count += 1
532-
assert False, f"{msg} didn't complete in: {attempts} attempts, " \
533-
f"sleep_time: {sleep_time_str}"
545+
546+
error_msg = f"{msg} didn't complete in: {attempts} attempts, " \
547+
f"sleep_time: {sleep_time_str}"
548+
if exception_obj is None:
549+
assert False, error_msg
550+
else:
551+
maybe_print(error_msg, verbose=verbose)
552+
raise exception_obj
534553

535554
def metakv_get_succ(cluster, key, **kwargs):
536555
return get_succ(cluster, f"/_metakv{key}", **kwargs)

0 commit comments

Comments
 (0)