Skip to content

Commit cc55684

Browse files
committed
more extensive healthcheck
1 parent e68639a commit cc55684

File tree

1 file changed

+22
-5
lines changed

1 file changed

+22
-5
lines changed

libs/blockscout-service-launcher/src/test_server.rs

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,18 @@ pub fn get_test_server_settings() -> (ServerSettings, Url) {
2222
(server, base)
2323
}
2424

25-
pub async fn init_server<F, R>(run: F, base: &Url) -> JoinHandle<Result<(), anyhow::Error>>
25+
/// `check_health_response` - additional logic to verify if healthcheck
26+
/// was successful. `true` - success
27+
pub async fn init_server<F, R, FCheck>(
28+
run: F,
29+
base: &Url,
30+
healthcheck_timeout: Option<Duration>,
31+
check_health_response: Option<FCheck>,
32+
) -> JoinHandle<Result<(), anyhow::Error>>
2633
where
2734
F: FnOnce() -> R + Send + 'static,
2835
R: Future<Output = Result<(), anyhow::Error>> + Send,
36+
FCheck: Fn(reqwest::Response) -> bool,
2937
{
3038
let server_handle = tokio::spawn(async move { run().await });
3139

@@ -34,18 +42,27 @@ where
3442

3543
let wait_health_check = async {
3644
loop {
37-
if let Ok(_response) = client
38-
.get(health_endpoint.clone())
45+
if let Ok(response) = client
46+
.request(reqwest::Method::GET, health_endpoint.clone())
3947
.query(&[("service", "")])
4048
.send()
4149
.await
4250
{
43-
break;
51+
if response.status() == reqwest::StatusCode::OK {
52+
if let Some(check_health_response) = &check_health_response {
53+
if check_health_response(response) {
54+
break;
55+
}
56+
} else {
57+
break;
58+
}
59+
}
4460
}
4561
}
4662
};
4763
// Wait for the server to start
48-
if (timeout(Duration::from_secs(30), wait_health_check).await).is_err() {
64+
let timeout_duration = healthcheck_timeout.unwrap_or(Duration::from_secs(15));
65+
if (timeout(timeout_duration, wait_health_check).await).is_err() {
4966
match timeout(Duration::from_secs(1), server_handle).await {
5067
Ok(Ok(result)) => {
5168
panic!("Server terminated with: {result:?}")

0 commit comments

Comments
 (0)