1- use duct:: { cmd, Handle } ;
1+ use duct:: cmd;
22use lazy_static:: lazy_static;
33use rand:: distributions:: { Alphanumeric , DistString } ;
4+ use std:: thread:: JoinHandle ;
45use std:: { collections:: HashSet , fs:: create_dir_all, sync:: Mutex } ;
56
7+ use crate :: invoke_cli;
68use crate :: modules:: { module_path, CompiledModule } ;
79use std:: path:: Path ;
810
9- struct StandaloneProcess {
10- handle : Handle ,
11- num_using : usize ,
12- }
13-
14- impl StandaloneProcess {
15- fn start ( ) -> Self {
16- let handle = cmd ! ( "spacetime" , "start" )
17- . stderr_to_stdout ( )
18- . stdout_capture ( )
19- . unchecked ( )
20- . start ( )
21- . expect ( "Failed to run `spacetime start`" ) ;
22-
23- StandaloneProcess { handle, num_using : 1 }
11+ pub fn ensure_standalone_process ( ) {
12+ lazy_static ! {
13+ static ref JOIN_HANDLE : Mutex <Option <JoinHandle <( ) >>> =
14+ Mutex :: new( Some ( std:: thread:: spawn( || invoke_cli( & [ "start" ] ) ) ) ) ;
2415 }
2516
26- fn stop ( & mut self ) -> anyhow:: Result < ( ) > {
27- assert ! ( self . num_using == 0 ) ;
28-
29- self . handle . kill ( ) ?;
17+ let mut join_handle = JOIN_HANDLE . lock ( ) . unwrap ( ) ;
3018
31- Ok ( ( ) )
32- }
33-
34- fn running_or_err ( & self ) -> anyhow:: Result < ( ) > {
35- if let Some ( output) = self
36- . handle
37- . try_wait ( )
38- . expect ( "Error from spacetime standalone subprocess" )
39- {
40- let code = output. status ;
41- let output = String :: from_utf8_lossy ( & output. stdout ) ;
42- Err ( anyhow:: anyhow!(
43- "spacetime start exited unexpectedly. Exit status: {}. Output:\n {}" ,
44- code,
45- output,
46- ) )
47- } else {
48- Ok ( ( ) )
49- }
50- }
51-
52- fn add_user ( & mut self ) -> anyhow:: Result < ( ) > {
53- self . running_or_err ( ) ?;
54- self . num_using += 1 ;
55- Ok ( ( ) )
56- }
57-
58- /// Returns true if the process was stopped because no one is using it.
59- fn sub_user ( & mut self ) -> anyhow:: Result < bool > {
60- self . num_using -= 1 ;
61- if self . num_using == 0 {
62- self . stop ( ) ?;
63- Ok ( true )
64- } else {
65- Ok ( false )
66- }
67- }
68- }
69-
70- static STANDALONE_PROCESS : Mutex < Option < StandaloneProcess > > = Mutex :: new ( None ) ;
71-
72- /// An RAII handle on the `STANDALONE_PROCESS`.
73- ///
74- /// On construction, ensures that the `STANDALONE_PROCESS` is running.
75- ///
76- /// On drop, checks to see if it was the last `StandaloneHandle`, and if so,
77- /// terminates the `STANDALONE_PROCESS`.
78- pub struct StandaloneHandle {
79- _hidden : ( ) ,
80- }
81-
82- impl Default for StandaloneHandle {
83- fn default ( ) -> Self {
84- let mut process = STANDALONE_PROCESS . lock ( ) . expect ( "STANDALONE_PROCESS Mutex is poisoned" ) ;
85- if let Some ( proc) = & mut * process {
86- proc. add_user ( )
87- . expect ( "Failed to add user for running spacetime standalone process" ) ;
88- } else {
89- * process = Some ( StandaloneProcess :: start ( ) ) ;
90- }
91- StandaloneHandle { _hidden : ( ) }
92- }
93- }
94-
95- impl Drop for StandaloneHandle {
96- fn drop ( & mut self ) {
97- let mut process = STANDALONE_PROCESS . lock ( ) . expect ( "STANDALONE_PROCESS Mutex is poisoned" ) ;
98- if let Some ( proc) = & mut * process {
99- if proc
100- . sub_user ( )
101- . expect ( "Failed to remove user for running spacetime standalone process" )
102- {
103- * process = None ;
104- }
105- }
19+ if join_handle
20+ . as_ref ( )
21+ . expect ( "Standalone process already finished" )
22+ . is_finished ( )
23+ {
24+ join_handle. take ( ) . unwrap ( ) . join ( ) . expect ( "Standalone process failed" ) ;
10625 }
10726}
10827
@@ -180,7 +99,7 @@ impl Test {
18099 TestBuilder :: default ( )
181100 }
182101 pub fn run ( & self ) {
183- let _handle = StandaloneHandle :: default ( ) ;
102+ ensure_standalone_process ( ) ;
184103
185104 let compiled = CompiledModule :: compile ( & self . module_name ) ;
186105
@@ -189,12 +108,11 @@ impl Test {
189108 compiled. path ( ) ,
190109 & self . client_project ,
191110 & self . generate_subdir ,
192- & self . name ,
193111 ) ;
194112
195113 compile_client ( & self . compile_command , & self . client_project , & self . name ) ;
196114
197- let db_name = publish_module ( & self . module_name , & self . name ) ;
115+ let db_name = publish_module ( & self . module_name ) ;
198116
199117 run_client ( & self . run_command , & self . client_project , & db_name, & self . name ) ;
200118 }
@@ -216,22 +134,20 @@ fn random_module_name() -> String {
216134 Alphanumeric . sample_string ( & mut rand:: thread_rng ( ) , 16 )
217135}
218136
219- fn publish_module ( module : & str , test_name : & str ) -> String {
137+ fn publish_module ( module : & str ) -> String {
220138 let name = random_module_name ( ) ;
221- let output = cmd ! ( "spacetime" , "publish" , "--skip_clippy" , name. clone( ) , )
222- . stderr_to_stdout ( )
223- . stdout_capture ( )
224- . dir ( module_path ( module) )
225- . unchecked ( )
226- . run ( )
227- . expect ( "Error running spacetime publish" ) ;
228-
229- status_ok_or_panic ( output, "spacetime publish" , test_name) ;
139+ invoke_cli ( & [
140+ "publish" ,
141+ "--project-path" ,
142+ module_path ( module) . to_str ( ) . unwrap ( ) ,
143+ "--skip_clippy" ,
144+ & name,
145+ ] ) ;
230146
231147 name
232148}
233149
234- fn generate_bindings ( language : & str , path : & Path , client_project : & str , generate_subdir : & str , test_name : & str ) {
150+ fn generate_bindings ( language : & str , path : & Path , client_project : & str , generate_subdir : & str ) {
235151 let generate_dir = format ! ( "{}/{}" , client_project, generate_subdir) ;
236152
237153 let mut bindings_lock = BINDINGS_GENERATED . lock ( ) . expect ( "BINDINGS_GENERATED Mutex is poisoned" ) ;
@@ -245,24 +161,16 @@ fn generate_bindings(language: &str, path: &Path, client_project: &str, generate
245161 }
246162
247163 create_dir_all ( & generate_dir) . expect ( "Error creating generate subdir" ) ;
248- let output = cmd ! (
249- "spacetime" ,
164+ invoke_cli ( & [
250165 "generate" ,
251166 "--skip_clippy" ,
252167 "--lang" ,
253168 language,
254169 "--wasm-file" ,
255- path,
170+ path. to_str ( ) . unwrap ( ) ,
256171 "--out-dir" ,
257- generate_dir
258- )
259- . stderr_to_stdout ( )
260- . stdout_capture ( )
261- . unchecked ( )
262- . run ( )
263- . expect ( "Error running spacetime generate" ) ;
264-
265- status_ok_or_panic ( output, "spacetime generate" , test_name) ;
172+ & generate_dir,
173+ ] ) ;
266174}
267175
268176fn split_command_string ( command : & str ) -> ( & str , Vec < & str > ) {
0 commit comments