Skip to content

Commit

Permalink
Fix Beacon API after internal cache rehaul:
Browse files Browse the repository at this point in the history
- 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.
  • Loading branch information
fselmo committed Jun 26, 2024
1 parent 5f5e056 commit e898ee9
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 30 deletions.
1 change: 1 addition & 0 deletions newsfragments/3421.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
A bugfix, pre-release, to update the ``Beacon`` APIs (sync and async) to properly use the new ``HTTPSessionManager``.
21 changes: 10 additions & 11 deletions tests/beacon/test_async_beacon.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,14 @@
randint,
)

from aiohttp.client_exceptions import (
InvalidURL,
)
import pytest_asyncio

from web3._utils.request import (
_async_session_cache,
)
from web3.beacon import (
AsyncBeacon,
)
from web3.exceptions import (
Web3ValueError,
)

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

@pytest.fixture
def async_beacon():
return AsyncBeacon(base_url=BASE_URL)
return AsyncBeacon(base_url=BASE_URL, request_timeout=30.0)


@pytest_asyncio.fixture(autouse=True)
async def _cleanup():
async def _cleanup(async_beacon):
yield
[await session.close() for _, session in _async_session_cache.items()]
_async_session_cache.clear()
[
await session.close()
for _, session in async_beacon._request_session_manager.session_cache.items()
]


# sanity check to make sure the positive test cases are valid
@pytest.mark.asyncio
async def test_async_cl_beacon_raises_exception_on_invalid_url(async_beacon):
with pytest.raises(Web3ValueError):
with pytest.raises(InvalidURL):
await async_beacon._async_make_get_request(
BASE_URL + "/eth/v1/beacon/nonexistent"
)
Expand Down
17 changes: 4 additions & 13 deletions tests/beacon/test_beacon.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,13 @@
from requests import (
Timeout,
)

from web3._utils.request import (
_session_cache,
from requests.exceptions import (
InvalidURL,
)

from web3.beacon import (
Beacon,
)
from web3.exceptions import (
Web3ValueError,
)

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


@pytest.fixture(autouse=True)
def _cleanup():
yield
_session_cache.clear()


# sanity check to make sure the positive test cases are valid
def test_cl_beacon_raises_exception_on_invalid_url(beacon):
with pytest.raises(Web3ValueError):
with pytest.raises(InvalidURL):
beacon._make_get_request(BASE_URL + "/eth/v1/beacon/nonexistent")


Expand Down
6 changes: 3 additions & 3 deletions web3/beacon/async_beacon.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
HexStr,
)

from web3._utils.http_session_manager import (
HTTPSessionManager,
)
from web3.beacon.api_endpoints import (
GET_ATTESTATIONS,
GET_ATTESTER_SLASHINGS,
Expand Down Expand Up @@ -47,9 +50,6 @@
GET_VERSION,
GET_VOLUNTARY_EXITS,
)
from web3.session_manager import (
HTTPSessionManager,
)


class AsyncBeacon:
Expand Down
6 changes: 3 additions & 3 deletions web3/beacon/beacon.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
HexStr,
)

from web3._utils.http_session_manager import (
HTTPSessionManager,
)
from web3.beacon.api_endpoints import (
GET_ATTESTATIONS,
GET_ATTESTER_SLASHINGS,
Expand Down Expand Up @@ -47,9 +50,6 @@
GET_VERSION,
GET_VOLUNTARY_EXITS,
)
from web3.session_manager import (
HTTPSessionManager,
)


class Beacon:
Expand Down

0 comments on commit e898ee9

Please sign in to comment.