1
1
use crate :: util:: { RequestHelper , TestApp } ;
2
+ use deadpool_diesel:: postgres:: Pool ;
3
+ use deadpool_diesel:: Timeouts ;
2
4
use http:: StatusCode ;
3
5
use std:: time:: Duration ;
4
6
5
7
const DB_HEALTHY_TIMEOUT : Duration = Duration :: from_millis ( 2000 ) ;
6
8
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
+
7
20
#[ test]
8
21
fn http_error_with_unhealthy_database ( ) {
9
22
let ( app, anon) = TestApp :: init ( ) . with_chaos_proxy ( ) . empty ( ) ;
@@ -17,10 +30,7 @@ fn http_error_with_unhealthy_database() {
17
30
assert_eq ! ( response. status( ) , StatusCode :: SERVICE_UNAVAILABLE ) ;
18
31
19
32
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) ;
24
34
25
35
let response = anon. get :: < ( ) > ( "/api/v1/summary" ) ;
26
36
assert_eq ! ( response. status( ) , StatusCode :: OK ) ;
@@ -43,10 +53,7 @@ fn fallback_to_replica_returns_user_info() {
43
53
44
54
// restore primary database connection
45
55
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) ;
50
57
}
51
58
52
59
#[ test]
@@ -67,22 +74,19 @@ fn restored_replica_returns_user_info() {
67
74
68
75
// Once the replica database is restored, it should serve as a fallback again
69
76
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
72
80
. 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) ;
76
83
77
84
let response = owner. get :: < ( ) > ( URL ) ;
78
85
assert_eq ! ( response. status( ) , StatusCode :: OK ) ;
79
86
80
87
// restore connection
81
88
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) ;
86
90
}
87
91
88
92
#[ test]
@@ -103,10 +107,7 @@ fn restored_primary_returns_user_info() {
103
107
104
108
// Once the replica database is restored, it should serve as a fallback again
105
109
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) ;
110
111
111
112
let response = owner. get :: < ( ) > ( URL ) ;
112
113
assert_eq ! ( response. status( ) , StatusCode :: OK ) ;
0 commit comments