diff --git a/homestar-core/src/test_utils/ports.rs b/homestar-core/src/test_utils/ports.rs index 362c2868e..673fc0fa4 100644 --- a/homestar-core/src/test_utils/ports.rs +++ b/homestar-core/src/test_utils/ports.rs @@ -9,6 +9,6 @@ static PORTS: OnceCell = OnceCell::new(); /// Return a unique port(in runtime) for test pub fn get_port() -> usize { PORTS - .get_or_init(|| AtomicUsize::new(rand::thread_rng().gen_range(3000..3800))) + .get_or_init(|| AtomicUsize::new(rand::thread_rng().gen_range(3000..6800))) .fetch_add(1, Ordering::Relaxed) } diff --git a/homestar-runtime/src/metrics/node.rs b/homestar-runtime/src/metrics/node.rs index bb58f389d..d25e575dc 100644 --- a/homestar-runtime/src/metrics/node.rs +++ b/homestar-runtime/src/metrics/node.rs @@ -108,8 +108,8 @@ pub(crate) fn describe() { } /// Collect node metrics on a settings-defined interval. -pub(crate) async fn collect_metrics(interval: u64) { - let mut interval = tokio::time::interval(Duration::from_millis(interval)); +pub(crate) async fn collect_metrics(interval: Duration) { + let mut interval = tokio::time::interval(interval); // Log static system info log_static_info(); diff --git a/homestar-runtime/src/network/webserver.rs b/homestar-runtime/src/network/webserver.rs index e0332a3e5..ce5efee6b 100644 --- a/homestar-runtime/src/network/webserver.rs +++ b/homestar-runtime/src/network/webserver.rs @@ -233,11 +233,15 @@ mod test { use notifier::NotifyReceipt; use tokio::sync::mpsc; + fn set_ports(settings: &mut Settings) { + settings.node.network.metrics_port = test_utils::ports::get_port() as u16; + settings.node.network.webserver_port = test_utils::ports::get_port() as u16; + } + #[tokio::test] async fn ws_connect() { let mut settings = Settings::load().unwrap(); - settings.node.network.metrics_port = 6000; - settings.node.network.webserver_port = 1200; + set_ports(&mut settings); let server = Server::new(settings.node().network()).unwrap(); #[cfg(feature = "monitoring")] let metrics_hdl = metrics::start(settings.monitoring(), settings.node.network()) @@ -272,8 +276,8 @@ mod test { #[tokio::test] async fn ws_metrics_no_prefix() { let mut settings = Settings::load().unwrap(); - settings.node.network.metrics_port = 6001; - settings.node.network.webserver_port = 1201; + set_ports(&mut settings); + settings.monitoring.process_collector_interval = Duration::from_millis(2); let server = Server::new(settings.node().network()).unwrap(); let metrics_hdl = metrics::start(settings.monitoring(), settings.node.network()) @@ -284,6 +288,9 @@ mod test { let ws_url = format!("ws://{}", server.addr); + // wait for interval to pass + std::thread::sleep(Duration::from_millis(2)); + let client = WsClientBuilder::default().build(ws_url).await.unwrap(); let ws_resp1: serde_json::Value = client .request(rpc::METRICS_ENDPOINT, rpc_params![]) @@ -303,8 +310,7 @@ mod test { #[tokio::test] async fn ws_subscribe_unsubscribe_network_events() { let mut settings = Settings::load().unwrap(); - settings.node.network.metrics_port = 6002; - settings.node.network.webserver_port = 1202; + set_ports(&mut settings); let server = Server::new(settings.node().network()).unwrap(); #[cfg(feature = "monitoring")] let metrics_hdl = metrics::start(settings.monitoring(), settings.node.network()) @@ -341,8 +347,7 @@ mod test { #[tokio::test] async fn ws_subscribe_workflow_incorrect_params() { let mut settings = Settings::load().unwrap(); - settings.node.network.metrics_port = 6003; - settings.node.network.webserver_port = 1203; + set_ports(&mut settings); let server = Server::new(settings.node().network()).unwrap(); let metrics_hdl = metrics::start(settings.monitoring(), settings.node.network()) .await @@ -375,8 +380,7 @@ mod test { #[tokio::test] async fn ws_subscribe_workflow_runner_timeout() { let mut settings = Settings::load().unwrap(); - settings.node.network.metrics_port = 6004; - settings.node.network.webserver_port = 1204; + set_ports(&mut settings); let server = Server::new(settings.node().network()).unwrap(); let metrics_hdl = metrics::start(settings.monitoring(), settings.node.network()) .await diff --git a/homestar-runtime/src/network/webserver/rpc.rs b/homestar-runtime/src/network/webserver/rpc.rs index da449917f..6749585df 100644 --- a/homestar-runtime/src/network/webserver/rpc.rs +++ b/homestar-runtime/src/network/webserver/rpc.rs @@ -96,7 +96,7 @@ impl Context { } } -/// TODO +/// [RpcModule] wrapper. pub(crate) struct JsonRpc(RpcModule); impl JsonRpc { @@ -106,8 +106,8 @@ impl JsonRpc { Ok(Self(module)) } - #[allow(dead_code)] /// Get a reference to the inner [RpcModule]. + #[allow(dead_code)] pub(crate) fn inner(&self) -> &RpcModule { &self.0 } diff --git a/homestar-runtime/src/settings.rs b/homestar-runtime/src/settings.rs index f4ce65a96..c8d3b0814 100644 --- a/homestar-runtime/src/settings.rs +++ b/homestar-runtime/src/settings.rs @@ -33,13 +33,25 @@ impl Settings { } } -/// Process monitoring settings. +/// Monitoring settings. +#[cfg(feature = "monitoring")] +#[serde_as] #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)] pub struct Monitoring { /// Tokio console port. pub console_subscriber_port: u16, /// Monitoring collection interval in milliseconds. - pub process_collector_interval: u64, + #[serde_as(as = "DurationMilliSeconds")] + pub process_collector_interval: Duration, +} + +/// Monitoring settings. +#[cfg(not(feature = "monitoring"))] +#[serde_as] +#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)] +pub struct Monitoring { + /// Tokio console port. + pub console_subscriber_port: u16, } /// Server settings. @@ -166,10 +178,20 @@ pub(crate) struct Database { pub(crate) max_pool_size: u32, } +#[cfg(feature = "monitoring")] +impl Default for Monitoring { + fn default() -> Self { + Self { + process_collector_interval: Duration::from_millis(5000), + console_subscriber_port: 5555, + } + } +} + +#[cfg(not(feature = "monitoring"))] impl Default for Monitoring { fn default() -> Self { Self { - process_collector_interval: 5000, console_subscriber_port: 5555, } } diff --git a/homestar-runtime/src/test_utils/proc_macro/src/lib.rs b/homestar-runtime/src/test_utils/proc_macro/src/lib.rs index f40a5c7ef..928e3b495 100644 --- a/homestar-runtime/src/test_utils/proc_macro/src/lib.rs +++ b/homestar-runtime/src/test_utils/proc_macro/src/lib.rs @@ -100,6 +100,7 @@ pub fn runner_test(_attr: TokenStream, item: TokenStream) -> TokenStream { let mut settings = crate::Settings::load().unwrap(); settings.node.network.webserver_port = ::homestar_core::test_utils::ports::get_port() as u16; settings.node.network.rpc_port = ::homestar_core::test_utils::ports::get_port() as u16; + settings.node.network.metrics_port = ::homestar_core::test_utils::ports::get_port() as u16; settings.node.db.url = Some(format!("{}.db", #func_name_as_string)); let db = crate::test_utils::db::MemoryDb::setup_connection_pool(&settings.node, None).unwrap(); let runner = crate::Runner::start(settings.clone(), db).unwrap(); diff --git a/homestar-runtime/tests/metrics.rs b/homestar-runtime/tests/metrics.rs index 02fda3bc4..e764b8885 100644 --- a/homestar-runtime/tests/metrics.rs +++ b/homestar-runtime/tests/metrics.rs @@ -13,6 +13,7 @@ use std::{ static BIN: Lazy = Lazy::new(|| assert_cmd::cargo::cargo_bin(BIN_NAME)); const METRICS_URL: &str = "http://localhost:4020"; +#[cfg(feature = "monitoring")] #[test] #[file_serial] fn test_metrics_serial() -> Result<()> {