diff --git a/waku/src/node_management/mod.rs b/waku/src/node_management/mod.rs index e273c93..c19d941 100644 --- a/waku/src/node_management/mod.rs +++ b/waku/src/node_management/mod.rs @@ -12,7 +12,7 @@ use crate::general::Result; pub use config::WakuNodeConfig; /// Shared flag to check if a waku node is already running in the current process -static WAKU_NODE_RUNNING: Mutex = Mutex::new(false); +static WAKU_NODE_INITIALIZED: Mutex = Mutex::new(false); /// Marker trait to disallow undesired waku node states in the handle pub trait WakuNodeState {} @@ -37,32 +37,38 @@ impl WakuNodeHandle { node::waku_listen_addressses() } } +fn stop_node() -> Result<()> { + let mut node_initialized = WAKU_NODE_INITIALIZED + .lock() + .expect("Access to the mutex at some point"); + *node_initialized = false; + node::waku_stop().map(|_| ()) +} impl WakuNodeHandle { pub fn start(self) -> Result> { - let mut node_running = WAKU_NODE_RUNNING - .lock() - .expect("Access to the mutex at some point"); - if *node_running { - return Err("Waku node is already running".into()); - } - match node::waku_start() { - Ok(_) => { - *node_running = true; - Ok(WakuNodeHandle(Default::default())) - } - Err(e) => Err(e), - } + node::waku_start().map(|_| WakuNodeHandle(Default::default())) + } + + pub fn stop(self) -> Result<()> { + stop_node() } } impl WakuNodeHandle { pub fn stop(self) -> Result<()> { - node::waku_stop().map(|_| ()) + stop_node() } } pub fn waku_new(config: Option) -> Result> { + let mut node_initialized = WAKU_NODE_INITIALIZED + .lock() + .expect("Access to the mutex at some point"); + if *node_initialized { + return Err("Waku node is already initialized".into()); + } + *node_initialized = true; node::waku_new(config).map(|_| WakuNodeHandle(Default::default())) } @@ -73,9 +79,9 @@ mod tests { #[test] fn exclusive_running() { let handle1 = waku_new(None).unwrap(); - let handle2 = waku_new(None).unwrap(); - let stop_handle1 = handle1.start().unwrap(); - assert!(handle2.start().is_err()); - stop_handle1.stop().unwrap(); + let handle2 = waku_new(None); + assert!(handle2.is_err()); + let stop_handle = handle1.start().unwrap(); + stop_handle.stop().unwrap(); } } diff --git a/waku/src/node_management/node.rs b/waku/src/node_management/node.rs index af2e28a..1a90ed1 100644 --- a/waku/src/node_management/node.rs +++ b/waku/src/node_management/node.rs @@ -96,6 +96,7 @@ mod test { let id = waku_peer_id().unwrap(); dbg!(&id); assert!(!id.is_empty()); + waku_stop().unwrap(); } #[test] @@ -104,5 +105,6 @@ mod test { let addresses = waku_listen_addressses().unwrap(); dbg!(&addresses); assert!(!addresses.is_empty()); + waku_stop().unwrap(); } }