Skip to content

Commit ea7d589

Browse files
authored
docs: document sqlx test connection limits (#4278)
1 parent 4893f83 commit ea7d589

1 file changed

Lines changed: 32 additions & 0 deletions

File tree

src/macros/test.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,38 @@ This feature is activated by changing the signature of your test function. The f
4141

4242
Where `DB` is a supported `Database` type and `Ret` is `()` or `Result<_, _>`.
4343

44+
##### Concurrent Tests and Connection Limits
45+
46+
Each `#[sqlx::test]` gets its own database and pool, but all tests in the same process still share the
47+
database server's connection limit. If many tests run in parallel and fail with `PoolTimedOut`, reduce
48+
the test runner's concurrency, increase the server's connection limit, or reduce the per-test pool size.
49+
50+
To reduce the per-test pool size, use the `PoolOptions` signature and create the pool explicitly:
51+
52+
```rust,no_run
53+
# #[cfg(all(feature = "migrate", feature = "postgres"))]
54+
# mod example {
55+
use sqlx::{pool::PoolOptions, postgres::PgConnectOptions, Postgres};
56+
57+
#[sqlx::test]
58+
async fn basic_test(
59+
pool_options: PoolOptions<Postgres>,
60+
connect_options: PgConnectOptions,
61+
) -> sqlx::Result<()> {
62+
let pool = pool_options
63+
.max_connections(1)
64+
.connect_with(connect_options)
65+
.await?;
66+
67+
sqlx::query("SELECT 1").execute(&pool).await?;
68+
69+
Ok(())
70+
}
71+
# }
72+
```
73+
74+
You can also limit Rust's built-in test runner with `cargo test -- --test-threads <N>`.
75+
4476
##### Supported Databases
4577

4678
Most of these will require you to set `DATABASE_URL` as an environment variable

0 commit comments

Comments
 (0)