11use crate :: util:: { RequestHelper , TestApp } ;
2+ use deadpool_diesel:: postgres:: Pool ;
3+ use deadpool_diesel:: Timeouts ;
24use http:: StatusCode ;
35use std:: time:: Duration ;
46
57const DB_HEALTHY_TIMEOUT : Duration = Duration :: from_millis ( 2000 ) ;
68
9+ fn default_timeouts ( ) -> Timeouts {
10+ Timeouts :: wait_millis ( DB_HEALTHY_TIMEOUT . as_millis ( ) as u64 )
11+ }
12+
13+ fn wait_until_healthy ( pool : & Pool , app : & TestApp ) {
14+ let _ = app
15+ . runtime ( )
16+ . block_on ( pool. timeout_get ( & default_timeouts ( ) ) )
17+ . expect ( "the database did not return healthy" ) ;
18+ }
19+
720#[ test]
821fn http_error_with_unhealthy_database ( ) {
922 let ( app, anon) = TestApp :: init ( ) . with_chaos_proxy ( ) . empty ( ) ;
@@ -17,10 +30,7 @@ fn http_error_with_unhealthy_database() {
1730 assert_eq ! ( response. status( ) , StatusCode :: SERVICE_UNAVAILABLE ) ;
1831
1932 app. primary_db_chaosproxy ( ) . restore_networking ( ) . unwrap ( ) ;
20- app. as_inner ( )
21- . primary_database
22- . wait_until_healthy ( DB_HEALTHY_TIMEOUT )
23- . expect ( "the database did not return healthy" ) ;
33+ wait_until_healthy ( & app. as_inner ( ) . deadpool_primary , & app) ;
2434
2535 let response = anon. get :: < ( ) > ( "/api/v1/summary" ) ;
2636 assert_eq ! ( response. status( ) , StatusCode :: OK ) ;
@@ -43,10 +53,7 @@ fn fallback_to_replica_returns_user_info() {
4353
4454 // restore primary database connection
4555 app. primary_db_chaosproxy ( ) . restore_networking ( ) . unwrap ( ) ;
46- app. as_inner ( )
47- . primary_database
48- . wait_until_healthy ( DB_HEALTHY_TIMEOUT )
49- . expect ( "the database did not return healthy" ) ;
56+ wait_until_healthy ( & app. as_inner ( ) . deadpool_primary , & app) ;
5057}
5158
5259#[ test]
@@ -67,22 +74,19 @@ fn restored_replica_returns_user_info() {
6774
6875 // Once the replica database is restored, it should serve as a fallback again
6976 app. replica_db_chaosproxy ( ) . restore_networking ( ) . unwrap ( ) ;
70- app. as_inner ( )
71- . read_only_replica_database
77+ let replica = app
78+ . as_inner ( )
79+ . deadpool_replica
7280 . as_ref ( )
73- . expect ( "no replica database configured" )
74- . wait_until_healthy ( DB_HEALTHY_TIMEOUT )
75- . expect ( "the database did not return healthy" ) ;
81+ . expect ( "no replica database configured" ) ;
82+ wait_until_healthy ( replica, & app) ;
7683
7784 let response = owner. get :: < ( ) > ( URL ) ;
7885 assert_eq ! ( response. status( ) , StatusCode :: OK ) ;
7986
8087 // restore connection
8188 app. primary_db_chaosproxy ( ) . restore_networking ( ) . unwrap ( ) ;
82- app. as_inner ( )
83- . primary_database
84- . wait_until_healthy ( DB_HEALTHY_TIMEOUT )
85- . expect ( "the database did not return healthy" ) ;
89+ wait_until_healthy ( & app. as_inner ( ) . deadpool_primary , & app) ;
8690}
8791
8892#[ test]
@@ -103,10 +107,7 @@ fn restored_primary_returns_user_info() {
103107
104108 // Once the replica database is restored, it should serve as a fallback again
105109 app. primary_db_chaosproxy ( ) . restore_networking ( ) . unwrap ( ) ;
106- app. as_inner ( )
107- . primary_database
108- . wait_until_healthy ( DB_HEALTHY_TIMEOUT )
109- . expect ( "the database did not return healthy" ) ;
110+ wait_until_healthy ( & app. as_inner ( ) . deadpool_primary , & app) ;
110111
111112 let response = owner. get :: < ( ) > ( URL ) ;
112113 assert_eq ! ( response. status( ) , StatusCode :: OK ) ;
0 commit comments