Skip to content

Commit

Permalink
Bump blockbuster to 1.5.15 and revert some changes
Browse files Browse the repository at this point in the history
  • Loading branch information
cbornet committed Feb 8, 2025
1 parent d0c591f commit 106d765
Show file tree
Hide file tree
Showing 12 changed files with 116 additions and 124 deletions.
1 change: 1 addition & 0 deletions requirements/lint.in
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
aiodns
blockbuster
freezegun
mypy; implementation_name == "cpython"
pre-commit
Expand Down
4 changes: 4 additions & 0 deletions requirements/lint.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ annotated-types==0.7.0
# via pydantic
async-timeout==5.0.1
# via valkey
blockbuster==1.5.15
# via -r requirements/lint.in
cffi==1.17.1
# via
# cryptography
Expand All @@ -27,6 +29,8 @@ exceptiongroup==1.2.2
# via pytest
filelock==3.17.0
# via virtualenv
forbiddenfruit==0.1.4
# via blockbuster
freezegun==1.5.1
# via -r requirements/lint.in
identify==2.6.6
Expand Down
2 changes: 1 addition & 1 deletion requirements/test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ annotated-types==0.7.0
# via pydantic
async-timeout==5.0.1 ; python_version < "3.11"
# via -r requirements/runtime-deps.in
blockbuster==1.5.14
blockbuster==1.5.15
# via -r requirements/test.in
brotli==1.1.0 ; platform_python_implementation == "CPython"
# via -r requirements/runtime-deps.in
Expand Down
11 changes: 1 addition & 10 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,11 @@

@pytest.fixture(autouse=True)
def blockbuster() -> Iterator[None]:
with blockbuster_ctx("aiohttp") as bb:
with blockbuster_ctx("aiohttp", excluded_modules=["aiohttp.pytest_plugin", "aiohttp.test_utils"]) as bb:
bb.functions["io.TextIOWrapper.read"].can_block_in(
"aiohttp/client_reqrep.py", "update_auth"
)
bb.functions["os.stat"].can_block_in("aiohttp/client_reqrep.py", "update_auth")
bb.functions["os.stat"].can_block_in(
"asyncio/unix_events.py", "create_unix_server"
)
bb.functions["os.sendfile"].can_block_in(
"asyncio/unix_events.py", "_sock_sendfile_native_impl"
)
bb.functions["os.read"].can_block_in(
"asyncio/base_events.py", "subprocess_shell"
)
yield


Expand Down
13 changes: 5 additions & 8 deletions tests/test_client_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -3019,8 +3019,7 @@ async def close(self) -> None:
def create_server_for_url_and_handler(
aiohttp_server: AiohttpServer, tls_certificate_authority: trustme.CA
) -> Callable[[URL, Handler], Awaitable[TestServer]]:

async def create(url: URL, srv: Handler) -> TestServer:
def create(url: URL, srv: Handler) -> Awaitable[TestServer]:
app = web.Application()
app.router.add_route("GET", url.path, srv)

Expand All @@ -3030,9 +3029,9 @@ async def create(url: URL, srv: Handler) -> TestServer:
url.host, "localhost", "127.0.0.1"
)
ssl_ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
await asyncio.to_thread(cert.configure_cert, ssl_ctx)
return await aiohttp_server(app, ssl=ssl_ctx)
return await aiohttp_server(app)
cert.configure_cert(ssl_ctx)
return aiohttp_server(app, ssl=ssl_ctx)
return aiohttp_server(app)

return create

Expand Down Expand Up @@ -4166,9 +4165,7 @@ async def not_ok_handler(request: web.Request) -> NoReturn:

file_size_bytes = 1024 * 1024
file_path = tmp_path / "uploaded.txt"
await asyncio.to_thread(
file_path.write_text, "0" * file_size_bytes, encoding="utf8"
)
file_path.write_text("0" * file_size_bytes, encoding="utf8")

with open(file_path, "rb") as file:
data = {"file": file}
Expand Down
8 changes: 3 additions & 5 deletions tests/test_client_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -782,7 +782,7 @@ async def test_pass_falsy_data_file(
loop: asyncio.AbstractEventLoop, tmp_path: pathlib.Path
) -> None:
testfile = (tmp_path / "tmpfile").open("w+b")
await asyncio.to_thread(testfile.write, b"data")
testfile.write(b"data")
testfile.seek(0)
skip = frozenset([hdrs.CONTENT_TYPE])
req = ClientRequest(
Expand Down Expand Up @@ -971,11 +971,10 @@ async def test_chunked_transfer_encoding(

async def test_file_upload_not_chunked(loop: asyncio.AbstractEventLoop) -> None:
file_path = pathlib.Path(__file__).parent / "aiohttp.png"
file_stat = await asyncio.to_thread(file_path.stat)
with file_path.open("rb") as f:
req = ClientRequest("post", URL("http://python.org/"), data=f, loop=loop)
assert not req.chunked
assert req.headers["CONTENT-LENGTH"] == str(file_stat.st_size)
assert req.headers["CONTENT-LENGTH"] == str(file_path.stat().st_size)
await req.close()


Expand All @@ -997,11 +996,10 @@ async def test_precompressed_data_stays_intact(loop: asyncio.AbstractEventLoop)

async def test_file_upload_not_chunked_seek(loop: asyncio.AbstractEventLoop) -> None:
file_path = pathlib.Path(__file__).parent / "aiohttp.png"
file_stat = await asyncio.to_thread(file_path.stat)
with file_path.open("rb") as f:
f.seek(100)
req = ClientRequest("post", URL("http://python.org/"), data=f, loop=loop)
assert req.headers["CONTENT-LENGTH"] == str(file_stat.st_size - 100)
assert req.headers["CONTENT-LENGTH"] == str(file_path.stat().st_size - 100)
await req.close()


Expand Down
9 changes: 6 additions & 3 deletions tests/test_proxy_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -797,7 +797,8 @@ async def test_proxy_from_env_http_with_auth_from_netrc(
auth.login,
auth.password,
)
await asyncio.to_thread(netrc_file.write_text, netrc_file_data)
with netrc_file.open("w") as f:
f.write(netrc_file_data)
mocker.patch.dict(
os.environ, {"http_proxy": str(proxy.url), "NETRC": str(netrc_file)}
)
Expand All @@ -824,7 +825,8 @@ async def test_proxy_from_env_http_without_auth_from_netrc(
auth.login,
auth.password,
)
await asyncio.to_thread(netrc_file.write_text, netrc_file_data)
with netrc_file.open("w") as f:
f.write(netrc_file_data)
mocker.patch.dict(
os.environ, {"http_proxy": str(proxy.url), "NETRC": str(netrc_file)}
)
Expand All @@ -848,7 +850,8 @@ async def test_proxy_from_env_http_without_auth_from_wrong_netrc(
auth = aiohttp.BasicAuth("user", "pass")
netrc_file = tmp_path / "test_netrc"
invalid_data = f"machine 127.0.0.1 {auth.login} pass {auth.password}"
await asyncio.to_thread(netrc_file.write_text, invalid_data)
with netrc_file.open("w") as f:
f.write(invalid_data)

mocker.patch.dict(
os.environ, {"http_proxy": str(proxy.url), "NETRC": str(netrc_file)}
Expand Down
21 changes: 13 additions & 8 deletions tests/test_web_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -760,7 +760,8 @@ async def handler(request: web.Request) -> web.Response:
async def test_upload_file(aiohttp_client: AiohttpClient) -> None:
here = pathlib.Path(__file__).parent
fname = here / "aiohttp.png"
data = await asyncio.to_thread(fname.read_bytes)
with fname.open("rb") as f:
data = f.read()

async def handler(request: web.Request) -> web.Response:
form = await request.post()
Expand All @@ -784,7 +785,8 @@ async def handler(request: web.Request) -> web.Response:
async def test_upload_file_object(aiohttp_client: AiohttpClient) -> None:
here = pathlib.Path(__file__).parent
fname = here / "aiohttp.png"
data = await asyncio.to_thread(fname.read_bytes)
with fname.open("rb") as f:
data = f.read()

async def handler(request: web.Request) -> web.Response:
form = await request.post()
Expand Down Expand Up @@ -908,9 +910,12 @@ async def handler(request: web.Request) -> web.Response:


async def test_response_with_async_gen(
aiohttp_client: AiohttpClient, fname: pathlib.Path, file_content: bytes
aiohttp_client: AiohttpClient, fname: pathlib.Path
) -> None:
data_size = len(file_content)
with fname.open("rb") as f:
data = f.read()

data_size = len(data)

async def stream(f_name: pathlib.Path) -> AsyncIterator[bytes]:
with f_name.open("rb") as f:
Expand All @@ -930,7 +935,7 @@ async def handler(request: web.Request) -> web.Response:
resp = await client.get("/")
assert 200 == resp.status
resp_data = await resp.read()
assert resp_data == file_content
assert resp_data == data
assert resp.headers.get("Content-Length") == str(len(resp_data))

resp.release()
Expand Down Expand Up @@ -1312,8 +1317,8 @@ async def handler(request: web.Request) -> NoReturn:

resp.release()

file_content = await asyncio.to_thread((here / fname).read_bytes)
assert body == file_content
with (here / fname).open("rb") as f:
assert body == f.read()


async def test_subapp_app(aiohttp_client: AiohttpClient) -> None:
Expand Down Expand Up @@ -1797,7 +1802,7 @@ async def handler(request: web.Request) -> web.Response:
client = await aiohttp_client(app)

f = tmp_path / "foobar.txt"
await asyncio.to_thread(f.write_text, "test", encoding="utf8")
f.write_text("test", encoding="utf8")
with f.open("rb") as fd:
data = {"file": fd}
resp = await client.post("/", data=data)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_web_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -668,7 +668,7 @@ async def test_multipart_formdata_file(protocol: BaseProtocol) -> None:
)
result = await req.post()
assert hasattr(result["a_file"], "file")
content = await asyncio.to_thread(result["a_file"].file.read)
content = result["a_file"].file.read()
assert content == b"\ff"

req._finish()
Expand Down
6 changes: 1 addition & 5 deletions tests/test_web_response.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import asyncio
import collections.abc
import datetime
import gzip
Expand Down Expand Up @@ -512,8 +511,7 @@ async def test_change_content_threaded_compression_enabled_explicit() -> None:
req = make_request("GET", "/")
body_thread_size = 1024
body = b"answer" * body_thread_size
executor = ThreadPoolExecutor(1)
try:
with ThreadPoolExecutor(1) as executor:
resp = web.Response(
body=body, zlib_executor_size=body_thread_size, zlib_executor=executor
)
Expand All @@ -522,8 +520,6 @@ async def test_change_content_threaded_compression_enabled_explicit() -> None:
await resp.prepare(req)
assert resp._compressed_body is not None
assert gzip.decompress(resp._compressed_body) == body
finally:
await asyncio.to_thread(executor.shutdown)


async def test_change_content_length_if_compression_enabled() -> None:
Expand Down
Loading

0 comments on commit 106d765

Please sign in to comment.