Switch to connection pool for concurrency.
This commit is contained in:
parent
d09533b678
commit
e7bb6ea08f
1 changed files with 24 additions and 17 deletions
41
src/lib.rs
41
src/lib.rs
|
@ -1,14 +1,15 @@
|
||||||
use std::error::Error;
|
use std::error::Error;
|
||||||
|
|
||||||
use mysql_async::{Conn, prelude::*};
|
use mysql_async::{Pool, prelude::*};
|
||||||
|
|
||||||
pub async fn load_data(conn: &mut Conn) -> Result<(), Box<dyn Error>> {
|
pub async fn load_data(pool: &Pool) -> Result<(), Box<dyn Error>> {
|
||||||
// Obviously use something better/more robust here if you're a) loading
|
// Obviously use something better/more robust here if you're a) loading
|
||||||
// other data sources and b) aren't so lucky as to have a single `;` after
|
// other data sources and b) aren't so lucky as to have a single `;` after
|
||||||
// each block of SQL. :)
|
// each block of SQL. :)
|
||||||
let data = String::from_utf8_lossy(include_bytes!("../data.sql"));
|
let data = String::from_utf8_lossy(include_bytes!("../data.sql"));
|
||||||
for stmt in data.split(";") {
|
for stmt in data.split(";") {
|
||||||
if !stmt.is_empty() {
|
if !stmt.trim().is_empty() {
|
||||||
|
let mut conn = pool.get_conn().await?;
|
||||||
conn.exec_drop(stmt, params::Params::Empty).await?;
|
conn.exec_drop(stmt, params::Params::Empty).await?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -21,36 +22,42 @@ mod test {
|
||||||
|
|
||||||
use derive_more::{Deref, DerefMut};
|
use derive_more::{Deref, DerefMut};
|
||||||
|
|
||||||
|
use mysql_async::Pool;
|
||||||
use testcontainers_modules::testcontainers::ContainerAsync;
|
use testcontainers_modules::testcontainers::ContainerAsync;
|
||||||
|
|
||||||
// This is a bit YOLO but is the quickest/cleanest way I can think of to a) get
|
// This is a bit YOLO but is the quickest/cleanest way I can think of to a) get
|
||||||
// a connection per database and b) ensure `Drop` cleans it up.
|
// a connection per database and b) ensure `Drop` cleans it up.
|
||||||
#[derive(Deref, DerefMut)]
|
#[derive(Deref, DerefMut)]
|
||||||
struct TestConnection(
|
struct TestPool(
|
||||||
#[deref]
|
#[deref]
|
||||||
#[deref_mut]
|
#[deref_mut]
|
||||||
mysql_async::Conn,
|
Pool,
|
||||||
// Never read, only needed for `Drop`.
|
// Never read, only needed for `Drop`.
|
||||||
#[allow(dead_code)] ContainerAsync<testcontainers_modules::mysql::Mysql>,
|
#[allow(dead_code)] ContainerAsync<testcontainers_modules::mysql::Mysql>,
|
||||||
);
|
);
|
||||||
|
|
||||||
async fn get_test_connection() -> Result<TestConnection, Box<dyn Error>> {
|
impl TestPool {
|
||||||
use testcontainers_modules::{mysql, testcontainers::runners::AsyncRunner};
|
async fn try_new() -> Result<TestPool, Box<dyn Error>> {
|
||||||
let mysql = mysql::Mysql::default().start().await?;
|
use testcontainers_modules::{mysql, testcontainers::runners::AsyncRunner};
|
||||||
let url = format!(
|
let mysql = mysql::Mysql::default().start().await?;
|
||||||
"mysql://root@{}:{}/test",
|
let url = format!(
|
||||||
mysql.get_host().await?,
|
"mysql://root@{}:{}/test",
|
||||||
mysql.get_host_port_ipv4(3306).await?
|
mysql.get_host().await?,
|
||||||
);
|
mysql.get_host_port_ipv4(3306).await?
|
||||||
let conn = mysql_async::Conn::from_url(&url).await?;
|
);
|
||||||
Ok(TestConnection(conn, mysql))
|
let pool = Pool::from_url(&url)?;
|
||||||
|
Ok(TestPool(pool, mysql))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn test_mysql() -> Result<(), Box<dyn std::error::Error>> {
|
async fn test_mysql() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
let mut conn = get_test_connection().await?;
|
let pool = TestPool::try_new().await?;
|
||||||
time_test::time_test!();
|
time_test::time_test!();
|
||||||
crate::load_data(&mut conn).await?;
|
crate::load_data(&pool).await?;
|
||||||
|
<mysql_async::Pool as Clone>::clone(&pool)
|
||||||
|
.disconnect()
|
||||||
|
.await?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue