#[sqlx::test] macro failing to connect to the created database occasionally
Bug Description
The #[sqlx::test] macro is occasionally failing to connect to the created database.
Error:
thread 'my_module::test_fn' panicked at 'failed to connect to test database: Database(PgDatabaseError { severity: Fatal, code: "3D000", message: "database \"_sqlx_test_8\" does not exist", detail: Some("It seems to have just been dropped or renamed."), hint: None, position: None, where: None, schema: None, table: None, column: None, data_type: None, constraint: None, file: Some("postinit.c"), line: Some(1050), routine: Some("InitPostgres") })', /home/runner/.cargo/registry/src/index.crates.io-6f17d22bba15001f/sqlx-core-0.6.3/src/testing/mod.rs:243:10
PS: it works fine most of the times, but occasionally it panics with this error.
Minimal Reproduction
The example from documentation should be enough, but informing Pool<Postgres> as the test function argument and with multiple tests functions.
use sqlx::{Postgres, Pool};
#[sqlx::test]
async fn basic_test(pool: Pool<Postgres>) -> sqlx::Result<()> {
let mut conn = pool.acquire().await?;
let foo = sqlx::query("SELECT * FROM foo")
.fetch_one(&mut conn)
.await?;
assert_eq!(foo.get::<String, _>("bar"), "foobar!");
Ok(())
}
Info
- SQLx version: 0.6.3
- SQLx features enabled: ["runtime-tokio-rustls", "postgres", "chrono", "uuid", "offline"]
- Database server and version: Postgres
- Operating system: macos
-
rustc --version: rustc 1.70.0 (90c541806 2023-05-31)
Any ideas about why this is happening? Should it be fixed in the most recent version? I just saw that 0.7.x was released, and I can upgrade it here :)
It may be already fixed by this PR: https://github.com/launchbadge/sqlx/pull/2454 Despite that the error message differs from the one I'm getting here.
I'm gonna update to version 0.7.1 and see if the error stops happening!
Update: The issue is still happening after the upgrade to sqlx 0.7.1.
thread 'my_module::test_fn' panicked at 'failed to connect to test database: Database(PgDatabaseError { severity: Fatal, code: "3D000", message: "database \"_sqlx_test_327\" does not exist", detail: Some("It seems to have just been dropped or renamed."), hint: None, position: None, where: None, schema: None, table: None, column: None, data_type: None, constraint: None, file: Some("postinit.c"), line: Some(1050), routine: Some("InitPostgres") })', /home/runner/.cargo/registry/src/index.crates.io-6f17d22bba15001f/sqlx-core-0.7.1/src/testing/mod.rs:242:10
Update: forcing tests to execute in serial (with just one thread) makes the error go away.
I'm using nextest for it, so when I run like that: cargo nextest run --test-threads 1 it works as expected, but this approach makes the tests take much longer to finish.
PS: the same error occurs when running with cargo test, since it executes the tests in parallel.
PS2: running this command (cargo nextest run) sequentially at least 100-300 times makes it happen. Usually it happens much earlier, in less than 10 tries.
It would be great if #[sqlx::test] worked when running tests in parallel!
I just opened a PR to fix this issue for postgres: https://github.com/launchbadge/sqlx/pull/2635
EDIT: it didn't fix the issue, however, I just opened a new PR that looks promising: https://github.com/launchbadge/sqlx/pull/2640
Still happening on cargo-nextest 0.9.93 with sqlx 0.8.3 in GitHub Actions, although relatively rarely :(
@jssblck I think that is to be expected. https://github.com/launchbadge/sqlx/pull/3334 seems to be a reliable fix (I've been running a git version that has that fix included for a few weeks), and that isn't included in sqlx 0.8.3. So with the next release it should be resolved.