Skip to content

Commit

Permalink
Use blockbuster to detect blocking calls
Browse files Browse the repository at this point in the history
  • Loading branch information
cbornet committed Dec 17, 2024
1 parent af491fc commit 0d5c175
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 12 deletions.
28 changes: 26 additions & 2 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ pytest-httpserver = "~1.0.8"
testcontainers = "~3.7.1"
ruff = "^0.6.8"
types-toml = "^0.10.8.7"
blockbuster = "~1.5.0"

[build-system]
requires = ["poetry-core>=1.0.0"]
Expand Down
24 changes: 24 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@

import functools
import warnings
from collections.abc import Iterator
from typing import Any, Awaitable, Callable, TypedDict

import pytest
from blockbuster import BlockBuster, blockbuster_ctx
from deprecation import UnsupportedWarning

from astrapy import DataAPIClient
Expand Down Expand Up @@ -51,6 +53,28 @@
)


@pytest.fixture(autouse=True)
def blockbuster() -> Iterator[BlockBuster]:
with blockbuster_ctx() as bb:
bb.functions["os.stat"].can_block_functions.extend(
[
# TODO: follow discussion in https://github.com/encode/httpx/discussions/3456
("httpx/_client.py", {"_init_transport"}),
("coverage/control.py", {"_should_trace"}),
]
)
bb.functions["os.path.abspath"].can_block_functions.extend(
[
("coverage/control.py", {"_should_trace"}),
("_pytest/assertion/rewrite.py", {"_should_rewrite"}),
]
)
bb.functions["os.mkdir"].can_block_functions.append(
("_pytest/assertion/rewrite.py", {"try_makedirs"})
)
yield bb


class DataAPICredentials(TypedDict):
token: str | TokenProvider
api_endpoint: str
Expand Down
3 changes: 2 additions & 1 deletion tests/idiomatic/integration/test_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

from __future__ import annotations

import asyncio
import time
from typing import Any, Awaitable, Callable

Expand Down Expand Up @@ -73,7 +74,7 @@ async def await_until_true(
while not (await acondition()):
if time.time() - ini_time > max_seconds:
raise ValueError("Timed out on condition.")
time.sleep(poll_interval)
await asyncio.sleep(poll_interval)


@pytest.mark.skipif(not IS_ASTRA_DB, reason="Not supported outside of Astra DB")
Expand Down
14 changes: 7 additions & 7 deletions tests/idiomatic/integration/test_ddl_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

from __future__ import annotations

import time
import asyncio

import pytest

Expand Down Expand Up @@ -146,7 +146,7 @@ async def test_collection_default_id_type_async(
assert isinstance(doc["_id"], UUID)
await acol.drop()

time.sleep(2)
await asyncio.sleep(2)
acol = await async_database.create_collection(
ID_TEST_COLLECTION_NAME_ROOT + DefaultIdType.UUIDV6,
definition=CollectionDefinition(
Expand All @@ -168,7 +168,7 @@ async def test_collection_default_id_type_async(
assert doc["_id"].version == 6
await acol.drop()

time.sleep(2)
await asyncio.sleep(2)
acol = await async_database.create_collection(
ID_TEST_COLLECTION_NAME_ROOT + DefaultIdType.UUIDV7,
definition=CollectionDefinition(
Expand All @@ -190,7 +190,7 @@ async def test_collection_default_id_type_async(
assert doc["_id"].version == 7
await acol.drop()

time.sleep(2)
await asyncio.sleep(2)
acol = await async_database.create_collection(
ID_TEST_COLLECTION_NAME_ROOT + DefaultIdType.DEFAULT,
definition=CollectionDefinition(
Expand All @@ -205,7 +205,7 @@ async def test_collection_default_id_type_async(
assert acol_options.default_id.default_id_type == DefaultIdType.DEFAULT
await acol.drop()

time.sleep(2)
await asyncio.sleep(2)
acol = await async_database.create_collection(
ID_TEST_COLLECTION_NAME_ROOT + DefaultIdType.OBJECTID,
definition=CollectionDefinition(
Expand Down Expand Up @@ -429,7 +429,7 @@ async def test_tokenless_client_async(
@pytest.mark.describe(
"test database-from-admin default keyspace per environment, async"
)
async def test_database_from_admin_default_keyspace_per_environment_async(
def test_async_database_from_admin_default_keyspace_per_environment(
self,
data_api_credentials_kwargs: DataAPICredentials,
data_api_credentials_info: DataAPICredentialsInfo,
Expand All @@ -448,7 +448,7 @@ async def test_database_from_admin_default_keyspace_per_environment_async(
@pytest.mark.describe(
"test database-from-astradbadmin default keyspace per environment, async"
)
async def test_database_from_astradbadmin_default_keyspace_per_environment_async(
def test_async_database_from_astradbadmin_default_keyspace_per_environment(
self,
data_api_credentials_kwargs: DataAPICredentials,
data_api_credentials_info: DataAPICredentialsInfo,
Expand Down
4 changes: 2 additions & 2 deletions tests/idiomatic/integration/test_timeout_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,11 @@ async def test_cursor_overalltimeout_exceptions_async(
acol = async_empty_collection
await acol.insert_many([{"a": 1}] * 1000)

await acol.distinct("a", timeout_ms=20000)
await acol.distinct("a", timeout_ms=60000)
with pytest.raises(DataAPITimeoutException):
await acol.distinct("a", timeout_ms=1)

await acol.distinct("a", timeout_ms=20000)
await acol.distinct("a", timeout_ms=60000)
with pytest.raises(DataAPITimeoutException):
await acol.distinct("a", timeout_ms=1)

Expand Down

0 comments on commit 0d5c175

Please sign in to comment.