Skip to content

Commit

Permalink
Better var name. Add missing test for coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkusSintonen committed Jun 11, 2024
1 parent 05b1844 commit 1358872
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 18 deletions.
18 changes: 10 additions & 8 deletions httpcore/_async/http11.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ def __init__(
our_role=h11.CLIENT,
max_incomplete_event_size=self.MAX_INCOMPLETE_EVENT_SIZE,
)
self._prev_socket_use_time = time.monotonic()
# Assuming we were just connected
self._network_stream_used_at = time.monotonic()

async def handle_async_request(self, request: Request) -> Response:
if not self.can_handle_request(request.url.origin):
Expand Down Expand Up @@ -177,7 +178,7 @@ async def _send_event(
bytes_to_send = self._h11_state.send(event)
if bytes_to_send is not None:
await self._network_stream.write(bytes_to_send, timeout=timeout)
self._prev_socket_use_time = time.monotonic()
self._network_stream_used_at = time.monotonic()

# Receiving the response...

Expand Down Expand Up @@ -229,7 +230,7 @@ async def _receive_event(
data = await self._network_stream.read(
self.READ_NUM_BYTES, timeout=timeout
)
self._prev_socket_use_time = time.monotonic()
self._network_stream_used_at = time.monotonic()

# If we feed this case through h11 we'll raise an exception like:
#
Expand Down Expand Up @@ -294,15 +295,16 @@ def has_expired(self) -> bool:
# only valid state is that the socket is about to return b"", indicating
# a server-initiated disconnect.
# Checking the readable status is relatively expensive so check it at a lower frequency.
if (now - self._prev_socket_use_time) > self._socket_poll_interval():
self._prev_socket_use_time = now
if (now - self._network_stream_used_at) > self._socket_poll_interval():
self._network_stream_used_at = now
server_disconnected = (
self._state == HTTPConnectionState.IDLE
and self._network_stream.get_extra_info("is_readable")
)
return server_disconnected
else:
return False
if server_disconnected:
return True

return False

def _socket_poll_interval(self) -> float:
# Randomize to avoid polling for all the connections at once
Expand Down
18 changes: 10 additions & 8 deletions httpcore/_sync/http11.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ def __init__(
our_role=h11.CLIENT,
max_incomplete_event_size=self.MAX_INCOMPLETE_EVENT_SIZE,
)
self._prev_socket_use_time = time.monotonic()
# Assuming we were just connected
self._network_stream_used_at = time.monotonic()

def handle_request(self, request: Request) -> Response:
if not self.can_handle_request(request.url.origin):
Expand Down Expand Up @@ -177,7 +178,7 @@ def _send_event(
bytes_to_send = self._h11_state.send(event)
if bytes_to_send is not None:
self._network_stream.write(bytes_to_send, timeout=timeout)
self._prev_socket_use_time = time.monotonic()
self._network_stream_used_at = time.monotonic()

# Receiving the response...

Expand Down Expand Up @@ -229,7 +230,7 @@ def _receive_event(
data = self._network_stream.read(
self.READ_NUM_BYTES, timeout=timeout
)
self._prev_socket_use_time = time.monotonic()
self._network_stream_used_at = time.monotonic()

# If we feed this case through h11 we'll raise an exception like:
#
Expand Down Expand Up @@ -294,15 +295,16 @@ def has_expired(self) -> bool:
# only valid state is that the socket is about to return b"", indicating
# a server-initiated disconnect.
# Checking the readable status is relatively expensive so check it at a lower frequency.
if (now - self._prev_socket_use_time) > self._socket_poll_interval():
self._prev_socket_use_time = now
if (now - self._network_stream_used_at) > self._socket_poll_interval():
self._network_stream_used_at = now
server_disconnected = (
self._state == HTTPConnectionState.IDLE
and self._network_stream.get_extra_info("is_readable")
)
return server_disconnected
else:
return False
if server_disconnected:
return True

return False

def _socket_poll_interval(self) -> float:
# Randomize to avoid polling for all the connections at once
Expand Down
39 changes: 38 additions & 1 deletion tests/_async/test_http11.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Any
from typing import Any, List

import pytest

Expand Down Expand Up @@ -158,6 +158,43 @@ async def test_http11_connection_with_local_protocol_error():
)


@pytest.mark.anyio
async def test_http11_has_expired_checks_readable_status():
class AsyncMockStreamReadable(httpcore.AsyncMockStream):
def __init__(self, buffer: List[bytes]) -> None:
super().__init__(buffer)
self.is_readable = False
self.checks = 0

def get_extra_info(self, info: str) -> Any:
if info == "is_readable":
self.checks += 1
return self.is_readable
return super().get_extra_info(info) # pragma: nocover

origin = httpcore.Origin(b"https", b"example.com", 443)
stream = AsyncMockStreamReadable(
[
b"HTTP/1.1 200 OK\r\n",
b"Content-Type: plain/text\r\n",
b"Content-Length: 13\r\n",
b"\r\n",
b"Hello, world!",
]
)
async with httpcore.AsyncHTTP11Connection(
origin=origin, stream=stream, socket_poll_interval_between=(0, 0)
) as conn:
response = await conn.request("GET", "https://example.com/")
assert response.status == 200

assert stream.checks == 0
assert not conn.has_expired()
stream.is_readable = True
assert conn.has_expired()
assert stream.checks == 2


@pytest.mark.anyio
@pytest.mark.parametrize("should_check", [True, False])
async def test_http11_has_expired_checks_readable_status_by_interval(
Expand Down
39 changes: 38 additions & 1 deletion tests/_sync/test_http11.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Any
from typing import Any, List

import pytest

Expand Down Expand Up @@ -159,6 +159,43 @@ def test_http11_connection_with_local_protocol_error():



def test_http11_has_expired_checks_readable_status():
class MockStreamReadable(httpcore.MockStream):
def __init__(self, buffer: List[bytes]) -> None:
super().__init__(buffer)
self.is_readable = False
self.checks = 0

def get_extra_info(self, info: str) -> Any:
if info == "is_readable":
self.checks += 1
return self.is_readable
return super().get_extra_info(info) # pragma: nocover

origin = httpcore.Origin(b"https", b"example.com", 443)
stream = MockStreamReadable(
[
b"HTTP/1.1 200 OK\r\n",
b"Content-Type: plain/text\r\n",
b"Content-Length: 13\r\n",
b"\r\n",
b"Hello, world!",
]
)
with httpcore.HTTP11Connection(
origin=origin, stream=stream, socket_poll_interval_between=(0, 0)
) as conn:
response = conn.request("GET", "https://example.com/")
assert response.status == 200

assert stream.checks == 0
assert not conn.has_expired()
stream.is_readable = True
assert conn.has_expired()
assert stream.checks == 2



@pytest.mark.parametrize("should_check", [True, False])
def test_http11_has_expired_checks_readable_status_by_interval(
monkeypatch, should_check
Expand Down

0 comments on commit 1358872

Please sign in to comment.