|
1 | 1 | import asyncio |
| 2 | +import logging |
| 3 | +import os.path |
2 | 4 | import time |
| 5 | +import threading |
3 | 6 |
|
4 | 7 | import pytest |
5 | 8 | from scalecodec import ss58_encode |
6 | 9 |
|
7 | | -from async_substrate_interface.async_substrate import AsyncSubstrateInterface |
| 10 | +from async_substrate_interface.async_substrate import AsyncSubstrateInterface, logger |
8 | 11 | from async_substrate_interface.types import ScaleObj |
9 | 12 | from tests.helpers.settings import ARCHIVE_ENTRYPOINT, LATENT_LITE_ENTRYPOINT |
| 13 | +from tests.helpers.proxy_server import ProxyServer |
10 | 14 |
|
11 | 15 |
|
12 | 16 | @pytest.mark.asyncio |
@@ -174,3 +178,120 @@ async def test_query_map_with_odd_number_of_params(): |
174 | 178 | first_record = qm.records[0] |
175 | 179 | assert len(first_record) == 2 |
176 | 180 | assert len(first_record[0]) == 4 |
| 181 | + |
| 182 | + |
| 183 | +@pytest.mark.asyncio |
| 184 | +async def test_improved_reconnection_(): |
| 185 | + ws_logger_path = "/tmp/websockets-proxy-test" |
| 186 | + ws_logger = logging.getLogger("websockets.proxy") |
| 187 | + if os.path.exists(ws_logger_path): |
| 188 | + os.remove(ws_logger_path) |
| 189 | + ws_logger.setLevel(logging.INFO) |
| 190 | + ws_logger.addHandler(logging.FileHandler(ws_logger_path)) |
| 191 | + |
| 192 | + asi_logger_path = "/tmp/async-substrate-interface-test" |
| 193 | + if os.path.exists(asi_logger_path): |
| 194 | + os.remove(asi_logger_path) |
| 195 | + logger.setLevel(logging.DEBUG) |
| 196 | + logger.addHandler(logging.FileHandler(asi_logger_path)) |
| 197 | + |
| 198 | + proxy = ProxyServer("wss://archive.sub.latent.to", 10, 20) |
| 199 | + proxy.connect() |
| 200 | + server_thread = threading.Thread(target=proxy.serve) |
| 201 | + server_thread.daemon = True |
| 202 | + server_thread.start() |
| 203 | + |
| 204 | + await asyncio.sleep(3) # give the server start up time |
| 205 | + |
| 206 | + try: |
| 207 | + async with AsyncSubstrateInterface( |
| 208 | + "ws://localhost:8080", |
| 209 | + ss58_format=42, |
| 210 | + chain_name="Bittensor", |
| 211 | + retry_timeout=10.0, |
| 212 | + ws_shutdown_timer=None, |
| 213 | + ) as substrate: |
| 214 | + blocks_to_check = [ |
| 215 | + 5215000, |
| 216 | + 5215001, |
| 217 | + 5215002, |
| 218 | + 5215003, |
| 219 | + 5215004, |
| 220 | + 5215005, |
| 221 | + 5215006, |
| 222 | + ] |
| 223 | + tasks = [] |
| 224 | + for block in blocks_to_check: |
| 225 | + block_hash = await substrate.get_block_hash(block_id=block) |
| 226 | + tasks.append( |
| 227 | + substrate.query_map( |
| 228 | + "SubtensorModule", "TotalHotkeyShares", block_hash=block_hash |
| 229 | + ) |
| 230 | + ) |
| 231 | + records = await asyncio.gather(*tasks) |
| 232 | + assert len(records) == len(blocks_to_check) |
| 233 | + await substrate.close() |
| 234 | + with open(ws_logger_path, "r") as f: |
| 235 | + assert "Pausing" in f.read() |
| 236 | + with open(asi_logger_path, "r") as f: |
| 237 | + assert "Timeout/ConnectionClosed occurred." in f.read() |
| 238 | + finally: |
| 239 | + proxy.stop() |
| 240 | + server_thread.join(timeout=5) |
| 241 | + |
| 242 | + |
| 243 | +@pytest.mark.asyncio |
| 244 | +async def test_improved_reconnection(): |
| 245 | + ws_logger_path = "/tmp/websockets-proxy-test" |
| 246 | + ws_logger = logging.getLogger("websockets.proxy") |
| 247 | + if os.path.exists(ws_logger_path): |
| 248 | + os.remove(ws_logger_path) |
| 249 | + ws_logger.setLevel(logging.INFO) |
| 250 | + ws_logger.addHandler(logging.FileHandler(ws_logger_path)) |
| 251 | + |
| 252 | + asi_logger_path = "/tmp/async-substrate-interface-test" |
| 253 | + if os.path.exists(asi_logger_path): |
| 254 | + os.remove(asi_logger_path) |
| 255 | + logger.setLevel(logging.DEBUG) |
| 256 | + logger.addHandler(logging.FileHandler(asi_logger_path)) |
| 257 | + |
| 258 | + proxy = ProxyServer("wss://archive.sub.latent.to", 10, 20) |
| 259 | + |
| 260 | + server_thread = threading.Thread(target=proxy.connect_and_serve) |
| 261 | + server_thread.start() |
| 262 | + await asyncio.sleep(3) # give the server start up time |
| 263 | + async with AsyncSubstrateInterface( |
| 264 | + "ws://localhost:8080", |
| 265 | + ss58_format=42, |
| 266 | + chain_name="Bittensor", |
| 267 | + retry_timeout=10.0, |
| 268 | + ws_shutdown_timer=None, |
| 269 | + ) as substrate: |
| 270 | + blocks_to_check = [ |
| 271 | + 5215000, |
| 272 | + 5215001, |
| 273 | + 5215002, |
| 274 | + 5215003, |
| 275 | + 5215004, |
| 276 | + 5215005, |
| 277 | + 5215006, |
| 278 | + ] |
| 279 | + tasks = [] |
| 280 | + for block in blocks_to_check: |
| 281 | + block_hash = await substrate.get_block_hash(block_id=block) |
| 282 | + tasks.append( |
| 283 | + substrate.query_map( |
| 284 | + "SubtensorModule", "TotalHotkeyShares", block_hash=block_hash |
| 285 | + ) |
| 286 | + ) |
| 287 | + records = await asyncio.gather(*tasks) |
| 288 | + assert len(records) == len(blocks_to_check) |
| 289 | + await substrate.close() |
| 290 | + with open(ws_logger_path, "r") as f: |
| 291 | + assert "Pausing" in f.read() |
| 292 | + with open(asi_logger_path, "r") as f: |
| 293 | + assert "Timeout/ConnectionClosed occurred." in f.read() |
| 294 | + shutdown_thread = threading.Thread(target=proxy.close) |
| 295 | + shutdown_thread.start() |
| 296 | + shutdown_thread.join(timeout=5) |
| 297 | + server_thread.join(timeout=5) |
0 commit comments