@@ -22,10 +22,18 @@ pub fn get_test_server_settings() -> (ServerSettings, Url) {
22
22
( server, base)
23
23
}
24
24
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 > >
26
33
where
27
34
F : FnOnce ( ) -> R + Send + ' static ,
28
35
R : Future < Output = Result < ( ) , anyhow:: Error > > + Send ,
36
+ FCheck : Fn ( reqwest:: Response ) -> bool ,
29
37
{
30
38
let server_handle = tokio:: spawn ( async move { run ( ) . await } ) ;
31
39
@@ -34,18 +42,27 @@ where
34
42
35
43
let wait_health_check = async {
36
44
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 ( ) )
39
47
. query ( & [ ( "service" , "" ) ] )
40
48
. send ( )
41
49
. await
42
50
{
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
+ }
44
60
}
45
61
}
46
62
} ;
47
63
// 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 ( ) {
49
66
match timeout ( Duration :: from_secs ( 1 ) , server_handle) . await {
50
67
Ok ( Ok ( result) ) => {
51
68
panic ! ( "Server terminated with: {result:?}" )
0 commit comments