Skip to content

Commit

Permalink
Fix linters (encode#550)
Browse files Browse the repository at this point in the history
  • Loading branch information
igor.stulikov authored and igor.stulikov committed Jan 29, 2023
1 parent e061990 commit a88274f
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 35 deletions.
4 changes: 3 additions & 1 deletion httpcore/_async/connection_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,9 @@ async def _attempt_to_acquire_connection(self, status: RequestStatus) -> bool:

# Attempt to close CONNECTING connections that no one needs
if self._is_pool_full:
for idx, connection in enumerate(self._pool): # Try to check old connections first
for idx, connection in enumerate(
self._pool
): # Try to check old connections first
if not connection.is_connecting():
continue
for req_status in self._requests:
Expand Down
4 changes: 3 additions & 1 deletion httpcore/_sync/connection_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,9 @@ def _attempt_to_acquire_connection(self, status: RequestStatus) -> bool:

# Attempt to close CONNECTING connections that no one needs
if self._is_pool_full:
for idx, connection in enumerate(self._pool): # Try to check old connections first
for idx, connection in enumerate(
self._pool
): # Try to check old connections first
if not connection.is_connecting():
continue
for req_status in self._requests:
Expand Down
23 changes: 13 additions & 10 deletions httpcore/backends/mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
import anyio

from httpcore import ReadTimeout
from .base import AsyncNetworkBackend, AsyncNetworkStream, NetworkBackend, NetworkStream

from .._exceptions import ReadError
from .base import AsyncNetworkBackend, AsyncNetworkStream, NetworkBackend, NetworkStream


class MockSSLObject:
Expand Down Expand Up @@ -59,10 +60,10 @@ def read(self, max_bytes: int, timeout: Optional[float] = None) -> bytes:

class MockBackend(NetworkBackend):
def __init__(
self,
buffer: typing.List[bytes],
http2: bool = False,
resp_stream_cls: Optional[Type[NetworkStream]] = None,
self,
buffer: typing.List[bytes],
http2: bool = False,
resp_stream_cls: Optional[Type[MockStream]] = None,
) -> None:
self._buffer = buffer
self._http2 = http2
Expand Down Expand Up @@ -127,14 +128,16 @@ async def read(self, max_bytes: int, timeout: Optional[float] = None) -> bytes:

class AsyncMockBackend(AsyncNetworkBackend):
def __init__(
self,
buffer: typing.List[bytes],
http2: bool = False,
resp_stream_cls: Optional[Type[AsyncNetworkStream]] = None,
self,
buffer: typing.List[bytes],
http2: bool = False,
resp_stream_cls: Optional[Type[AsyncMockStream]] = None,
) -> None:
self._buffer = buffer
self._http2 = http2
self._resp_stream_cls: Type[AsyncMockStream] = resp_stream_cls or AsyncMockStream
self._resp_stream_cls: Type[AsyncMockStream] = (
resp_stream_cls or AsyncMockStream
)

async def connect_tcp(
self,
Expand Down
44 changes: 27 additions & 17 deletions tests/_async/test_connection_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
AsyncConnectionPool,
ConnectError,
PoolTimeout,
ReadTimeout,
ReadError,
ReadTimeout,
UnsupportedProtocol,
)
from httpcore.backends.base import AsyncNetworkStream
from httpcore.backends.mock import AsyncMockBackend, AsyncHangingStream
from httpcore.backends.mock import AsyncHangingStream, AsyncMockBackend


@pytest.mark.anyio
Expand Down Expand Up @@ -511,19 +511,21 @@ async def test_pool_under_load():
"""
network_backend = AsyncMockBackend([], resp_stream_cls=AsyncHangingStream)

async def fetch(_pool: AsyncConnectionPool, *exceptions: Type[BaseException]):
async def fetch(
_pool: AsyncConnectionPool, *exceptions: Type[BaseException]
) -> None:
with contextlib.suppress(*exceptions):
async with pool.stream(
"GET",
"http://a.com/",
extensions={
"timeout": {
"connect": 0.1,
"read": 0.1,
"pool": 0.1,
"write": 0.1,
},
"GET",
"http://a.com/",
extensions={
"timeout": {
"connect": 0.1,
"read": 0.1,
"pool": 0.1,
"write": 0.1,
},
},
) as response:
await response.aread()

Expand All @@ -537,11 +539,12 @@ async def fetch(_pool: AsyncConnectionPool, *exceptions: Type[BaseException]):
nursery.start_soon(fetch, pool, PoolTimeout, ReadTimeout)
if pool.connections: # There is one connection in pool in "CONNECTING" state
assert pool.connections[0].is_connecting()
with pytest.raises(ReadTimeout): # ReadTimeout indicates that connection could be retrieved
with pytest.raises(
ReadTimeout
): # ReadTimeout indicates that connection could be retrieved
await fetch(pool)



@pytest.mark.trio
async def test_pool_timeout_connection_cleanup():
"""
Expand All @@ -555,7 +558,8 @@ async def test_pool_timeout_connection_cleanup():
b"Content-Length: 13\r\n",
b"\r\n",
b"Hello, world!",
] * 2,
]
* 2,
)

async with AsyncConnectionPool(
Expand All @@ -568,12 +572,18 @@ async def test_pool_timeout_connection_cleanup():
"write": 0.1,
}
with contextlib.suppress(PoolTimeout):
await pool.request("GET", "https://example.com/", extensions={"timeout": timeout})
await pool.request(
"GET", "https://example.com/", extensions={"timeout": timeout}
)

# wait for a considerable amount of time to make sure all requests time out
await concurrency.sleep(0.1)

await pool.request("GET", "https://example.com/", extensions={"timeout": {**timeout, 'pool': 0.1}})
await pool.request(
"GET",
"https://example.com/",
extensions={"timeout": {**timeout, "pool": 0.1}},
)

if pool.connections:
for conn in pool.connections:
Expand Down
11 changes: 5 additions & 6 deletions tests/_sync/test_connection_pool.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
import contextlib
import time
from typing import List, Optional, Type

import pytest
from tests import concurrency

from httpcore import (
ConnectionPool,
ConnectError,
PoolTimeout,
ReadTimeout,
ReadError,
ReadTimeout,
UnsupportedProtocol,
)
from httpcore.backends.base import NetworkStream
from httpcore.backends.mock import MockBackend, HangingStream
from tests import concurrency
from httpcore.backends.mock import HangingStream, MockBackend


def test_connection_pool_with_keepalive():
Expand Down Expand Up @@ -511,7 +510,7 @@ def test_pool_under_load():
"""
network_backend = MockBackend([], resp_stream_cls=HangingStream)

def fetch(_pool: ConnectionPool, *exceptions: Type[BaseException]):
def fetch(_pool: ConnectionPool, *exceptions: Type[BaseException]) -> None:
with contextlib.suppress(*exceptions):
with pool.stream(
"GET",
Expand Down Expand Up @@ -570,7 +569,7 @@ def test_pool_timeout_connection_cleanup():
pool.request("GET", "https://example.com/", extensions={"timeout": timeout})

# wait for a considerable amount of time to make sure all requests time out
time.sleep(0.1)
concurrency.sleep(0.1)

pool.request("GET", "https://example.com/", extensions={"timeout": {**timeout, 'pool': 0.1}})

Expand Down
5 changes: 5 additions & 0 deletions tests/concurrency.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
childen, because we don't need that for our use-case.
"""
import threading
import time
from types import TracebackType
from typing import Any, Callable, List, Optional, Type

Expand Down Expand Up @@ -38,3 +39,7 @@ def start_soon(self, func: Callable[..., object], *args: Any) -> None:

def open_nursery() -> Nursery:
return Nursery()


def sleep(seconds: float) -> None:
time.sleep(seconds)

0 comments on commit a88274f

Please sign in to comment.