Skip to content

Commit e898ee9

Browse files
committed
Fix Beacon API after internal cache rehaul:
- We moved session caching as internal parts of HTTP providers. Since that change, the ``Beacon`` API was receiving a bad bath for session caching. These updated fix that and raise the timeout for the async tests since some seem to be timing out.
1 parent 5f5e056 commit e898ee9

File tree

5 files changed

+21
-30
lines changed

5 files changed

+21
-30
lines changed

newsfragments/3421.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
A bugfix, pre-release, to update the ``Beacon`` APIs (sync and async) to properly use the new ``HTTPSessionManager``.

tests/beacon/test_async_beacon.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,14 @@
33
randint,
44
)
55

6+
from aiohttp.client_exceptions import (
7+
InvalidURL,
8+
)
69
import pytest_asyncio
710

8-
from web3._utils.request import (
9-
_async_session_cache,
10-
)
1111
from web3.beacon import (
1212
AsyncBeacon,
1313
)
14-
from web3.exceptions import (
15-
Web3ValueError,
16-
)
1714

1815
# tested against lighthouse which uses port 5052 by default
1916
BASE_URL = "http://localhost:5052"
@@ -27,20 +24,22 @@ def _assert_valid_response(response):
2724

2825
@pytest.fixture
2926
def async_beacon():
30-
return AsyncBeacon(base_url=BASE_URL)
27+
return AsyncBeacon(base_url=BASE_URL, request_timeout=30.0)
3128

3229

3330
@pytest_asyncio.fixture(autouse=True)
34-
async def _cleanup():
31+
async def _cleanup(async_beacon):
3532
yield
36-
[await session.close() for _, session in _async_session_cache.items()]
37-
_async_session_cache.clear()
33+
[
34+
await session.close()
35+
for _, session in async_beacon._request_session_manager.session_cache.items()
36+
]
3837

3938

4039
# sanity check to make sure the positive test cases are valid
4140
@pytest.mark.asyncio
4241
async def test_async_cl_beacon_raises_exception_on_invalid_url(async_beacon):
43-
with pytest.raises(Web3ValueError):
42+
with pytest.raises(InvalidURL):
4443
await async_beacon._async_make_get_request(
4544
BASE_URL + "/eth/v1/beacon/nonexistent"
4645
)

tests/beacon/test_beacon.py

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,13 @@
66
from requests import (
77
Timeout,
88
)
9-
10-
from web3._utils.request import (
11-
_session_cache,
9+
from requests.exceptions import (
10+
InvalidURL,
1211
)
12+
1313
from web3.beacon import (
1414
Beacon,
1515
)
16-
from web3.exceptions import (
17-
Web3ValueError,
18-
)
1916

2017
# tested against lighthouse which uses port 5052 by default
2118
BASE_URL = "http://localhost:5052"
@@ -32,15 +29,9 @@ def beacon():
3229
return Beacon(base_url=BASE_URL)
3330

3431

35-
@pytest.fixture(autouse=True)
36-
def _cleanup():
37-
yield
38-
_session_cache.clear()
39-
40-
4132
# sanity check to make sure the positive test cases are valid
4233
def test_cl_beacon_raises_exception_on_invalid_url(beacon):
43-
with pytest.raises(Web3ValueError):
34+
with pytest.raises(InvalidURL):
4435
beacon._make_get_request(BASE_URL + "/eth/v1/beacon/nonexistent")
4536

4637

web3/beacon/async_beacon.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
HexStr,
99
)
1010

11+
from web3._utils.http_session_manager import (
12+
HTTPSessionManager,
13+
)
1114
from web3.beacon.api_endpoints import (
1215
GET_ATTESTATIONS,
1316
GET_ATTESTER_SLASHINGS,
@@ -47,9 +50,6 @@
4750
GET_VERSION,
4851
GET_VOLUNTARY_EXITS,
4952
)
50-
from web3.session_manager import (
51-
HTTPSessionManager,
52-
)
5353

5454

5555
class AsyncBeacon:

web3/beacon/beacon.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
HexStr,
99
)
1010

11+
from web3._utils.http_session_manager import (
12+
HTTPSessionManager,
13+
)
1114
from web3.beacon.api_endpoints import (
1215
GET_ATTESTATIONS,
1316
GET_ATTESTER_SLASHINGS,
@@ -47,9 +50,6 @@
4750
GET_VERSION,
4851
GET_VOLUNTARY_EXITS,
4952
)
50-
from web3.session_manager import (
51-
HTTPSessionManager,
52-
)
5353

5454

5555
class Beacon:

0 commit comments

Comments
 (0)