Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 1358872

Browse files
committedJun 11, 2024·
Better var name. Add missing test for coverage
1 parent 05b1844 commit 1358872

File tree

4 files changed

+96
-18
lines changed

4 files changed

+96
-18
lines changed
 

‎httpcore/_async/http11.py

+10-8
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ def __init__(
7171
our_role=h11.CLIENT,
7272
max_incomplete_event_size=self.MAX_INCOMPLETE_EVENT_SIZE,
7373
)
74-
self._prev_socket_use_time = time.monotonic()
74+
# Assuming we were just connected
75+
self._network_stream_used_at = time.monotonic()
7576

7677
async def handle_async_request(self, request: Request) -> Response:
7778
if not self.can_handle_request(request.url.origin):
@@ -177,7 +178,7 @@ async def _send_event(
177178
bytes_to_send = self._h11_state.send(event)
178179
if bytes_to_send is not None:
179180
await self._network_stream.write(bytes_to_send, timeout=timeout)
180-
self._prev_socket_use_time = time.monotonic()
181+
self._network_stream_used_at = time.monotonic()
181182

182183
# Receiving the response...
183184

@@ -229,7 +230,7 @@ async def _receive_event(
229230
data = await self._network_stream.read(
230231
self.READ_NUM_BYTES, timeout=timeout
231232
)
232-
self._prev_socket_use_time = time.monotonic()
233+
self._network_stream_used_at = time.monotonic()
233234

234235
# If we feed this case through h11 we'll raise an exception like:
235236
#
@@ -294,15 +295,16 @@ def has_expired(self) -> bool:
294295
# only valid state is that the socket is about to return b"", indicating
295296
# a server-initiated disconnect.
296297
# Checking the readable status is relatively expensive so check it at a lower frequency.
297-
if (now - self._prev_socket_use_time) > self._socket_poll_interval():
298-
self._prev_socket_use_time = now
298+
if (now - self._network_stream_used_at) > self._socket_poll_interval():
299+
self._network_stream_used_at = now
299300
server_disconnected = (
300301
self._state == HTTPConnectionState.IDLE
301302
and self._network_stream.get_extra_info("is_readable")
302303
)
303-
return server_disconnected
304-
else:
305-
return False
304+
if server_disconnected:
305+
return True
306+
307+
return False
306308

307309
def _socket_poll_interval(self) -> float:
308310
# Randomize to avoid polling for all the connections at once

‎httpcore/_sync/http11.py

+10-8
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ def __init__(
7171
our_role=h11.CLIENT,
7272
max_incomplete_event_size=self.MAX_INCOMPLETE_EVENT_SIZE,
7373
)
74-
self._prev_socket_use_time = time.monotonic()
74+
# Assuming we were just connected
75+
self._network_stream_used_at = time.monotonic()
7576

7677
def handle_request(self, request: Request) -> Response:
7778
if not self.can_handle_request(request.url.origin):
@@ -177,7 +178,7 @@ def _send_event(
177178
bytes_to_send = self._h11_state.send(event)
178179
if bytes_to_send is not None:
179180
self._network_stream.write(bytes_to_send, timeout=timeout)
180-
self._prev_socket_use_time = time.monotonic()
181+
self._network_stream_used_at = time.monotonic()
181182

182183
# Receiving the response...
183184

@@ -229,7 +230,7 @@ def _receive_event(
229230
data = self._network_stream.read(
230231
self.READ_NUM_BYTES, timeout=timeout
231232
)
232-
self._prev_socket_use_time = time.monotonic()
233+
self._network_stream_used_at = time.monotonic()
233234

234235
# If we feed this case through h11 we'll raise an exception like:
235236
#
@@ -294,15 +295,16 @@ def has_expired(self) -> bool:
294295
# only valid state is that the socket is about to return b"", indicating
295296
# a server-initiated disconnect.
296297
# Checking the readable status is relatively expensive so check it at a lower frequency.
297-
if (now - self._prev_socket_use_time) > self._socket_poll_interval():
298-
self._prev_socket_use_time = now
298+
if (now - self._network_stream_used_at) > self._socket_poll_interval():
299+
self._network_stream_used_at = now
299300
server_disconnected = (
300301
self._state == HTTPConnectionState.IDLE
301302
and self._network_stream.get_extra_info("is_readable")
302303
)
303-
return server_disconnected
304-
else:
305-
return False
304+
if server_disconnected:
305+
return True
306+
307+
return False
306308

307309
def _socket_poll_interval(self) -> float:
308310
# Randomize to avoid polling for all the connections at once

‎tests/_async/test_http11.py

+38-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Any
1+
from typing import Any, List
22

33
import pytest
44

@@ -158,6 +158,43 @@ async def test_http11_connection_with_local_protocol_error():
158158
)
159159

160160

161+
@pytest.mark.anyio
162+
async def test_http11_has_expired_checks_readable_status():
163+
class AsyncMockStreamReadable(httpcore.AsyncMockStream):
164+
def __init__(self, buffer: List[bytes]) -> None:
165+
super().__init__(buffer)
166+
self.is_readable = False
167+
self.checks = 0
168+
169+
def get_extra_info(self, info: str) -> Any:
170+
if info == "is_readable":
171+
self.checks += 1
172+
return self.is_readable
173+
return super().get_extra_info(info) # pragma: nocover
174+
175+
origin = httpcore.Origin(b"https", b"example.com", 443)
176+
stream = AsyncMockStreamReadable(
177+
[
178+
b"HTTP/1.1 200 OK\r\n",
179+
b"Content-Type: plain/text\r\n",
180+
b"Content-Length: 13\r\n",
181+
b"\r\n",
182+
b"Hello, world!",
183+
]
184+
)
185+
async with httpcore.AsyncHTTP11Connection(
186+
origin=origin, stream=stream, socket_poll_interval_between=(0, 0)
187+
) as conn:
188+
response = await conn.request("GET", "https://example.com/")
189+
assert response.status == 200
190+
191+
assert stream.checks == 0
192+
assert not conn.has_expired()
193+
stream.is_readable = True
194+
assert conn.has_expired()
195+
assert stream.checks == 2
196+
197+
161198
@pytest.mark.anyio
162199
@pytest.mark.parametrize("should_check", [True, False])
163200
async def test_http11_has_expired_checks_readable_status_by_interval(

‎tests/_sync/test_http11.py

+38-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Any
1+
from typing import Any, List
22

33
import pytest
44

@@ -159,6 +159,43 @@ def test_http11_connection_with_local_protocol_error():
159159

160160

161161

162+
def test_http11_has_expired_checks_readable_status():
163+
class MockStreamReadable(httpcore.MockStream):
164+
def __init__(self, buffer: List[bytes]) -> None:
165+
super().__init__(buffer)
166+
self.is_readable = False
167+
self.checks = 0
168+
169+
def get_extra_info(self, info: str) -> Any:
170+
if info == "is_readable":
171+
self.checks += 1
172+
return self.is_readable
173+
return super().get_extra_info(info) # pragma: nocover
174+
175+
origin = httpcore.Origin(b"https", b"example.com", 443)
176+
stream = MockStreamReadable(
177+
[
178+
b"HTTP/1.1 200 OK\r\n",
179+
b"Content-Type: plain/text\r\n",
180+
b"Content-Length: 13\r\n",
181+
b"\r\n",
182+
b"Hello, world!",
183+
]
184+
)
185+
with httpcore.HTTP11Connection(
186+
origin=origin, stream=stream, socket_poll_interval_between=(0, 0)
187+
) as conn:
188+
response = conn.request("GET", "https://example.com/")
189+
assert response.status == 200
190+
191+
assert stream.checks == 0
192+
assert not conn.has_expired()
193+
stream.is_readable = True
194+
assert conn.has_expired()
195+
assert stream.checks == 2
196+
197+
198+
162199
@pytest.mark.parametrize("should_check", [True, False])
163200
def test_http11_has_expired_checks_readable_status_by_interval(
164201
monkeypatch, should_check

0 commit comments

Comments
 (0)
Please sign in to comment.