Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#[sqlx::test] macro failing to connect to the created database occasionally #2631

Open
fhsgoncalves opened this issue Jul 19, 2023 · 4 comments
Labels

Comments

@fhsgoncalves
Copy link
Contributor

fhsgoncalves commented Jul 19, 2023

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 :)

@fhsgoncalves
Copy link
Contributor Author

It may be already fixed by this PR:
#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!

@fhsgoncalves
Copy link
Contributor Author

fhsgoncalves commented Jul 19, 2023

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

@fhsgoncalves
Copy link
Contributor Author

fhsgoncalves commented Jul 19, 2023

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!

@fhsgoncalves
Copy link
Contributor Author

fhsgoncalves commented Jul 20, 2023

I just opened a PR to fix this issue for postgres: #2635

EDIT: it didn't fix the issue, however, I just opened a new PR that looks promising: #2640

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant