Skip to content

Commit c4b38c2

Browse files
committed
fix: tests
1 parent 52d8933 commit c4b38c2

File tree

2 files changed

+37
-33
lines changed

2 files changed

+37
-33
lines changed

sqlx-core/src/pool/connect.rs

+9-6
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,19 @@ use std::io;
3333
/// use sqlx::PgConnection;
3434
/// use sqlx::postgres::PgPoolOptions;
3535
/// use sqlx::Connection;
36+
/// use sqlx::pool::PoolConnectMetadata;
3637
///
37-
/// # async fn _example() -> sqlx::Result<()> {
38-
/// // `PoolConnector` is implemented for closures but has restrictions on returning borrows
39-
/// // due to current language limitations.
38+
/// async fn _example() -> sqlx::Result<()> {
39+
/// // `PoolConnector` is implemented for closures but this has restrictions on returning borrows
40+
/// // due to current language limitations. Custom implementations are not subject to this.
4041
/// //
4142
/// // This example shows how to get around this using `Arc`.
4243
/// let database_url: Arc<str> = "postgres://...".into();
4344
///
4445
/// let pool = PgPoolOptions::new()
4546
/// .min_connections(5)
4647
/// .max_connections(30)
47-
/// .connect_with_connector(move |meta| {
48+
/// .connect_with_connector(move |meta: PoolConnectMetadata| {
4849
/// let database_url = database_url.clone();
4950
/// async move {
5051
/// println!(
@@ -57,7 +58,9 @@ use std::io;
5758
/// let mut conn = PgConnection::connect(&database_url).await?;
5859
///
5960
/// // Override the time zone of the connection.
60-
/// sqlx::raw_sql("SET TIME ZONE 'Europe/Berlin'").await?;
61+
/// sqlx::raw_sql("SET TIME ZONE 'Europe/Berlin'")
62+
/// .execute(&mut conn)
63+
/// .await?;
6164
///
6265
/// Ok(conn)
6366
/// }
@@ -76,7 +79,7 @@ use std::io;
7679
///
7780
/// ```rust,no_run
7881
/// use std::sync::Arc;
79-
/// use tokio::sync::{Mutex, RwLock};
82+
/// use tokio::sync::RwLock;
8083
/// use sqlx::PgConnection;
8184
/// use sqlx::postgres::PgConnectOptions;
8285
/// use sqlx::postgres::PgPoolOptions;

tests/any/pool.rs

+28-27
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ use std::sync::{
55
Arc, Mutex,
66
};
77
use std::time::Duration;
8+
use sqlx_core::connection::ConnectOptions;
9+
use sqlx_core::pool::PoolConnectMetadata;
810

911
#[sqlx_macros::test]
1012
async fn pool_should_invoke_after_connect() -> anyhow::Result<()> {
@@ -83,38 +85,13 @@ async fn test_pool_callbacks() -> anyhow::Result<()> {
8385

8486
sqlx_test::setup_if_needed();
8587

86-
let conn_options: AnyConnectOptions = std::env::var("DATABASE_URL")?.parse()?;
88+
let conn_options: Arc<AnyConnectOptions> = Arc::new(std::env::var("DATABASE_URL")?.parse()?);
8789

8890
let current_id = AtomicI32::new(0);
8991

9092
let pool = AnyPoolOptions::new()
9193
.max_connections(1)
9294
.acquire_timeout(Duration::from_secs(5))
93-
.after_connect(move |conn, meta| {
94-
assert_eq!(meta.age, Duration::ZERO);
95-
assert_eq!(meta.idle_for, Duration::ZERO);
96-
97-
let id = current_id.fetch_add(1, Ordering::AcqRel);
98-
99-
Box::pin(async move {
100-
let statement = format!(
101-
// language=SQL
102-
r#"
103-
CREATE TEMPORARY TABLE conn_stats(
104-
id int primary key,
105-
before_acquire_calls int default 0,
106-
after_release_calls int default 0
107-
);
108-
INSERT INTO conn_stats(id) VALUES ({});
109-
"#,
110-
// Until we have generalized bind parameters
111-
id
112-
);
113-
114-
conn.execute(&statement[..]).await?;
115-
Ok(())
116-
})
117-
})
11895
.before_acquire(|conn, meta| {
11996
// `age` and `idle_for` should both be nonzero
12097
assert_ne!(meta.age, Duration::ZERO);
@@ -165,7 +142,31 @@ async fn test_pool_callbacks() -> anyhow::Result<()> {
165142
})
166143
})
167144
// Don't establish a connection yet.
168-
.connect_lazy_with(conn_options);
145+
.connect_lazy_with_connector(move |meta: PoolConnectMetadata| {
146+
let connect_opts = Arc::clone(&conn_options);
147+
let id = current_id.fetch_add(1, Ordering::AcqRel);
148+
149+
async move {
150+
let mut conn = connect_opts.connect().await?;
151+
152+
let statement = format!(
153+
// language=SQL
154+
r#"
155+
CREATE TEMPORARY TABLE conn_stats(
156+
id int primary key,
157+
before_acquire_calls int default 0,
158+
after_release_calls int default 0
159+
);
160+
INSERT INTO conn_stats(id) VALUES ({});
161+
"#,
162+
// Until we have generalized bind parameters
163+
id
164+
);
165+
166+
conn.execute(&statement[..]).await?;
167+
Ok(conn)
168+
}
169+
});
169170

170171
// Expected pattern of (id, before_acquire_calls, after_release_calls)
171172
let pattern = [

0 commit comments

Comments
 (0)