Make test suites generated by phx.gen.auth use async: true - #5689
Merged
josevalim merged 1 commit intoJan 6, 2024
Merged
Conversation
Member
|
💚 💙 💜 💛 ❤️ |
SteffenDE
pushed a commit
that referenced
this pull request
Aug 2, 2026
… apps (#6783) Fixes a long-standing flaky test issue in generated SQLite applications where running `mix test` would intermittently fail with: ** (Exqlite.Error) Database busy Root Cause `phx.new` generated `error_html_test.exs` and `error_json_test.exs` with hardcoded `use ConnCase, async: true`. In apps with Ecto enabled, `ConnCase` includes a setup block that calls `DataCase.setup_sandbox(tags)`. When ExUnit runs an `async: true` test module concurrently with `async: false` test modules (such as tests generated by `phx.gen.auth`), `setup_sandbox` sees `tags[:async] == true` and checks out a non-shared secondary connection handle (`shared: false`) from the Repo pool. Because SQLite uses single-file database locking, having two active database connections opening sandbox transactions concurrently causes file lock contention whenever a synchronous test inserts records (e.g. `user_fixture()`). Depending on ExUnit test seeding and process scheduling, the secondary connection checkout fails with `(Exqlite.Error) Database busy`. Background When `--database sqlite3` support was added in 5143696 (#4268, April 2021), error controller test templates retained hardcoded `use ConnCase, async: true`. Later, when `phx.gen.auth` introduced support for `async: true` in 3d609e0 (#5689, January 2024), it added a helper to conditionally append `async: true` ONLY for PostgreSQL adapters while leaving non-Postgres adapters as `async: false`. However, `phx.new` was not updated at the time to use the same logic for its generated error test templates. Fix 1. Add `test_case_options/1` in `Phx.New.Generator` matching the behavior of `phx.gen.auth`: - `Ecto.Adapters.Postgres` -> `", async: true"` - `nil` (no Ecto) -> `", async: true"` - non-Postgres adapters (SQLite, MySQL, TDS) -> `""` 2. Update `error_html_test.exs.eex` and `error_json_test.exs.eex` templates to use `<%= @test_case_options %>`. 3. Added test coverage across single app and umbrella project generators for Postgres, `--no-ecto`, SQLite3, MySQL, and MSSQL.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Test suites generated by
mix phx.gen.authconditionally setasync: trueif the database adapter is Postgres. Some of those test suites are missing that condition despite no apparent issues running tests concurrently.I used these steps to test this out:
mix phx.newmix phx.gen.auth Accounts User usersand run migrationsfor X in 1 2 3 4 5 6 7 8 9 10; do mix test; doneI've ran these tests many times and I did not see any failures.
Originally posed as a question here, pinging @josevalim as requested :)