Skip to content

Commit 1e98614

Browse files
committed
Added test
1 parent f18b52f commit 1e98614

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

async_substrate_interface/async_substrate.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -618,7 +618,9 @@ async def _handler(self, ws: ClientConnection) -> None:
618618
for task in pending:
619619
task.cancel()
620620
for task in done:
621-
if isinstance(task.result(), (asyncio.TimeoutError, ConnectionClosed, TimeoutError)):
621+
if isinstance(
622+
task.result(), (asyncio.TimeoutError, ConnectionClosed, TimeoutError)
623+
):
622624
should_reconnect = True
623625
if should_reconnect is True:
624626
for original_id, payload in list(self._inflight.items()):
@@ -692,7 +694,9 @@ async def _start_receiving(self, ws: ClientConnection) -> Exception:
692694
except Exception as e:
693695
if isinstance(e, ssl.SSLError):
694696
e = ConnectionClosed
695-
if not isinstance(e, (asyncio.TimeoutError, TimeoutError, ConnectionClosed)):
697+
if not isinstance(
698+
e, (asyncio.TimeoutError, TimeoutError, ConnectionClosed)
699+
):
696700
logger.exception("Websocket receiving exception", exc_info=e)
697701
for fut in self._received.values():
698702
if not fut.done():
@@ -717,7 +721,9 @@ async def _start_sending(self, ws) -> Exception:
717721
except Exception as e:
718722
if isinstance(e, ssl.SSLError):
719723
e = ConnectionClosed
720-
if not isinstance(e, (asyncio.TimeoutError, TimeoutError, ConnectionClosed)):
724+
if not isinstance(
725+
e, (asyncio.TimeoutError, TimeoutError, ConnectionClosed)
726+
):
721727
logger.exception("Websocket sending exception", exc_info=e)
722728
if to_send is not None:
723729
self._received[to_send["id"]].set_exception(e)

tests/integration_tests/test_async_substrate_interface.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import asyncio
12
import time
23

34
import pytest
@@ -149,3 +150,14 @@ async def test_query_multiple():
149150
storage_function="OwnedHotkeys",
150151
block_hash=block_hash,
151152
)
153+
154+
155+
@pytest.mark.asyncio
156+
async def test_reconnection():
157+
async with AsyncSubstrateInterface(
158+
ARCHIVE_ENTRYPOINT, ss58_format=42, retry_timeout=8.0
159+
) as substrate:
160+
await asyncio.sleep(9) # sleep for longer than the retry timeout
161+
bh = await substrate.get_chain_finalised_head()
162+
assert isinstance(bh, str)
163+
assert isinstance(await substrate.get_block_number(bh), int)

0 commit comments

Comments
 (0)