Skip to content

Commit

Permalink
Detect blocking calls in coroutines during tests using BlockBuster
Browse files Browse the repository at this point in the history
  • Loading branch information
cbornet committed Jan 30, 2025
1 parent 10b7295 commit f0118c4
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 1 deletion.
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
9 changes: 9 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import asyncio
import json
import mimetypes
import os
import threading
import time
import typing

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 +33,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

0 comments on commit f0118c4

Please sign in to comment.