Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Detect blocking calls in coroutines during tests using BlockBuster #3486

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ venv*/
.python-version
build/
dist/
.idea/
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,4 @@ trio-typing==0.10.0
trustme==1.1.0; python_version < '3.9'
trustme==1.2.0; python_version >= '3.9'
uvicorn==0.32.1
blockbuster==1.5.10
8 changes: 8 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import pytest
import trustme
from blockbuster import blockbuster_ctx
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives.serialization import (
BestAvailableEncryption,
Expand All @@ -31,6 +32,13 @@
}


@pytest.fixture(autouse=True)
def blockbuster():
with blockbuster_ctx() as bb:
bb.functions["os.stat"].can_block_in("/mimetypes.py", "init")
yield bb


@pytest.fixture(scope="function", autouse=True)
def clean_environ():
"""Keeps os.environ clean for every test without having to mock os.environ"""
Expand Down
4 changes: 3 additions & 1 deletion tests/models/test_whatwg.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@
# https://url.spec.whatwg.org/

import json
from pathlib import Path

import pytest

from httpx._urlparse import urlparse

# URL test cases from...
# https://github.com/web-platform-tests/wpt/blob/master/url/resources/urltestdata.json
with open("tests/models/whatwg.json", "r", encoding="utf-8") as input:
whatwg_path = Path(__file__).parent.parent / "models" / "whatwg.json"
with whatwg_path.open("r", encoding="utf-8") as input:
test_cases = json.load(input)
test_cases = [
item
Expand Down