11use async_process:: Command ;
22use dlopen:: raw:: AddressInfoObtainer ;
33use futures:: prelude:: * ;
4- use ipc_channel:: ipc:: { IpcOneShotServer , IpcReceiver , IpcSender } ;
54pub use libpacket:: * ;
65use netsim_embed_core:: * ;
76pub use netsim_embed_core:: { DelayBuffer , Ipv4Range , Protocol } ;
@@ -73,12 +72,13 @@ where
7372 & mut self . machines
7473 }
7574
75+ #[ cfg( feature = "ipc" ) ]
7676 pub async fn spawn < T : ' static + Send > (
7777 & mut self ,
78- function : fn ( IpcReceiver < T > ) ,
79- ) -> ( MachineId , IpcSender < T > ) {
78+ function : fn ( ipc_channel :: ipc :: IpcReceiver < T > ) ,
79+ ) -> ( MachineId , ipc_channel :: ipc :: IpcSender < T > ) {
8080 let name = get_fn_name ( function) ;
81- let ( server, server_name) = IpcOneShotServer :: new ( ) . unwrap ( ) ;
81+ let ( server, server_name) = ipc_channel :: ipc :: IpcOneShotServer :: new ( ) . unwrap ( ) ;
8282 let mut command = Command :: new ( std:: env:: current_exe ( ) . unwrap ( ) ) ;
8383 command. args ( [ "--netsim-embed-internal-call" , & name, & server_name] ) ;
8484 let machine = self . spawn_machine ( command, None ) . await ;
@@ -205,7 +205,8 @@ where
205205 }
206206}
207207
208- pub fn get_fn_name < T > ( function : fn ( IpcReceiver < T > ) ) -> String {
208+ #[ cfg( feature = "ipc" ) ]
209+ pub fn get_fn_name < T > ( function : fn ( ipc_channel:: ipc:: IpcReceiver < T > ) ) -> String {
209210 let info = AddressInfoObtainer :: new ( )
210211 . obtain ( function as * const ( ) )
211212 . expect ( "look up existing function pointer" ) ;
@@ -269,8 +270,9 @@ pub struct NatConfig {
269270/// println!("cargo:rustc-link-arg-tests=-rdynamic");
270271/// }
271272/// ```
273+ #[ cfg( feature = "ipc" ) ]
272274#[ macro_export]
273- macro_rules! dispatch_args {
275+ macro_rules! declare_machines {
274276 ( $( $fn: path) ,* ) => { {
275277 let mut args = std:: env:: args( ) ;
276278 args. next( ) ;
@@ -279,8 +281,8 @@ macro_rules! dispatch_args {
279281 let server_name = args. next( ) . unwrap( ) ;
280282 $(
281283 if function == $crate:: get_fn_name( $fn) {
282- let ( sender, receiver) = ipc_channel :: ipc:: channel( ) . unwrap( ) ;
283- let server_sender = ipc_channel :: ipc:: IpcSender :: connect( server_name) . unwrap( ) ;
284+ let ( sender, receiver) = $crate :: test_util :: ipc:: channel( ) . unwrap( ) ;
285+ let server_sender = $crate :: test_util :: ipc:: IpcSender :: connect( server_name) . unwrap( ) ;
284286 server_sender. send( sender) . unwrap( ) ;
285287 $fn( receiver) ;
286288 std:: process:: exit( 0 ) ;
@@ -290,3 +292,41 @@ macro_rules! dispatch_args {
290292 }
291293 } }
292294}
295+
296+ #[ cfg( feature = "ipc" ) ]
297+ pub mod test_util {
298+ pub struct TestResult ( anyhow:: Result < ( ) > ) ;
299+ impl TestResult {
300+ pub fn into_inner ( self ) -> anyhow:: Result < ( ) > {
301+ self . 0
302+ }
303+ }
304+ impl From < ( ) > for TestResult {
305+ fn from ( _: ( ) ) -> Self {
306+ Self ( Ok ( ( ) ) )
307+ }
308+ }
309+ impl < E : std:: error:: Error + Send + Sync + ' static > From < Result < ( ) , E > > for TestResult {
310+ fn from ( res : Result < ( ) , E > ) -> Self {
311+ Self ( res. map_err ( Into :: into) )
312+ }
313+ }
314+ pub use ipc_channel:: ipc;
315+ pub use libtest_mimic:: { run, Arguments , Trial } ;
316+ }
317+
318+ #[ cfg( feature = "ipc" ) ]
319+ #[ macro_export]
320+ macro_rules! run_tests {
321+ ( $( $fn: path) ,* ) => { {
322+ $crate:: unshare_user( ) . unwrap( ) ;
323+ let args = $crate:: test_util:: Arguments :: from_args( ) ;
324+ let tests = vec![
325+ $( $crate:: test_util:: Trial :: test( stringify!( $fn) , || {
326+ $crate:: test_util:: TestResult :: from( $fn( ) ) . into_inner( ) ?;
327+ Ok ( ( ) )
328+ } ) ) ,*
329+ ] ;
330+ $crate:: test_util:: run( & args, tests) . exit( ) ;
331+ } } ;
332+ }
0 commit comments