Skip to content

Commit 767e122

Browse files
committed
[WIP] tests
1 parent 3f50aaf commit 767e122

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

tests/conftest.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import subprocess
2+
import time
3+
4+
CONTAINER_NAME_PREFIX = "test_local_chain_"
5+
LOCALNET_IMAGE_NAME = "ghcr.io/opentensor/subtensor-localnet:devnet-ready"
6+
7+
8+
def start_docker_container(exposed_port, name_salt: str):
9+
container_name = f"{CONTAINER_NAME_PREFIX}{name_salt}"
10+
11+
# Command to start container
12+
cmds = [
13+
"docker",
14+
"run",
15+
"--rm",
16+
"--name",
17+
container_name,
18+
"-p",
19+
f"{exposed_port}:9945",
20+
LOCALNET_IMAGE_NAME,
21+
]
22+
23+
proc = subprocess.Popen(cmds, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
24+
return proc, container_name

tests/test_substrate_addons.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import threading
2+
import subprocess
3+
4+
import pytest
5+
import time
6+
7+
from async_substrate_interface.substrate_addons import RetrySyncSubstrate
8+
from tests.conftest import start_docker_container
9+
10+
11+
@pytest.fixture(scope="function")
12+
def start_containers():
13+
# Store our subprocesses globally
14+
processes = (start_docker_container(9945, 9945), start_docker_container(9946, 9946))
15+
yield processes
16+
17+
# To stop the instances, you can iterate over the processes and kill them:
18+
for process in processes:
19+
subprocess.run(["docker", "kill", process[1]])
20+
process[0].kill()
21+
22+
23+
def test_retry_sync_substrate(start_containers):
24+
container1, container2 = start_containers
25+
time.sleep(10)
26+
with RetrySyncSubstrate(
27+
"ws://127.0.0.1:9945", fallback_chains=["ws://127.0.0.1:9946"]
28+
) as substrate:
29+
for i in range(10):
30+
assert substrate.get_chain_head().startswith("0x")
31+
if i == 8:
32+
subprocess.run(["docker", "kill", container1[1]])
33+
time.sleep(10)
34+
if i > 8:
35+
assert substrate.chain_endpoint == "ws://127.0.0.1:9946"
36+
time.sleep(2)

0 commit comments

Comments
 (0)