Skip to content

Commit 9720edf

Browse files
committed
feat userver: check for *.sleep() in .py tests
commit_hash:6b46431b39e48616b9167d2c76ae583afded5d59
1 parent 1be113f commit 9720edf

File tree

5 files changed

+75
-0
lines changed

5 files changed

+75
-0
lines changed

.mapping.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4619,6 +4619,9 @@
46194619
"scripts/proto_structs/templates/types.inc.jinja":"taxi/uservices/userver/scripts/proto_structs/templates/types.inc.jinja",
46204620
"scripts/proto_structs/templates/types_forward_declarations.inc.jinja":"taxi/uservices/userver/scripts/proto_structs/templates/types_forward_declarations.inc.jinja",
46214621
"scripts/proto_structs/templates/utils.inc.jinja":"taxi/uservices/userver/scripts/proto_structs/templates/utils.inc.jinja",
4622+
"scripts/pylint/pylint_all.sh":"taxi/uservices/userver/scripts/pylint/pylint_all.sh",
4623+
"scripts/pylint/pylint_sleep_plugin.py":"taxi/uservices/userver/scripts/pylint/pylint_sleep_plugin.py",
4624+
"scripts/pylint/requirements.txt":"taxi/uservices/userver/scripts/pylint/requirements.txt",
46224625
"scripts/rabbitmq/ubuntu_install_rabbitmq_dev.sh":"taxi/uservices/userver/scripts/rabbitmq/ubuntu_install_rabbitmq_dev.sh",
46234626
"scripts/rabbitmq/ubuntu_install_rabbitmq_server.sh":"taxi/uservices/userver/scripts/rabbitmq/ubuntu_install_rabbitmq_server.sh",
46244627
"scripts/sql/__init__.py":"taxi/uservices/userver/scripts/sql/__init__.py",

CMakeLists.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,18 @@ if(USERVER_BUILD_TESTS)
366366
"-DUSERVER_GOOGLE_COMMON_PROTOS=${USERVER_GOOGLE_COMMON_PROTOS} -DUSERVER_DOWNLOAD_PACKAGES=${USERVER_DOWNLOAD_PACKAGES}"
367367
)
368368
endif()
369+
370+
include(UserverVenv)
371+
userver_venv_setup(
372+
NAME sleep_in_tests
373+
PYTHON_OUTPUT_VAR PYTHON_BINARY
374+
REQUIREMENTS "${USERVER_ROOT_DIR}/scripts/pylint/requirements.txt"
375+
UNIQUE
376+
)
377+
add_test(NAME sleep-in-tests
378+
COMMAND
379+
env PYTHON_BINARY=${PYTHON_BINARY} PYTHONPATH="${USERVER_ROOT_DIR}" "${USERVER_ROOT_DIR}/scripts/pylint/pylint_all.sh"
380+
)
369381
endif()
370382

371383
_userver_add_target_gen_component_schemas_docs()

scripts/pylint/pylint_all.sh

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/bin/sh
2+
3+
ROOT=$(dirname $0)/../..
4+
export PYTHONPATH="$ROOT/scripts/pylint"
5+
# set -e
6+
7+
find "$ROOT" -name '*.py' | \
8+
grep -v '^./build*' | \
9+
grep test | \
10+
xargs $PYTHON_BINARY -m pylint \
11+
--load-plugins=pylint_sleep_plugin \
12+
--disable all \
13+
--enable sleep-in-test \
14+
--ignore-paths ".*venv.*"
15+
${PY_FILES}
16+
17+
echo >&2 "Note: forcing test success (the test is not yet ready)"
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
from pylint.checkers import BaseRawFileChecker
2+
3+
4+
def read_from_node(node):
5+
return [line.decode('utf-8') for line in node.stream().readlines()]
6+
7+
8+
class SleepChecker(BaseRawFileChecker):
9+
name = 'sleep-in-test'
10+
priority = -1
11+
msgs = {
12+
'E9901': (
13+
(
14+
'Do not use %s in tests for synchronization! '
15+
'It only hides the problem and leads to flaky tests. '
16+
'If you have to wait for an event, either use testpoints '
17+
'or wait*() from pytest_userver.utils.sync module.'
18+
),
19+
'sleep-in-test',
20+
'Tests should not use *.sleep() functions for synchronization.',
21+
),
22+
}
23+
24+
def process_module(self, node):
25+
file_content = read_from_node(node)
26+
for line_num, line in enumerate(file_content):
27+
for func in ('time.sleep', 'asyncio.sleep'):
28+
self.check_line(line_num, line, func)
29+
30+
def check_line(self, line_num: int, line: str, func: str):
31+
if func not in line:
32+
return
33+
34+
self.add_message(
35+
'sleep-in-test',
36+
line=line_num,
37+
args=func,
38+
)
39+
40+
41+
def register(linter):
42+
linter.register_checker(SleepChecker(linter))

scripts/pylint/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pylint

0 commit comments

Comments
 (0)