Skip to content

Commit a0ade99

Browse files
Improve test
1 parent c3db70d commit a0ade99

File tree

2 files changed

+76
-38
lines changed

2 files changed

+76
-38
lines changed

tests/_async/test_connection_pool.py

Lines changed: 38 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -703,41 +703,60 @@ def get_extra_info(self, info: str) -> typing.Any:
703703
return self.mock_is_readable
704704
return super().get_extra_info(info) # pragma: nocover
705705

706+
streams = [
707+
MockStream(
708+
[
709+
b"HTTP/1.1 200 OK\r\n",
710+
b"Content-Type: plain/text\r\n",
711+
b"Content-Length: 15\r\n",
712+
b"\r\n",
713+
b"Hello, world 1!",
714+
b"HTTP/1.1 200 OK\r\n",
715+
b"Content-Type: plain/text\r\n",
716+
b"Content-Length: 15\r\n",
717+
b"\r\n",
718+
b"Hello, world 2!",
719+
]
720+
),
721+
MockStream(
722+
[
723+
b"HTTP/1.1 200 OK\r\n",
724+
b"Content-Type: plain/text\r\n",
725+
b"Content-Length: 29\r\n",
726+
b"\r\n",
727+
b"Hello, world from new stream!",
728+
]
729+
),
730+
]
731+
706732
class MockBackend(httpcore.AsyncMockBackend):
707733
async def connect_tcp(
708734
self, *args: typing.Any, **kwargs: typing.Any
709735
) -> MockStream:
710-
return MockStream(list(self._buffer))
736+
return streams.pop(0)
711737

712-
network_backend = MockBackend(
713-
[
714-
b"HTTP/1.1 200 OK\r\n",
715-
b"Content-Type: plain/text\r\n",
716-
b"Content-Length: 13\r\n",
717-
b"\r\n",
718-
b"Hello, world!",
719-
b"HTTP/1.1 200 OK\r\n",
720-
b"Content-Type: plain/text\r\n",
721-
b"Content-Length: 13\r\n",
722-
b"\r\n",
723-
b"Hello, world!",
724-
]
725-
)
726738
async with httpcore.AsyncConnectionPool(
727-
network_backend=network_backend, max_connections=1
739+
network_backend=MockBackend([]), max_connections=1
728740
) as pool:
729-
await pool.request("GET", "https://example.com/")
741+
res = await pool.request("GET", "https://example.com/")
742+
assert (await res.aread()) == b"Hello, world 1!"
743+
730744
assert len(pool.connections) == 1
731745
conn = pool.connections[0]
732-
await pool.request("GET", "https://example.com/")
746+
747+
res = await pool.request("GET", "https://example.com/")
748+
assert (await res.aread()) == b"Hello, world 2!"
749+
733750
assert len(pool.connections) == 1
734751
assert conn is pool.connections[0], "Should reuse connection"
735752

736753
# Simulate network breakage
737754
assert conn.is_idle()
738755
conn._connection._network_stream.mock_is_readable = True # type: ignore[attr-defined]
739756

740-
await pool.request("GET", "https://example.com/")
757+
res = await pool.request("GET", "https://example.com/")
758+
assert (await res.aread()) == b"Hello, world from new stream!"
759+
741760
assert len(pool.connections) == 1
742761
new_conn = pool.connections[0]
743762
assert new_conn is not conn, "Should be a new connection"

tests/_sync/test_connection_pool.py

Lines changed: 38 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -703,41 +703,60 @@ def get_extra_info(self, info: str) -> typing.Any:
703703
return self.mock_is_readable
704704
return super().get_extra_info(info) # pragma: nocover
705705

706+
streams = [
707+
MockStream(
708+
[
709+
b"HTTP/1.1 200 OK\r\n",
710+
b"Content-Type: plain/text\r\n",
711+
b"Content-Length: 15\r\n",
712+
b"\r\n",
713+
b"Hello, world 1!",
714+
b"HTTP/1.1 200 OK\r\n",
715+
b"Content-Type: plain/text\r\n",
716+
b"Content-Length: 15\r\n",
717+
b"\r\n",
718+
b"Hello, world 2!",
719+
]
720+
),
721+
MockStream(
722+
[
723+
b"HTTP/1.1 200 OK\r\n",
724+
b"Content-Type: plain/text\r\n",
725+
b"Content-Length: 29\r\n",
726+
b"\r\n",
727+
b"Hello, world from new stream!",
728+
]
729+
),
730+
]
731+
706732
class MockBackend(httpcore.MockBackend):
707733
def connect_tcp(
708734
self, *args: typing.Any, **kwargs: typing.Any
709735
) -> MockStream:
710-
return MockStream(list(self._buffer))
736+
return streams.pop(0)
711737

712-
network_backend = MockBackend(
713-
[
714-
b"HTTP/1.1 200 OK\r\n",
715-
b"Content-Type: plain/text\r\n",
716-
b"Content-Length: 13\r\n",
717-
b"\r\n",
718-
b"Hello, world!",
719-
b"HTTP/1.1 200 OK\r\n",
720-
b"Content-Type: plain/text\r\n",
721-
b"Content-Length: 13\r\n",
722-
b"\r\n",
723-
b"Hello, world!",
724-
]
725-
)
726738
with httpcore.ConnectionPool(
727-
network_backend=network_backend, max_connections=1
739+
network_backend=MockBackend([]), max_connections=1
728740
) as pool:
729-
pool.request("GET", "https://example.com/")
741+
res = pool.request("GET", "https://example.com/")
742+
assert (res.read()) == b"Hello, world 1!"
743+
730744
assert len(pool.connections) == 1
731745
conn = pool.connections[0]
732-
pool.request("GET", "https://example.com/")
746+
747+
res = pool.request("GET", "https://example.com/")
748+
assert (res.read()) == b"Hello, world 2!"
749+
733750
assert len(pool.connections) == 1
734751
assert conn is pool.connections[0], "Should reuse connection"
735752

736753
# Simulate network breakage
737754
assert conn.is_idle()
738755
conn._connection._network_stream.mock_is_readable = True # type: ignore[attr-defined]
739756

740-
pool.request("GET", "https://example.com/")
757+
res = pool.request("GET", "https://example.com/")
758+
assert (res.read()) == b"Hello, world from new stream!"
759+
741760
assert len(pool.connections) == 1
742761
new_conn = pool.connections[0]
743762
assert new_conn is not conn, "Should be a new connection"

0 commit comments

Comments
 (0)