Skip to content

Commit

Permalink
add iterate_timeout() for test usage
Browse files Browse the repository at this point in the history
  • Loading branch information
Shrews committed Aug 19, 2021
1 parent c293743 commit 3ad5396
Showing 1 changed file with 20 additions and 9 deletions.
29 changes: 20 additions & 9 deletions test/integration/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@
HERE = os.path.abspath(os.path.dirname(__file__))


def iterate_timeout(max_seconds, purpose, interval=2):
start = time.time()
count = 0
while (time.time() < start + max_seconds):
count += 1
yield count
time.sleep(interval)
raise Exception("Timeout waiting for %s" % purpose)


def ensure_directory(directory):
if not os.path.exists(directory):
os.makedirs(directory)
Expand Down Expand Up @@ -304,21 +314,22 @@ def test_playbook_start(skipif_pre_ansible28):
temp_dir]])
p.start()

time.sleep(5)

assert os.path.exists(os.path.join(temp_dir, 'pid'))
pid_path = os.path.join(temp_dir, 'pid')
for _ in iterate_timeout(30, "pid file creation"):
if os.path.exists(pid_path):
break

rc = main(['is-alive', temp_dir])
assert rc == 0
rc = main(['stop', temp_dir])
assert rc == 0

time.sleep(1)
for _ in iterate_timeout(30, "background process to stop"):
rc = main(['is-alive', temp_dir])
if rc == 1:
break

rc = main(['is-alive', temp_dir])
assert rc == 1

ensure_removed(os.path.join(temp_dir, 'pid'))
ensure_removed(pid_path)

rc = main(['stop', temp_dir])
assert rc == 1
assert rc == 1

0 comments on commit 3ad5396

Please sign in to comment.