Skip to content

Commit 023c92d

Browse files
committed
Put back timeout check on ccip read tests; close sessions appropriately.
1 parent b7ffd70 commit 023c92d

File tree

3 files changed

+28
-6
lines changed

3 files changed

+28
-6
lines changed

web3/_utils/module_testing/module_testing_utils.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@
1212
Union,
1313
)
1414

15-
import aiohttp
15+
from aiohttp import (
16+
ClientSession,
17+
ClientTimeout,
18+
)
1619
from eth_typing import (
1720
ChecksumAddress,
1821
HexStr,
@@ -28,14 +31,19 @@
2831
)
2932
import requests
3033

34+
from web3._utils.http import (
35+
DEFAULT_HTTP_TIMEOUT,
36+
)
3137
from web3.types import (
3238
BlockData,
3339
LogReceipt,
3440
)
3541

3642
if TYPE_CHECKING:
3743
from _pytest.monkeypatch import MonkeyPatch # noqa: F401
38-
from aiohttp import ClientResponse # noqa: F401
44+
from aiohttp import ( # noqa: F401
45+
ClientResponse,
46+
)
3947
from requests import Response # noqa: F401
4048

4149
from web3 import Web3 # noqa: F401
@@ -97,6 +105,7 @@ def _mock_specific_request(
97105

98106
# mock response only to specified url while validating appropriate fields
99107
if url_from_args == mocked_request_url:
108+
assert kwargs["timeout"] == DEFAULT_HTTP_TIMEOUT
100109
if http_method.upper() == "POST":
101110
assert kwargs["data"] == {"data": calldata, "sender": sender}
102111
return MockedResponse()
@@ -146,15 +155,18 @@ async def _mock_specific_request(
146155

147156
# mock response only to specified url while validating appropriate fields
148157
if url_from_args == mocked_request_url:
158+
assert kwargs["timeout"] == ClientTimeout(DEFAULT_HTTP_TIMEOUT)
149159
if http_method.upper() == "post":
150160
assert kwargs["data"] == {"data": calldata, "sender": sender}
151161
return AsyncMockedResponse()
152162

153163
# else, make a normal request (no mocking)
154-
session = aiohttp.ClientSession()
155-
return await session.request(
164+
session = ClientSession()
165+
response = await session.request(
156166
method=http_method.upper(), url=url_from_args, **kwargs
157167
)
168+
await session.close()
169+
return response
158170

159171
monkeypatch.setattr(
160172
f"aiohttp.ClientSession.{http_method.lower()}", _mock_specific_request

web3/utils/async_exception_handling.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ async def async_handle_offchain_lookup(
5454
try:
5555
if "{data}" in url and "{sender}" in url:
5656
response = await session.get(
57-
formatted_url, timeout=DEFAULT_HTTP_TIMEOUT
57+
formatted_url, timeout=ClientTimeout(DEFAULT_HTTP_TIMEOUT)
5858
)
5959
elif "{sender}" in url:
6060
response = await session.post(
@@ -63,20 +63,23 @@ async def async_handle_offchain_lookup(
6363
timeout=ClientTimeout(DEFAULT_HTTP_TIMEOUT),
6464
)
6565
else:
66+
await session.close()
6667
raise Web3ValidationError("url not formatted properly.")
6768
except Exception:
6869
continue # try next url if timeout or issues making the request
6970

7071
if (
7172
400 <= response.status <= 499
7273
): # if request returns 400 error, raise exception
74+
await session.close()
7375
response.raise_for_status()
7476
if not 200 <= response.status <= 299: # if not 400 error, try next url
7577
continue
7678

7779
result = await response.json()
7880

7981
if "data" not in result.keys():
82+
await session.close()
8083
raise Web3ValidationError(
8184
"Improperly formatted response for offchain lookup HTTP request"
8285
" - missing 'data' field."
@@ -97,5 +100,8 @@ async def async_handle_offchain_lookup(
97100
]
98101
)
99102

103+
await session.close()
100104
return encoded_data_with_function_selector
105+
106+
await session.close()
101107
raise MultipleFailedRequests("Offchain lookup failed for supplied urls.")

web3/utils/exception_handling.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
)
1212
import requests
1313

14+
from web3._utils.http import (
15+
DEFAULT_HTTP_TIMEOUT,
16+
)
1417
from web3._utils.type_conversion import (
1518
to_bytes_if_hex,
1619
to_hex_if_bytes,
@@ -47,14 +50,15 @@ def handle_offchain_lookup(
4750

4851
try:
4952
if "{data}" in url and "{sender}" in url:
50-
response = session.get(formatted_url)
53+
response = session.get(formatted_url, timeout=DEFAULT_HTTP_TIMEOUT)
5154
elif "{sender}" in url:
5255
response = session.post(
5356
formatted_url,
5457
data={
5558
"data": formatted_data,
5659
"sender": formatted_sender,
5760
},
61+
timeout=DEFAULT_HTTP_TIMEOUT,
5862
)
5963
else:
6064
raise Web3ValidationError("url not formatted properly.")

0 commit comments

Comments
 (0)