Skip to content

Commit d3f6473

Browse files
committed
improved test a bit
1 parent 767e122 commit d3f6473

File tree

2 files changed

+22
-14
lines changed

2 files changed

+22
-14
lines changed

tests/conftest.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import subprocess
2-
import time
2+
from collections import namedtuple
33

44
CONTAINER_NAME_PREFIX = "test_local_chain_"
55
LOCALNET_IMAGE_NAME = "ghcr.io/opentensor/subtensor-localnet:devnet-ready"
66

7+
Container = namedtuple("Container", ["process", "name", "uri"])
8+
79

810
def start_docker_container(exposed_port, name_salt: str):
911
container_name = f"{CONTAINER_NAME_PREFIX}{name_salt}"
@@ -21,4 +23,4 @@ def start_docker_container(exposed_port, name_salt: str):
2123
]
2224

2325
proc = subprocess.Popen(cmds, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
24-
return proc, container_name
26+
return Container(proc, container_name, f"ws://127.0.0.1:{exposed_port}")

tests/test_substrate_addons.py

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,28 +9,34 @@
99

1010

1111
@pytest.fixture(scope="function")
12-
def start_containers():
13-
# Store our subprocesses globally
12+
def docker_containers():
1413
processes = (start_docker_container(9945, 9945), start_docker_container(9946, 9946))
15-
yield processes
14+
try:
15+
yield processes
1616

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()
17+
finally:
18+
for process in processes:
19+
subprocess.run(["docker", "kill", process[1]])
20+
process[0].kill()
2121

2222

23-
def test_retry_sync_substrate(start_containers):
24-
container1, container2 = start_containers
23+
def test_retry_sync_substrate(docker_containers):
2524
time.sleep(10)
2625
with RetrySyncSubstrate(
27-
"ws://127.0.0.1:9945", fallback_chains=["ws://127.0.0.1:9946"]
26+
docker_containers[0].uri, fallback_chains=[docker_containers[1].uri]
2827
) as substrate:
2928
for i in range(10):
3029
assert substrate.get_chain_head().startswith("0x")
3130
if i == 8:
32-
subprocess.run(["docker", "kill", container1[1]])
31+
subprocess.run(["docker", "stop", docker_containers[0].name])
3332
time.sleep(10)
3433
if i > 8:
35-
assert substrate.chain_endpoint == "ws://127.0.0.1:9946"
34+
assert substrate.chain_endpoint == docker_containers[1].uri
3635
time.sleep(2)
36+
37+
38+
def test_retry_sync_substrate_offline():
39+
with pytest.raises(ConnectionError):
40+
RetrySyncSubstrate(
41+
"ws://127.0.0.1:9945", fallback_chains=["ws://127.0.0.1:9946"]
42+
)

0 commit comments

Comments
 (0)