Skip to content

Commit

Permalink
Refactor healthcheck test to make it easier to read
Browse files Browse the repository at this point in the history
  • Loading branch information
FedericoPonzi committed Nov 2, 2024
1 parent a936fe3 commit 965abcd
Showing 1 changed file with 6 additions and 10 deletions.
16 changes: 6 additions & 10 deletions horust/src/horust/healthcheck/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,6 @@ mod test {
use crate::horust::formats::{Healthiness, HealthinessStatus};
use crate::horust::healthcheck::check_health;

fn check_health_w(healthiness: &Healthiness) -> bool {
check_health(healthiness) == HealthinessStatus::Healthy
}

#[test]
fn test_healthiness_check_file() -> Result<()> {
let tempdir = TempDir::new("health")?;
Expand All @@ -166,11 +162,11 @@ mod test {
http_endpoint: None,
..Default::default()
};
assert!(!check_health_w(&healthiness));
assert_ne!(check_health(&healthiness), HealthinessStatus::Healthy);
std::fs::write(file_path, "Hello world!")?;
assert!(check_health_w(&healthiness));
assert_eq!(check_health(&healthiness), HealthinessStatus::Healthy);
let healthiness: Healthiness = Default::default();
assert!(check_health_w(&healthiness));
assert_eq!(check_health(&healthiness), HealthinessStatus::Healthy);
Ok(())
}

Expand All @@ -193,7 +189,7 @@ mod test {
http_endpoint: Some("http://localhost:123/".into()),
..Default::default()
};
assert!(!check_health_w(&healthiness));
assert_ne!(check_health(&healthiness), HealthinessStatus::Healthy);
let loopback = Ipv4Addr::new(127, 0, 0, 1);
let socket = SocketAddrV4::new(loopback, 0);
let listener = TcpListener::bind(socket)?;
Expand All @@ -209,11 +205,11 @@ mod test {
handle_request(listener).unwrap();
sender.send(()).expect("Chan closed");
});
assert!(check_health_w(&healthiness));
assert_eq!(check_health(&healthiness), HealthinessStatus::Healthy);
receiver
.recv_timeout(Duration::from_millis(2000))
.expect("Failed to received response from handle_request");
assert!(!check_health_w(&healthiness));
assert_ne!(check_health(&healthiness), HealthinessStatus::Healthy);
Ok(())
}
}

0 comments on commit 965abcd

Please sign in to comment.