Skip to content

Commit 780a9d0

Browse files
committed
wait_for helper [wait_for]
1 parent d015d96 commit 780a9d0

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

tests/e2e/wait_for.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#pylint: disable=bare-except
2+
import time
3+
import pytest
4+
5+
def wait_for(fn):
6+
"""
7+
Keep retrying a function, catching any exceptions, until it returns something truthy,
8+
or we hit a timeout.
9+
"""
10+
timeout = time.time() + 3
11+
while time.time() < timeout:
12+
try:
13+
r = fn()
14+
if r:
15+
return r
16+
except:
17+
if time.time() > timeout:
18+
raise
19+
time.sleep(0.1)
20+
pytest.fail(f'function {fn} never returned anything truthy')

0 commit comments

Comments
 (0)