1
use super::*;
2
use crate::command::account::ListAccounts;
3
use crate::command::config::{GetConfig, ListConfig, SetConfig};
4
use crate::config::ConfigError;
5
use crate::db::install_test_pool;
6
use crate::error::ServerError;
7
use crate::exhausted_pool::exhausted;
8
use crate::log_capture::Captured;
9
use sqlx::PgPool;
10
use sqlx::types::Uuid;
11
use std::time::Duration;
12
use supp_macro::local_db_sqlx_test;
13

            
14
5
async fn setup() {}
15

            
16
3
fn pool_timed_out(err: &CmdError) -> bool {
17
3
    matches!(
18
3
        err,
19
        CmdError::Server(ServerError::DB(DBError::Sqlx(sqlx::Error::PoolTimedOut)))
20
    )
21
3
}
22

            
23
#[local_db_sqlx_test]
24
async fn a_user_command_on_an_exhausted_pool_keeps_the_pool_timeout(
25
    pool: PgPool,
26
) -> anyhow::Result<()> {
27
    let (held_pool, _held) = exhausted(&pool, Duration::from_millis(50)).await?;
28
    let _guard = install_test_pool(held_pool);
29

            
30
    let err = ListAccounts::new()
31
        .user_id(Uuid::new_v4())
32
        .run()
33
        .await
34
        .expect_err("no connection is free");
35

            
36
    assert!(pool_timed_out(&err), "{err:?}");
37
    assert!(err.is_unavailable());
38
}
39

            
40
#[local_db_sqlx_test]
41
async fn the_directory_connection_keeps_the_pool_timeout(pool: PgPool) -> anyhow::Result<()> {
42
    let (held_pool, _held) = exhausted(&pool, Duration::from_millis(50)).await?;
43
    let _guard = install_test_pool(held_pool);
44

            
45
    let refused = admin_connection().await.err();
46

            
47
1
    assert!(refused.is_some_and(|err| pool_timed_out(&err)));
48
}
49

            
50
#[local_db_sqlx_test]
51
async fn a_refused_connection_is_logged_once(pool: PgPool) -> anyhow::Result<()> {
52
    let (held_pool, _held) = exhausted(&pool, Duration::from_millis(50)).await?;
53
    let _guard = install_test_pool(held_pool);
54
    let captured = Captured::default();
55
    let _logs = tracing::subscriber::set_default(captured.subscriber());
56

            
57
    let refused = connection(&User { id: Uuid::new_v4() }).await.err();
58

            
59
1
    assert!(refused.is_some_and(|err| pool_timed_out(&err)));
60
    assert_eq!(captured.error_lines(), 1);
61
}
62

            
63
#[test]
64
1
fn a_refused_connection_that_is_not_unavailable_is_left_to_its_caller() {
65
1
    let captured = Captured::default();
66
1
    let _logs = tracing::subscriber::set_default(captured.subscriber());
67

            
68
1
    let err = refused(DBError::MissingUrl);
69

            
70
1
    assert!(!err.is_unavailable());
71
1
    assert_eq!(captured.error_lines(), 0);
72
1
}
73

            
74
#[local_db_sqlx_test]
75
async fn a_config_command_on_an_exhausted_pool_logs_once_and_is_unavailable(
76
    pool: PgPool,
77
) -> anyhow::Result<()> {
78
    let (held_pool, _held) = exhausted(&pool, Duration::from_millis(50)).await?;
79
    let _guard = install_test_pool(held_pool);
80
    let captured = Captured::default();
81
    let _logs = tracing::subscriber::set_default(captured.subscriber());
82

            
83
    let err = GetConfig::new()
84
        .user_id(Uuid::new_v4())
85
        .name("k".to_string())
86
        .run()
87
        .await
88
        .expect_err("no connection is free");
89

            
90
    assert!(err.is_unavailable(), "{err:?}");
91
    assert_eq!(captured.error_lines(), 1);
92
}
93

            
94
#[local_db_sqlx_test]
95
async fn listing_and_setting_config_on_an_exhausted_pool_are_unavailable(
96
    pool: PgPool,
97
) -> anyhow::Result<()> {
98
    let (held_pool, _held) = exhausted(&pool, Duration::from_millis(50)).await?;
99
    let _guard = install_test_pool(held_pool);
100
    let user_id = Uuid::new_v4();
101

            
102
    let listed = ListConfig::new().user_id(user_id).run().await;
103
    let set = SetConfig::new()
104
        .user_id(user_id)
105
        .name("k".to_string())
106
        .value("v".to_string())
107
        .run()
108
        .await;
109

            
110
1
    assert!(listed.is_err_and(|err| err.is_unavailable()));
111
1
    assert!(set.is_err_and(|err| err.is_unavailable()));
112
}
113

            
114
#[test]
115
1
fn pool_and_tenant_conditions_are_unavailable() {
116
1
    let unavailable = [
117
1
        DBError::PoolBudgetExhausted,
118
1
        DBError::TenantQuarantined,
119
1
        DBError::TenantDisabled,
120
1
        DBError::UnknownTenant,
121
1
        DBError::Sqlx(sqlx::Error::PoolTimedOut),
122
1
        DBError::Sqlx(sqlx::Error::PoolClosed),
123
1
    ];
124
1
    for cause in unavailable {
125
6
        let message = cause.to_string();
126
6
        let err = CmdError::from(cause);
127
6
        assert!(err.is_unavailable(), "{message}");
128
6
        assert!(matches!(&err, CmdError::Server(ServerError::DB(_))));
129
    }
130
1
}
131

            
132
#[test]
133
1
fn other_database_faults_are_not_unavailable() {
134
1
    let faults = [
135
1
        DBError::MissingUrl,
136
1
        DBError::UrlAlreadyResolved,
137
1
        DBError::Sqlx(sqlx::Error::RowNotFound),
138
1
        DBError::Config(ConfigError::NoConfig("k".into())),
139
1
    ];
140
1
    for cause in faults {
141
4
        let message = cause.to_string();
142
4
        assert!(!CmdError::from(cause).is_unavailable(), "{message}");
143
    }
144
1
}