that need a given number of tries and some seconds to sleep between each
one of those tries.
- The most simple example possible will try 5 times sleeping for 5 seconds
- and increasing the sleep time by 5 each time::
+ The most simple example possible will try 10 times sleeping for 6 seconds:
>>> from teuthology.contexutil import safe_while
>>> with safe_while() as bomb:
indentation level and one extra call. Everything else stays the same,
code-wise. So adding this helper to existing code is simpler.
- The defaults are to start the sleeping time in seconds at 5s and to add
- 5 more seconds at every point in the loop. Setting the increment value to
- 0 makes the sleep time in seconds stay the same throughout the calls.
+ The defaults are to start the sleeping time at 6 seconds and try 10 times.
+ Setting the increment value will cause the sleep time to increase by that
+ value at each step.
"""
- def __init__(self, sleep=5, increment=5, tries=5, _sleeper=None):
+ def __init__(self, sleep=6, increment=0, tries=10, _sleeper=None):
self.sleep = sleep
self.increment = increment
self.tries = tries
self.fake_sleep = lambda s: True
self.s_while = contextutil.safe_while
- def test_5_5_5_deal(self):
+ def test_6_5_10_deal(self):
with raises(contextutil.MaxWhileTries):
with self.s_while(_sleeper=self.fake_sleep) as bomb:
while 1:
bomb()
- def test_5_5_1_deal(self):
+ def test_6_0_1_deal(self):
with raises(contextutil.MaxWhileTries) as error:
with self.s_while(
tries=1,
bomb()
msg = error.value[0]
- assert 'waiting for 5 seconds' in msg
+ assert 'waiting for 6 seconds' in msg
- def test_1_5_5_deal(self):
+ def test_1_0_10_deal(self):
with raises(contextutil.MaxWhileTries) as error:
with self.s_while(
sleep=1,
bomb()
msg = error.value[0]
- assert 'waiting for 55 seconds' in msg
+ assert 'waiting for 10 seconds' in msg
- def test_5_1_5_deal(self):
+ def test_6_1_10_deal(self):
with raises(contextutil.MaxWhileTries) as error:
with self.s_while(
increment=1,
bomb()
msg = error.value[0]
- assert 'waiting for 35 seconds' in msg
+ assert 'waiting for 105 seconds' in msg