12
12
13
13
from bittensor .utils .btlogging import logging
14
14
from tests .e2e_tests .utils .e2e_test_utils import (
15
- clone_or_update_templates ,
16
- install_templates ,
15
+ Templates ,
17
16
setup_wallet ,
18
- template_path ,
19
- uninstall_templates ,
20
17
)
21
18
22
19
@@ -38,15 +35,6 @@ def local_chain(request):
38
35
# Compile commands to send to process
39
36
cmds = shlex .split (f"{ script_path } { args } " )
40
37
41
- # Start new node process
42
- process = subprocess .Popen (
43
- cmds ,
44
- stdout = subprocess .PIPE ,
45
- stderr = subprocess .STDOUT ,
46
- text = True ,
47
- preexec_fn = os .setsid ,
48
- )
49
-
50
38
# Pattern match indicates node is compiled and ready
51
39
pattern = re .compile (r"Imported #1" )
52
40
timestamp = int (time .time ())
@@ -61,7 +49,7 @@ def wait_for_node_start(process, pattern):
61
49
# 10 min as timeout
62
50
if int (time .time ()) - timestamp > 10 * 60 :
63
51
print ("Subtensor not started in time" )
64
- return
52
+ raise TimeoutError
65
53
if pattern .search (line ):
66
54
print ("Node started!" )
67
55
break
@@ -77,38 +65,35 @@ def read_output():
77
65
reader_thread = threading .Thread (target = read_output , daemon = True )
78
66
reader_thread .start ()
79
67
80
- wait_for_node_start (process , pattern )
81
-
82
- # Run the test, passing in substrate interface
83
- yield SubstrateInterface (url = "ws://127.0.0.1:9944" )
84
-
85
- # Terminate the process group (includes all child processes)
86
- os .killpg (os .getpgid (process .pid ), signal .SIGTERM )
87
-
88
- # Give some time for the process to terminate
89
- time .sleep (1 )
90
-
91
- # If the process is not terminated, send SIGKILL
92
- if process .poll () is None :
93
- os .killpg (os .getpgid (process .pid ), signal .SIGKILL )
94
-
95
- # Ensure the process has terminated
96
- process .wait ()
97
-
98
-
99
- @pytest .fixture
68
+ with subprocess .Popen (
69
+ cmds ,
70
+ start_new_session = True ,
71
+ stderr = subprocess .STDOUT ,
72
+ stdout = subprocess .PIPE ,
73
+ text = True ,
74
+ ) as process :
75
+ try :
76
+ wait_for_node_start (process , pattern )
77
+ except TimeoutError :
78
+ raise
79
+ else :
80
+ yield SubstrateInterface (url = "ws://127.0.0.1:9944" )
81
+ finally :
82
+ # Terminate the process group (includes all child processes)
83
+ os .killpg (os .getpgid (process .pid ), signal .SIGTERM )
84
+
85
+ try :
86
+ process .wait (1 )
87
+ except subprocess .TimeoutExpired :
88
+ # If the process is not terminated, send SIGKILL
89
+ os .killpg (os .getpgid (process .pid ), signal .SIGKILL )
90
+ process .wait ()
91
+
92
+
93
+ @pytest .fixture (scope = "session" )
100
94
def templates ():
101
- logging .info ("downloading and installing neuron templates from github" )
102
-
103
- templates_dir = clone_or_update_templates ()
104
-
105
- install_templates (templates_dir )
106
-
107
- yield templates_dir
108
-
109
- logging .info ("uninstalling neuron templates" )
110
-
111
- uninstall_templates (template_path )
95
+ with Templates () as templates :
96
+ yield templates
112
97
113
98
114
99
@pytest .fixture
0 commit comments