|
3 | 3 | # Licensed under the MIT License. See LICENSE.txt in the project root for |
4 | 4 | # license information. |
5 | 5 | # ------------------------------------------------------------------------- |
| 6 | + |
| 7 | +import pytest |
| 8 | +import sys |
| 9 | +import asyncio |
| 10 | +from packaging.version import Version |
| 11 | +from unittest import mock |
| 12 | + |
| 13 | +import aiohttp |
| 14 | + |
6 | 15 | from azure.core.pipeline.transport import ( |
7 | 16 | AsyncHttpResponse as PipelineTransportAsyncHttpResponse, |
8 | 17 | AsyncHttpTransport, |
|
21 | 30 | ServiceRequestTimeoutError, |
22 | 31 | ServiceResponseTimeoutError, |
23 | 32 | ) |
| 33 | + |
24 | 34 | from utils import HTTP_REQUESTS, request_and_responses_product |
25 | | -import pytest |
26 | | -import sys |
27 | | -import asyncio |
28 | | -from unittest.mock import Mock |
29 | | -from packaging.version import Version |
30 | | -import aiohttp |
31 | 35 |
|
32 | 36 |
|
33 | 37 | # transport = mock.MagicMock(spec=AsyncHttpTransport) |
@@ -1049,47 +1053,66 @@ async def test_close_too_soon_works_fine(caplog, port, http_request): |
1049 | 1053 | assert result # No exception is good enough here |
1050 | 1054 |
|
1051 | 1055 |
|
1052 | | -@pytest.mark.skipif( |
1053 | | - Version(aiohttp.__version__) >= Version("3.10"), |
1054 | | - reason="aiohttp 3.10 introduced separate connection timeout", |
1055 | | -) |
1056 | 1056 | @pytest.mark.parametrize("http_request", HTTP_REQUESTS) |
1057 | 1057 | @pytest.mark.asyncio |
1058 | | -async def test_aiohttp_timeout_response(http_request): |
| 1058 | +async def test_aiohttp_timeout_response(port, http_request): |
1059 | 1059 | async with AioHttpTransport() as transport: |
1060 | | - transport.session._connector.connect = Mock(side_effect=asyncio.TimeoutError("Too slow!")) |
1061 | 1060 |
|
1062 | | - request = http_request("GET", f"http://localhost:12345/basic/string") |
| 1061 | + request = http_request("GET", f"http://localhost:{port}/basic/string") |
1063 | 1062 |
|
1064 | | - with pytest.raises(ServiceResponseTimeoutError) as err: |
1065 | | - await transport.send(request) |
| 1063 | + with mock.patch.object( |
| 1064 | + aiohttp.ClientResponse, "start", side_effect=asyncio.TimeoutError("Too slow!") |
| 1065 | + ) as mock_method: |
| 1066 | + with pytest.raises(ServiceResponseTimeoutError) as err: |
| 1067 | + await transport.send(request) |
1066 | 1068 |
|
1067 | | - with pytest.raises(ServiceResponseError) as err: |
1068 | | - await transport.send(request) |
| 1069 | + with pytest.raises(ServiceResponseError) as err: |
| 1070 | + await transport.send(request) |
1069 | 1071 |
|
1070 | | - stream_request = http_request("GET", f"http://localhost:12345/streams/basic") |
1071 | | - with pytest.raises(ServiceResponseTimeoutError) as err: |
1072 | | - await transport.send(stream_request, stream=True) |
| 1072 | + stream_resp = http_request("GET", f"http://localhost:{port}/streams/basic") |
| 1073 | + with pytest.raises(ServiceResponseTimeoutError) as err: |
| 1074 | + await transport.send(stream_resp, stream=True) |
| 1075 | + |
| 1076 | + stream_resp = await transport.send(stream_resp, stream=True) |
| 1077 | + with mock.patch.object( |
| 1078 | + aiohttp.streams.StreamReader, "read", side_effect=asyncio.TimeoutError("Too slow!") |
| 1079 | + ) as mock_method: |
| 1080 | + with pytest.raises(ServiceResponseTimeoutError) as err: |
| 1081 | + try: |
| 1082 | + # current HttpResponse |
| 1083 | + await stream_resp.read() |
| 1084 | + except AttributeError: |
| 1085 | + # legacy HttpResponse |
| 1086 | + b"".join([b async for b in stream_resp.stream_download(None)]) |
1073 | 1087 |
|
1074 | 1088 |
|
1075 | | -@pytest.mark.skipif( |
1076 | | - Version(aiohttp.__version__) < Version("3.10"), |
1077 | | - reason="aiohttp 3.10 introduced separate connection timeout", |
1078 | | -) |
1079 | 1089 | @pytest.mark.parametrize("http_request", HTTP_REQUESTS) |
1080 | 1090 | @pytest.mark.asyncio |
1081 | 1091 | async def test_aiohttp_timeout_request(http_request): |
1082 | 1092 | async with AioHttpTransport() as transport: |
1083 | | - transport.session._connector.connect = Mock(side_effect=asyncio.TimeoutError("Too slow!")) |
| 1093 | + transport.session._connector.connect = mock.Mock(side_effect=asyncio.TimeoutError("Too slow!")) |
1084 | 1094 |
|
1085 | 1095 | request = http_request("GET", f"http://localhost:12345/basic/string") |
1086 | 1096 |
|
1087 | | - with pytest.raises(ServiceRequestTimeoutError) as err: |
1088 | | - await transport.send(request) |
| 1097 | + # aiohttp 3.10 introduced separate connection timeout |
| 1098 | + if Version(aiohttp.__version__) >= Version("3.10"): |
| 1099 | + with pytest.raises(ServiceRequestTimeoutError) as err: |
| 1100 | + await transport.send(request) |
| 1101 | + |
| 1102 | + with pytest.raises(ServiceRequestError) as err: |
| 1103 | + await transport.send(request) |
| 1104 | + |
| 1105 | + stream_request = http_request("GET", f"http://localhost:12345/streams/basic") |
| 1106 | + with pytest.raises(ServiceRequestTimeoutError) as err: |
| 1107 | + await transport.send(stream_request, stream=True) |
| 1108 | + |
| 1109 | + else: |
| 1110 | + with pytest.raises(ServiceResponseTimeoutError) as err: |
| 1111 | + await transport.send(request) |
1089 | 1112 |
|
1090 | | - with pytest.raises(ServiceRequestError) as err: |
1091 | | - await transport.send(request) |
| 1113 | + with pytest.raises(ServiceResponseError) as err: |
| 1114 | + await transport.send(request) |
1092 | 1115 |
|
1093 | | - stream_request = http_request("GET", f"http://localhost:12345/streams/basic") |
1094 | | - with pytest.raises(ServiceRequestTimeoutError) as err: |
1095 | | - await transport.send(stream_request, stream=True) |
| 1116 | + stream_request = http_request("GET", f"http://localhost:12345/streams/basic") |
| 1117 | + with pytest.raises(ServiceResponseTimeoutError) as err: |
| 1118 | + await transport.send(stream_request, stream=True) |
0 commit comments