From 4843e41114190a768395251a6489b2d4c36e4632 Mon Sep 17 00:00:00 2001 From: cbornet Date: Thu, 13 Mar 2025 11:11:56 +0100 Subject: [PATCH] Detect blocking calls in coroutines using BlockBuster --- requirements-tests.txt | 1 + tests/conftest.py | 18 ++++++++++++++++++ tests/test_tutorial/conftest.py | 6 ++++++ 3 files changed, 25 insertions(+) create mode 100644 tests/conftest.py create mode 100644 tests/test_tutorial/conftest.py diff --git a/requirements-tests.txt b/requirements-tests.txt index 4a15844e4..c770306b5 100644 --- a/requirements-tests.txt +++ b/requirements-tests.txt @@ -11,6 +11,7 @@ PyJWT==2.8.0 pyyaml >=5.3.1,<7.0.0 passlib[bcrypt] >=1.7.2,<2.0.0 inline-snapshot==0.19.3 +blockbuster >= 1.5.23,<1.6.0 # types types-ujson ==5.10.0.20240515 types-orjson ==3.6.2 diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 000000000..a872a804f --- /dev/null +++ b/tests/conftest.py @@ -0,0 +1,18 @@ +import pytest +from blockbuster import blockbuster_ctx + + +@pytest.fixture(autouse=True) +def blockbuster(): + with blockbuster_ctx("fastapi") as bb: + bb.functions["io.BufferedReader.read"].can_block_in( + "starlette/testclient.py", "receive" + ) + for func in [ + "os.stat", + "io.TextIOWrapper.read", + ]: + bb.functions[func].can_block_in( + "pydantic/networks.py", "import_email_validator" + ) + yield bb diff --git a/tests/test_tutorial/conftest.py b/tests/test_tutorial/conftest.py new file mode 100644 index 000000000..b34ed8e38 --- /dev/null +++ b/tests/test_tutorial/conftest.py @@ -0,0 +1,6 @@ +import pytest + + +@pytest.fixture(autouse=True) +def deactivate_blockbuster(blockbuster): + blockbuster.deactivate()