-
-
Notifications
You must be signed in to change notification settings - Fork 454
/
connection_tests.rs
142 lines (113 loc) Β· 4.82 KB
/
connection_tests.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#![allow(unused_imports, dead_code)]
pub mod common;
pub use common::{bakery_chain::*, setup::*, TestContext};
use pretty_assertions::assert_eq;
use sea_orm::prelude::*;
#[sea_orm_macros::test]
pub async fn connection_ping() {
let ctx = TestContext::new("connection_ping").await;
ctx.db.ping().await.unwrap();
ctx.delete().await;
}
#[sea_orm_macros::test]
#[cfg(feature = "sqlx-mysql")]
pub async fn connection_ping_closed_mysql() {
let ctx = std::rc::Rc::new(Box::new(TestContext::new("connection_ping_closed").await));
let ctx_ping = std::rc::Rc::clone(&ctx);
ctx.db.get_mysql_connection_pool().close().await;
assert_eq!(
ctx_ping.db.ping().await,
Err(DbErr::ConnectionAcquire(ConnAcquireErr::ConnectionClosed))
);
let base_url = std::env::var("DATABASE_URL").unwrap();
let mut opt = sea_orm::ConnectOptions::new(format!("{base_url}/connection_ping_closed"));
opt
// The connection pool has a single connection only
.max_connections(1)
// A controlled connection acquire timeout
.acquire_timeout(std::time::Duration::from_secs(2));
let db = sea_orm::Database::connect(opt).await.unwrap();
async fn transaction_blocked(db: &DatabaseConnection) {
let _txn = sea_orm::TransactionTrait::begin(db).await.unwrap();
// Occupy the only connection, thus forcing others fail to acquire connection
tokio::time::sleep(std::time::Duration::from_secs(3)).await;
}
async fn transaction(db: &DatabaseConnection) {
// Should fail to acquire
let txn = sea_orm::TransactionTrait::begin(db).await;
assert_eq!(
txn.expect_err("should be a time out"),
crate::DbErr::ConnectionAcquire(ConnAcquireErr::Timeout)
)
}
tokio::join!(transaction_blocked(&db), transaction(&db));
ctx.delete().await;
}
#[sea_orm_macros::test]
#[cfg(feature = "sqlx-sqlite")]
pub async fn connection_ping_closed_sqlite() {
let ctx = std::rc::Rc::new(Box::new(TestContext::new("connection_ping_closed").await));
let ctx_ping = std::rc::Rc::clone(&ctx);
ctx.db.get_sqlite_connection_pool().close().await;
assert_eq!(
ctx_ping.db.ping().await,
Err(DbErr::ConnectionAcquire(ConnAcquireErr::ConnectionClosed))
);
let base_url = std::env::var("DATABASE_URL").unwrap();
let mut opt = sea_orm::ConnectOptions::new(base_url);
opt
// The connection pool has a single connection only
.max_connections(1)
// A controlled connection acquire timeout
.acquire_timeout(std::time::Duration::from_secs(2));
let db = sea_orm::Database::connect(opt).await.unwrap();
async fn transaction_blocked(db: &DatabaseConnection) {
let _txn = sea_orm::TransactionTrait::begin(db).await.unwrap();
// Occupy the only connection, thus forcing others fail to acquire connection
tokio::time::sleep(std::time::Duration::from_secs(3)).await;
}
async fn transaction(db: &DatabaseConnection) {
// Should fail to acquire
let txn = sea_orm::TransactionTrait::begin(db).await;
assert_eq!(
txn.expect_err("should be a time out"),
crate::DbErr::ConnectionAcquire(ConnAcquireErr::Timeout)
)
}
tokio::join!(transaction_blocked(&db), transaction(&db));
ctx.delete().await;
}
#[sea_orm_macros::test]
#[cfg(feature = "sqlx-postgres")]
pub async fn connection_ping_closed_postgres() {
let ctx = std::rc::Rc::new(Box::new(TestContext::new("connection_ping_closed").await));
let ctx_ping = std::rc::Rc::clone(&ctx);
ctx.db.get_postgres_connection_pool().close().await;
assert_eq!(
ctx_ping.db.ping().await,
Err(DbErr::ConnectionAcquire(ConnAcquireErr::ConnectionClosed))
);
let base_url = std::env::var("DATABASE_URL").unwrap();
let mut opt = sea_orm::ConnectOptions::new(format!("{base_url}/connection_ping_closed"));
opt
// The connection pool has a single connection only
.max_connections(1)
// A controlled connection acquire timeout
.acquire_timeout(std::time::Duration::from_secs(2));
let db = sea_orm::Database::connect(opt).await.unwrap();
async fn transaction_blocked(db: &DatabaseConnection) {
let _txn = sea_orm::TransactionTrait::begin(db).await.unwrap();
// Occupy the only connection, thus forcing others fail to acquire connection
tokio::time::sleep(std::time::Duration::from_secs(3)).await;
}
async fn transaction(db: &DatabaseConnection) {
// Should fail to acquire
let txn = sea_orm::TransactionTrait::begin(db).await;
assert_eq!(
txn.expect_err("should be a time out"),
crate::DbErr::ConnectionAcquire(ConnAcquireErr::Timeout)
)
}
tokio::join!(transaction_blocked(&db), transaction(&db));
ctx.delete().await;
}