Skip to content

Commit c70397f

Browse files
committed
feat testsuite: sync.wait_until()
commit_hash:5a66a1193a415700b1e66be8104fff80044e5c33
1 parent 138b972 commit c70397f

File tree

3 files changed

+60
-8
lines changed

3 files changed

+60
-8
lines changed

.mapping.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4785,6 +4785,7 @@
47854785
"testsuite/pytest_plugins/pytest_userver/utils/__init__.py":"taxi/uservices/userver/testsuite/pytest_plugins/pytest_userver/utils/__init__.py",
47864786
"testsuite/pytest_plugins/pytest_userver/utils/colorize.py":"taxi/uservices/userver/testsuite/pytest_plugins/pytest_userver/utils/colorize.py",
47874787
"testsuite/pytest_plugins/pytest_userver/utils/net.py":"taxi/uservices/userver/testsuite/pytest_plugins/pytest_userver/utils/net.py",
4788+
"testsuite/pytest_plugins/pytest_userver/utils/sync.py":"taxi/uservices/userver/testsuite/pytest_plugins/pytest_userver/utils/sync.py",
47884789
"testsuite/pytest_plugins/pytest_userver/utils/tskv.py":"taxi/uservices/userver/testsuite/pytest_plugins/pytest_userver/utils/tskv.py",
47894790
"testsuite/requirements-grpc-3.txt":"taxi/uservices/userver/testsuite/requirements-grpc-3.txt",
47904791
"testsuite/requirements-grpc-4.txt":"taxi/uservices/userver/testsuite/requirements-grpc-4.txt",

core/functional_tests/tracing/tests/conftest.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import asyncio
2-
31
import pytest
2+
import pytest_userver.utils.sync as sync
43

54
pytest_plugins = ['pytest_userver.plugins.core']
65

@@ -67,7 +66,6 @@ async def _check_the_files(check_trace_id: str):
6766
records = capture.select(trace_id=trace_id)
6867
assert len(records) >= 1, capture.select()
6968

70-
retries = 100
7169
required_data = {
7270
f'\ttrace_id={trace_id}',
7371
'service_name=http-tracing-test',
@@ -81,7 +79,10 @@ async def _check_the_files(check_trace_id: str):
8179
'}]',
8280
}
8381

84-
for _ in range(retries):
82+
probable_lines = []
83+
84+
async def check_ready() -> None:
85+
nonlocal probable_lines
8586
probable_lines = []
8687
with open(jaeger_logs_path, 'r') as jaeger_file:
8788
for line in reversed(jaeger_file.read().split('\n')):
@@ -90,8 +91,11 @@ async def _check_the_files(check_trace_id: str):
9091

9192
if all(substr in line for substr in required_data):
9293
return
94+
raise sync.NotReady()
9395

94-
await asyncio.sleep(0.5)
95-
assert False, (
96-
f'Missing substrings {required_data} in opentracing file for trace id {trace_id}. Lines:\n {probable_lines}'
97-
)
96+
try:
97+
await sync.wait_until(check_ready)
98+
except TimeoutError:
99+
assert False, (
100+
f'Missing substrings {required_data} in opentracing file for trace id {trace_id}. Lines:\n {probable_lines}'
101+
)
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import asyncio
2+
from collections.abc import Callable
3+
import datetime
4+
from typing import Any
5+
6+
MAX_WAIT_TIME = datetime.timedelta(seconds=30)
7+
ITERATION_PERIOD_SECONDS = 0.5
8+
9+
10+
class NotReady(Exception):
11+
pass
12+
13+
14+
async def wait_until(func: Callable) -> Any:
15+
"""
16+
Waits for some external event for MAX_WAIT_TIME and
17+
calls func every ITERATION_PERIOD_SECONDS.
18+
If func raises NotReady exception, the waiting continues.
19+
If func succesfully returns, wait_until() returns the same value.
20+
After MAX_WAIT_TIME of unsuccesfull checks wait_until()
21+
raises TimeoutError.
22+
23+
Example:
24+
25+
.. code-block:: python
26+
27+
async def try_to_connect_db():
28+
if not db_conn_is_ok():
29+
raise NotReady()
30+
31+
await wait_until(try_to_connect_db)
32+
33+
@throws TimeoutError after MAX_WAIT_TIME of unsuccesfull checks
34+
@note If possible, use @ref testpoint instead. @ref testpoint is
35+
an explicit message from the server "I'm ready", while wait_until()
36+
uses an implicit idea "A condition is met, so I can continue".
37+
38+
@ingroup userver_testsuite
39+
"""
40+
start = datetime.datetime.now()
41+
while datetime.datetime.now() - start < MAX_WAIT_TIME:
42+
try:
43+
return await func()
44+
except NotReady:
45+
await asyncio.sleep(ITERATION_PERIOD_SECONDS)
46+
47+
raise TimeoutError()

0 commit comments

Comments
 (0)