Lines
100 %
Functions
Branches
use super::*;
use crate::command::account::ListAccounts;
use crate::command::config::{GetConfig, ListConfig, SetConfig};
use crate::config::ConfigError;
use crate::db::install_test_pool;
use crate::error::ServerError;
use crate::exhausted_pool::exhausted;
use crate::log_capture::Captured;
use sqlx::PgPool;
use sqlx::types::Uuid;
use std::time::Duration;
use supp_macro::local_db_sqlx_test;
async fn setup() {}
fn pool_timed_out(err: &CmdError) -> bool {
matches!(
err,
CmdError::Server(ServerError::DB(DBError::Sqlx(sqlx::Error::PoolTimedOut)))
)
}
#[local_db_sqlx_test]
async fn a_user_command_on_an_exhausted_pool_keeps_the_pool_timeout(
pool: PgPool,
) -> anyhow::Result<()> {
let (held_pool, _held) = exhausted(&pool, Duration::from_millis(50)).await?;
let _guard = install_test_pool(held_pool);
let err = ListAccounts::new()
.user_id(Uuid::new_v4())
.run()
.await
.expect_err("no connection is free");
assert!(pool_timed_out(&err), "{err:?}");
assert!(err.is_unavailable());
async fn the_directory_connection_keeps_the_pool_timeout(pool: PgPool) -> anyhow::Result<()> {
let refused = admin_connection().await.err();
assert!(refused.is_some_and(|err| pool_timed_out(&err)));
async fn a_refused_connection_is_logged_once(pool: PgPool) -> anyhow::Result<()> {
let captured = Captured::default();
let _logs = tracing::subscriber::set_default(captured.subscriber());
let refused = connection(&User { id: Uuid::new_v4() }).await.err();
assert_eq!(captured.error_lines(), 1);
#[test]
fn a_refused_connection_that_is_not_unavailable_is_left_to_its_caller() {
let err = refused(DBError::MissingUrl);
assert!(!err.is_unavailable());
assert_eq!(captured.error_lines(), 0);
async fn a_config_command_on_an_exhausted_pool_logs_once_and_is_unavailable(
let err = GetConfig::new()
.name("k".to_string())
assert!(err.is_unavailable(), "{err:?}");
async fn listing_and_setting_config_on_an_exhausted_pool_are_unavailable(
let user_id = Uuid::new_v4();
let listed = ListConfig::new().user_id(user_id).run().await;
let set = SetConfig::new()
.user_id(user_id)
.value("v".to_string())
.await;
assert!(listed.is_err_and(|err| err.is_unavailable()));
assert!(set.is_err_and(|err| err.is_unavailable()));
fn pool_and_tenant_conditions_are_unavailable() {
let unavailable = [
DBError::PoolBudgetExhausted,
DBError::TenantQuarantined,
DBError::TenantDisabled,
DBError::UnknownTenant,
DBError::Sqlx(sqlx::Error::PoolTimedOut),
DBError::Sqlx(sqlx::Error::PoolClosed),
];
for cause in unavailable {
let message = cause.to_string();
let err = CmdError::from(cause);
assert!(err.is_unavailable(), "{message}");
assert!(matches!(&err, CmdError::Server(ServerError::DB(_))));
fn other_database_faults_are_not_unavailable() {
let faults = [
DBError::MissingUrl,
DBError::UrlAlreadyResolved,
DBError::Sqlx(sqlx::Error::RowNotFound),
DBError::Config(ConfigError::NoConfig("k".into())),
for cause in faults {
assert!(!CmdError::from(cause).is_unavailable(), "{message}");