Commit a6e9e63
authored
Only use async: true for ConnCase error tests on Postgres and no-Ecto 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.1 parent 1abb125 commit a6e9e63
5 files changed
Lines changed: 84 additions & 11 deletions
File tree
- installer
- lib/phx_new
- templates/phx_test/controllers
- test
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
326 | 326 | | |
327 | 327 | | |
328 | 328 | | |
329 | | - | |
| 329 | + | |
| 330 | + | |
330 | 331 | | |
331 | 332 | | |
332 | 333 | | |
| |||
410 | 411 | | |
411 | 412 | | |
412 | 413 | | |
| 414 | + | |
| 415 | + | |
| 416 | + | |
| 417 | + | |
413 | 418 | | |
414 | 419 | | |
415 | 420 | | |
| |||
Lines changed: 1 addition & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | | - | |
| 2 | + | |
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
| |||
Lines changed: 1 addition & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | | - | |
| 2 | + | |
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
89 | 89 | | |
90 | 90 | | |
91 | 91 | | |
92 | | - | |
93 | | - | |
| 92 | + | |
| 93 | + | |
94 | 94 | | |
95 | 95 | | |
96 | 96 | | |
| |||
557 | 557 | | |
558 | 558 | | |
559 | 559 | | |
| 560 | + | |
| 561 | + | |
| 562 | + | |
560 | 563 | | |
561 | 564 | | |
562 | 565 | | |
| |||
686 | 689 | | |
687 | 690 | | |
688 | 691 | | |
| 692 | + | |
| 693 | + | |
| 694 | + | |
| 695 | + | |
| 696 | + | |
| 697 | + | |
| 698 | + | |
| 699 | + | |
689 | 700 | | |
690 | 701 | | |
691 | 702 | | |
| |||
716 | 727 | | |
717 | 728 | | |
718 | 729 | | |
719 | | - | |
720 | | - | |
| 730 | + | |
| 731 | + | |
| 732 | + | |
| 733 | + | |
| 734 | + | |
| 735 | + | |
| 736 | + | |
| 737 | + | |
| 738 | + | |
| 739 | + | |
| 740 | + | |
| 741 | + | |
721 | 742 | | |
722 | 743 | | |
723 | 744 | | |
| |||
747 | 768 | | |
748 | 769 | | |
749 | 770 | | |
| 771 | + | |
| 772 | + | |
| 773 | + | |
| 774 | + | |
| 775 | + | |
| 776 | + | |
| 777 | + | |
| 778 | + | |
750 | 779 | | |
751 | 780 | | |
752 | 781 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
150 | 150 | | |
151 | 151 | | |
152 | 152 | | |
153 | | - | |
154 | | - | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
155 | 164 | | |
156 | 165 | | |
157 | 166 | | |
| |||
625 | 634 | | |
626 | 635 | | |
627 | 636 | | |
| 637 | + | |
| 638 | + | |
| 639 | + | |
| 640 | + | |
| 641 | + | |
| 642 | + | |
| 643 | + | |
| 644 | + | |
| 645 | + | |
| 646 | + | |
628 | 647 | | |
629 | 648 | | |
630 | 649 | | |
| |||
652 | 671 | | |
653 | 672 | | |
654 | 673 | | |
| 674 | + | |
| 675 | + | |
| 676 | + | |
| 677 | + | |
| 678 | + | |
| 679 | + | |
| 680 | + | |
| 681 | + | |
| 682 | + | |
| 683 | + | |
655 | 684 | | |
656 | 685 | | |
657 | 686 | | |
| |||
679 | 708 | | |
680 | 709 | | |
681 | 710 | | |
| 711 | + | |
| 712 | + | |
| 713 | + | |
| 714 | + | |
| 715 | + | |
| 716 | + | |
| 717 | + | |
| 718 | + | |
| 719 | + | |
| 720 | + | |
682 | 721 | | |
683 | 722 | | |
684 | 723 | | |
| |||
822 | 861 | | |
823 | 862 | | |
824 | 863 | | |
825 | | - | |
826 | | - | |
| 864 | + | |
| 865 | + | |
827 | 866 | | |
828 | 867 | | |
829 | 868 | | |
| |||
0 commit comments