Skip to content

Commit

Permalink
Use tenacity for wait_for_pools
Browse files Browse the repository at this point in the history
Signed-off-by: mulhern <amulhern@redhat.com>
  • Loading branch information
mulkieran committed Sep 19, 2024
1 parent fa60d1f commit 6621b96
Showing 1 changed file with 29 additions and 26 deletions.
55 changes: 29 additions & 26 deletions tests/client-dbus/tests/udev/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,13 @@
import dbus
import psutil
import pyudev
from tenacity import Retrying, retry_if_exception_type, stop_after_delay, wait_fixed
from tenacity import (
Retrying,
retry_if_exception_type,
stop_after_attempt,
stop_after_delay,
wait_fixed,
)

# isort: LOCAL
from stratisd_client_dbus import (
Expand Down Expand Up @@ -535,32 +541,29 @@ def wait_for_pools(self, expected_num, *, name=None):
:return: list of pool information found
:rtype: list of (str * MOPool)
"""
(count, limit, dbus_err, found_num, known_pools, start_time) = (
0,
expected_num + 1,
None,
None,
None,
time.time(),
)
while count < limit and not expected_num == found_num:
try:
known_pools = get_pools(name=name)
except dbus.exceptions.DBusException as err:
dbus_err = err

if known_pools is not None:
found_num = len(known_pools)

time.sleep(3)
count += 1

if found_num is None and dbus_err is not None:
try:
for attempt in Retrying(
retry=(retry_if_exception_type(dbus.exceptions.DBusException)),
wait=wait_fixed(3),
stop=stop_after_attempt(expected_num + 1),
reraise=True,
):
with attempt:
known_pools = get_pools(name=name)
if len(known_pools) == expected_num:
break
except dbus.exceptions.DBusException as err:
raise RuntimeError(
f"After {time.time() - start_time:.2f} seconds, the only "
"response is a D-Bus exception"
) from dbus_err
"Failed to obtain any information about pools from the D-Bus"
) from err

self.assertEqual(found_num, expected_num)
# Wait for the D-Bus to show more pools than expected. This can occur
# especially when the expected number of pools is zero, and stratisd
# receives the GetManagedObjects call immediately after it has set up
# the D-Bus connection and before object paths for every set up pool
# appear on the D-Bus.
time.sleep(3)
known_pools = get_pools(name=name)
self.assertEqual(len(known_pools), expected_num)

return known_pools

0 comments on commit 6621b96

Please sign in to comment.