Skip to content

Commit

Permalink
Upgrade websockets dependency to v10+ (ethereum#2324)
Browse files Browse the repository at this point in the history
* Require websockets v10+

- Remove event loop parameter

* Add newsfragment for websockets upgrade
  • Loading branch information
kclowes authored and Martin Votruba committed Nov 11, 2022
1 parent ffc004b commit 4562fd1
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 11 deletions.
1 change: 1 addition & 0 deletions newsfragments/2324.breaking-change.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Update ``websockets`` dependency to v10+
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@
"requests>=2.16.0,<3.0.0",
# remove typing_extensions after python_requires>=3.8, see web3._utils.compat
"typing-extensions>=3.7.4.1,<5;python_version<'3.8'",
"websockets>=9.1,<10",
"websockets>=10.0.0,<11",
],
python_requires=">=3.6,<4",
extras_require=extras_require,
Expand Down
6 changes: 4 additions & 2 deletions tests/core/providers/test_websocket_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ async def empty_server(websocket, path):
data = await websocket.recv()
await asyncio.sleep(0.02)
await websocket.send(data)
server = websockets.serve(empty_server, '127.0.0.1', open_port, loop=event_loop)

asyncio.set_event_loop(event_loop)
server = websockets.serve(empty_server, '127.0.0.1', open_port)
event_loop.run_until_complete(server)
event_loop.run_forever()

Expand All @@ -54,7 +56,7 @@ def w3(open_port, start_websocket_server):
# need new event loop as the one used by server is already running
event_loop = asyncio.new_event_loop()
endpoint_uri = 'ws://127.0.0.1:{}'.format(open_port)
event_loop.run_until_complete(wait_for_ws(endpoint_uri, event_loop))
event_loop.run_until_complete(wait_for_ws(endpoint_uri))
provider = WebsocketProvider(endpoint_uri, websocket_timeout=0.01)
return Web3(provider)

Expand Down
6 changes: 4 additions & 2 deletions tests/integration/go_ethereum/test_goethereum_ws.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import asyncio
import pytest

from tests.integration.common import (
Expand Down Expand Up @@ -63,8 +64,9 @@ def geth_command_arguments(geth_binary,


@pytest.fixture(scope="module")
def web3(geth_process, endpoint_uri, event_loop):
event_loop.run_until_complete(wait_for_ws(endpoint_uri, event_loop))
def web3(geth_process, endpoint_uri):
event_loop = asyncio.new_event_loop()
event_loop.run_until_complete(wait_for_ws(endpoint_uri))
_web3 = Web3(Web3.WebsocketProvider(endpoint_uri, websocket_timeout=30))
return _web3

Expand Down
4 changes: 2 additions & 2 deletions tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ def get_open_port():
return str(port)


async def wait_for_ws(endpoint_uri, event_loop, timeout=60):
async def wait_for_ws(endpoint_uri, timeout=10):
start = time.time()
while time.time() < start + timeout:
try:
async with websockets.connect(uri=endpoint_uri, loop=event_loop):
async with websockets.connect(uri=endpoint_uri):
pass
except (ConnectionRefusedError, OSError):
await asyncio.sleep(0.01)
Expand Down
7 changes: 3 additions & 4 deletions web3/providers/websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,16 @@ def get_default_endpoint() -> URI:
class PersistentWebSocket:

def __init__(
self, endpoint_uri: URI, loop: asyncio.AbstractEventLoop, websocket_kwargs: Any
self, endpoint_uri: URI, websocket_kwargs: Any
) -> None:
self.ws: WebSocketClientProtocol = None
self.endpoint_uri = endpoint_uri
self.loop = loop
self.websocket_kwargs = websocket_kwargs

async def __aenter__(self) -> WebSocketClientProtocol:
if self.ws is None:
self.ws = await connect(
uri=self.endpoint_uri, loop=self.loop, **self.websocket_kwargs
uri=self.endpoint_uri, **self.websocket_kwargs
)
return self.ws

Expand Down Expand Up @@ -113,7 +112,7 @@ def __init__(
'found: {1}'.format(RESTRICTED_WEBSOCKET_KWARGS, found_restricted_keys)
)
self.conn = PersistentWebSocket(
self.endpoint_uri, WebsocketProvider._loop, websocket_kwargs
self.endpoint_uri, websocket_kwargs
)
super().__init__()

Expand Down

0 comments on commit 4562fd1

Please sign in to comment.