Skip to content

Commit

Permalink
chore(monitoring): use constants
Browse files Browse the repository at this point in the history
commit-id:873e4d65
  • Loading branch information
Itay-Tsabary-Starkware committed Nov 10, 2024
1 parent b408419 commit 8801e88
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 11 deletions.
9 changes: 6 additions & 3 deletions crates/monitoring_endpoint/src/monitoring_endpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ use crate::config::MonitoringEndpointConfig;
mod monitoring_endpoint_test;

pub(crate) const MONITORING_PREFIX: &str = "monitoring";
pub(crate) const ALIVE: &str = "alive";
pub(crate) const READY: &str = "ready";
pub(crate) const VERSION: &str = "nodeVersion";

pub struct MonitoringEndpoint {
config: MonitoringEndpointConfig,
Expand Down Expand Up @@ -49,15 +52,15 @@ impl MonitoringEndpoint {

Router::new()
.route(
format!("/{MONITORING_PREFIX}/alive").as_str(),
format!("/{MONITORING_PREFIX}/{ALIVE}").as_str(),
get(move || async { StatusCode::OK.to_string() }),
)
.route(
format!("/{MONITORING_PREFIX}/ready").as_str(),
format!("/{MONITORING_PREFIX}/{READY}").as_str(),
get(move || async { StatusCode::OK.to_string() }),
)
.route(
format!("/{MONITORING_PREFIX}/nodeVersion").as_str(),
format!("/{MONITORING_PREFIX}/{VERSION}").as_str(),
get(move || async { version }),
)
}
Expand Down
17 changes: 11 additions & 6 deletions crates/monitoring_endpoint/src/monitoring_endpoint_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,17 @@ use tokio::task::yield_now;
use tower::ServiceExt;

use super::MonitoringEndpointConfig;
use crate::monitoring_endpoint::{create_monitoring_endpoint, MonitoringEndpoint};
use crate::monitoring_endpoint::{
create_monitoring_endpoint,
MonitoringEndpoint,
ALIVE,
READY,
VERSION,
};
use crate::test_utils::build_request;

// TODO(Tsabary): Clear feature dependencies and dev dependencies.

// TODO(Lev): Change method strings to constants.
const TEST_VERSION: &str = "1.2.3-dev";

fn setup_monitoring_endpoint() -> MonitoringEndpoint {
Expand All @@ -31,7 +36,7 @@ async fn request_app(app: Router, method: &str) -> Response<UnsyncBoxBody<Bytes,

#[tokio::test]
async fn test_node_version() {
let response = request_app(setup_monitoring_endpoint().app(), "nodeVersion").await;
let response = request_app(setup_monitoring_endpoint().app(), VERSION).await;
assert_eq!(response.status(), StatusCode::OK);

let body = to_bytes(response.into_body()).await.unwrap();
Expand All @@ -40,13 +45,13 @@ async fn test_node_version() {

#[tokio::test]
async fn test_alive() {
let response = request_app(setup_monitoring_endpoint().app(), "alive").await;
let response = request_app(setup_monitoring_endpoint().app(), ALIVE).await;
assert_eq!(response.status(), StatusCode::OK);
}

#[tokio::test]
async fn test_ready() {
let response = request_app(setup_monitoring_endpoint().app(), "ready").await;
let response = request_app(setup_monitoring_endpoint().app(), READY).await;
assert_eq!(response.status(), StatusCode::OK);
}

Expand All @@ -59,7 +64,7 @@ async fn test_endpoint_as_server() {

let client = Client::new();

let response = client.request(build_request(&ip, port, "nodeVersion")).await.unwrap();
let response = client.request(build_request(&ip, port, VERSION)).await.unwrap();
assert_eq!(response.status(), StatusCode::OK);

let body = to_bytes(response.into_body()).await.unwrap();
Expand Down
4 changes: 2 additions & 2 deletions crates/monitoring_endpoint/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use hyper::client::HttpConnector;
use hyper::Client;
use tracing::info;

use crate::monitoring_endpoint::MONITORING_PREFIX;
use crate::monitoring_endpoint::{ALIVE, MONITORING_PREFIX};

// TODO(Tsabary): Clean feature dependencies and dev dependencies.

Expand All @@ -28,7 +28,7 @@ impl IsAliveClient {
info!("Querying the node for aliveness.");

self.client
.request(build_request(&self.socket.ip(), self.socket.port(), "alive"))
.request(build_request(&self.socket.ip(), self.socket.port(), ALIVE))
.await
.map_or(false, |response| response.status().is_success())
}
Expand Down

0 comments on commit 8801e88

Please sign in to comment.