Skip to content

Commit

Permalink
refactor: cleanup settings
Browse files Browse the repository at this point in the history
  • Loading branch information
zeeshanlakhani committed Oct 31, 2023
1 parent 2c7d2c0 commit cf2282f
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 18 deletions.
2 changes: 1 addition & 1 deletion homestar-core/src/test_utils/ports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ static PORTS: OnceCell<AtomicUsize> = 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)
}
4 changes: 2 additions & 2 deletions homestar-runtime/src/metrics/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
24 changes: 14 additions & 10 deletions homestar-runtime/src/network/webserver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -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())
Expand All @@ -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![])
Expand All @@ -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())
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions homestar-runtime/src/network/webserver/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ impl Context {
}
}

/// TODO
/// [RpcModule] wrapper.
pub(crate) struct JsonRpc(RpcModule<Context>);

impl JsonRpc {
Expand All @@ -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<Context> {
&self.0
}
Expand Down
28 changes: 25 additions & 3 deletions homestar-runtime/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u64>")]
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.
Expand Down Expand Up @@ -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,
}
}
Expand Down
1 change: 1 addition & 0 deletions homestar-runtime/src/test_utils/proc_macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
1 change: 1 addition & 0 deletions homestar-runtime/tests/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use std::{
static BIN: Lazy<PathBuf> = 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<()> {
Expand Down

0 comments on commit cf2282f

Please sign in to comment.