From e0e0c84f83a2788202f1bd5e78314c572658c15f Mon Sep 17 00:00:00 2001 From: Tobias Pfeiffer Date: Fri, 4 Oct 2024 10:53:52 +0200 Subject: [PATCH] Pass async to tests, parallelizing them Tests run sync by default. But all of Elixir, essentially, is built to run tests in parallel/async. Unless you mess with the global environment there's usually no reason not to run tests in parallel and it's also best practice to always pass either `async: true` or `async: false` and a reason why they can't be run in parallel. Speeds up the test suite, admittedly not by a lot right now but it's nice. I didn't check all the tests for global state, so this may introduce flakies but on a first couple of runs everything seemed fine. async: ``` root@812d17669fe3:/app# mix test *********.................................................................................................................................................... Finished in 0.7 seconds (0.7s async, 0.00s sync) 157 tests, 0 failures, 9 skipped Randomized with seed 325387 root@812d17669fe3:/app# mix test ***.******................................................................................................................................................... Finished in 0.6 seconds (0.6s async, 0.00s sync) 157 tests, 0 failures, 9 skipped Randomized with seed 874856 root@812d17669fe3:/app# mix test *********.................................................................................................................................................... Finished in 0.7 seconds (0.7s async, 0.00s sync) 157 tests, 0 failures, 9 skipped Randomized with seed 194987 root@812d17669fe3:/app# mix test *********.................................................................................................................................................... Finished in 0.6 seconds (0.6s async, 0.00s sync) 157 tests, 0 failures, 9 skipped Randomized with seed 426291 root@812d17669fe3:/app# mix test ***..******.................................................................................................................................................. Finished in 0.7 seconds (0.7s async, 0.00s sync) 157 tests, 0 failures, 9 skipped ``` sync: ``` root@812d17669fe3:/app# mix test Compiling 3 files (.ex) ......................................................................................******............08:54:11.449 [error] Postgrex.Protocol (#PID<0.394.0>) disconnected: ** (DBConnection.ConnectionError) owner #PID<0.2162.0> exited ...............***................................... Finished in 1.6 seconds (0.00s async, 1.6s sync) 157 tests, 0 failures, 9 skipped Randomized with seed 38709 root@812d17669fe3:/app# mix test .................................................................................******.........................08:54:18.537 [error] Postgrex.Protocol (#PID<0.373.0>) disconnected: ** (DBConnection.ConnectionError) owner #PID<0.2293.0> exited .............***............................. Finished in 1.8 seconds (0.00s async, 1.8s sync) 157 tests, 0 failures, 9 skipped Randomized with seed 848590 root@812d17669fe3:/app# mix test ......................................................................................******...........08:54:22.337 [error] Postgrex.Protocol (#PID<0.374.0>) disconnected: ** (DBConnection.ConnectionError) owner #PID<0.2118.0> exited ..............***..................................... Finished in 1.8 seconds (0.00s async, 1.8s sync) 157 tests, 0 failures, 9 skipped Randomized with seed 729972 root@812d17669fe3:/app# mix test ......................................................................................******........................08:54:26.003 [error] Postgrex.Protocol (#PID<0.375.0>) disconnected: ** (DBConnection.ConnectionError) owner #PID<0.2282.0> exited .......***............................... Finished in 1.9 seconds (0.00s async, 1.9s sync) 157 tests, 0 failures, 9 skipped Randomized with seed 249736 root@812d17669fe3:/app# mix test ..........................................................................******..............................08:54:29.869 [error] Postgrex.Protocol (#PID<0.370.0>) disconnected: ** (DBConnection.ConnectionError) owner #PID<0.2196.0> exited .........***................................... Finished in 1.8 seconds (0.00s async, 1.8s sync) 157 tests, 0 failures, 9 skipped ``` Magically it also seems to have gotten rid off that one error, but really don't ask me why. --- .credo.exs | 2 +- test/mindwendel/accounts_test.exs | 2 +- test/mindwendel/brainstormings/create_brainstorming_test.exs | 2 +- test/mindwendel/brainstormings_test.exs | 2 +- test/mindwendel/brainstormings_users_test.exs | 2 +- test/mindwendel/csv_formatter_test.exs | 2 +- test/mindwendel/help_test.exs | 2 +- test/mindwendel/idea_idea_label_test.exs | 2 +- test/mindwendel/idea_labels_test.exs | 2 +- test/mindwendel/idea_test.exs | 2 +- test/mindwendel/ideas_test.exs | 2 +- test/mindwendel/lanes_test.exs | 2 +- test/mindwendel/likes_test.exs | 2 +- test/mindwendel/url_preview_test.exs | 2 +- test/mindwendel_web/channels/brainstorming_channel_test.exs | 2 +- .../controllers/brainstorming_controller_test.exs | 2 +- .../controllers/static_page_controller_test.exs | 2 +- .../live/admin/brainstorming_live/edit_test.exs | 2 +- .../live/brainstorming_live/show_idea_delete_test.exs | 2 +- .../live/brainstorming_live/show_idea_edit_test.exs | 2 +- .../live/brainstorming_live/show_sort_by_label_test.exs | 2 +- test/mindwendel_web/live/brainstorming_live_test.exs | 2 +- test/mindwendel_web/live/label_live/captions_test.exs | 4 ++-- test/mindwendel_web/live/live_helpers_test.exs | 2 +- .../live/response_header_content_security_policy_test.exs | 2 +- .../migrate_add_lanes_to_brainstormings_test.exs | 2 +- test/repo/data_migrations/migrate_idea_labels_test.exs | 2 +- .../migrate_idea_labels_to_idea_idea_labels_test.exs | 2 +- 28 files changed, 29 insertions(+), 29 deletions(-) diff --git a/.credo.exs b/.credo.exs index 73296ec7..0a8c81f7 100644 --- a/.credo.exs +++ b/.credo.exs @@ -130,6 +130,7 @@ {Credo.Check.Refactor.NegatedConditionsInUnless, []}, {Credo.Check.Refactor.NegatedConditionsWithElse, []}, {Credo.Check.Refactor.Nesting, []}, + {Credo.Check.Refactor.PassAsyncInTestCases, []}, {Credo.Check.Refactor.RedundantWithClauseResult, []}, {Credo.Check.Refactor.RejectReject, []}, {Credo.Check.Refactor.UnlessWithElse, []}, @@ -198,7 +199,6 @@ {Credo.Check.Refactor.MapMap, []}, {Credo.Check.Refactor.ModuleDependencies, []}, {Credo.Check.Refactor.NegatedIsNil, []}, - {Credo.Check.Refactor.PassAsyncInTestCases, []}, {Credo.Check.Refactor.PipeChainStart, []}, {Credo.Check.Refactor.RejectFilter, []}, {Credo.Check.Refactor.VariableRebinding, []}, diff --git a/test/mindwendel/accounts_test.exs b/test/mindwendel/accounts_test.exs index 91f82a4d..404ce23b 100644 --- a/test/mindwendel/accounts_test.exs +++ b/test/mindwendel/accounts_test.exs @@ -1,5 +1,5 @@ defmodule Mindwendel.AccountsTest do - use Mindwendel.DataCase + use Mindwendel.DataCase, async: true alias Mindwendel.Factory alias Mindwendel.Accounts alias Mindwendel.Accounts.User diff --git a/test/mindwendel/brainstormings/create_brainstorming_test.exs b/test/mindwendel/brainstormings/create_brainstorming_test.exs index f1afe6eb..ef01512f 100644 --- a/test/mindwendel/brainstormings/create_brainstorming_test.exs +++ b/test/mindwendel/brainstormings/create_brainstorming_test.exs @@ -1,5 +1,5 @@ defmodule Mindwendel.Brainstormings.CreateBrainstormingTest do - use Mindwendel.DataCase + use Mindwendel.DataCase, async: true alias Mindwendel.Brainstormings.BrainstormingModeratingUser alias Mindwendel.Accounts.BrainstormingUser alias Mindwendel.Factory diff --git a/test/mindwendel/brainstormings_test.exs b/test/mindwendel/brainstormings_test.exs index 1340ed31..0f85d1fe 100644 --- a/test/mindwendel/brainstormings_test.exs +++ b/test/mindwendel/brainstormings_test.exs @@ -1,6 +1,6 @@ defmodule Mindwendel.BrainstormingsTest do alias Mindwendel.Brainstormings.IdeaIdeaLabel - use Mindwendel.DataCase + use Mindwendel.DataCase, async: true alias Mindwendel.Brainstormings.BrainstormingModeratingUser alias Mindwendel.Factory diff --git a/test/mindwendel/brainstormings_users_test.exs b/test/mindwendel/brainstormings_users_test.exs index b5352e4f..047c0795 100644 --- a/test/mindwendel/brainstormings_users_test.exs +++ b/test/mindwendel/brainstormings_users_test.exs @@ -1,5 +1,5 @@ defmodule Mindwendel.AccountsMergeBrainstormingUserTest do - use Mindwendel.DataCase + use Mindwendel.DataCase, async: true alias Mindwendel.Factory alias Mindwendel.Accounts alias Mindwendel.Accounts.User diff --git a/test/mindwendel/csv_formatter_test.exs b/test/mindwendel/csv_formatter_test.exs index 97f54f47..1a917528 100644 --- a/test/mindwendel/csv_formatter_test.exs +++ b/test/mindwendel/csv_formatter_test.exs @@ -1,5 +1,5 @@ defmodule MindwendelServices.CSVFormatter do - use Mindwendel.DataCase + use Mindwendel.DataCase, async: true alias Mindwendel.Factory alias Mindwendel.CSVFormatter diff --git a/test/mindwendel/help_test.exs b/test/mindwendel/help_test.exs index 53975617..349c500c 100644 --- a/test/mindwendel/help_test.exs +++ b/test/mindwendel/help_test.exs @@ -1,5 +1,5 @@ defmodule Mindwendel.HelpTest do - use Mindwendel.DataCase + use Mindwendel.DataCase, async: true alias Mindwendel.Factory alias Mindwendel.Help diff --git a/test/mindwendel/idea_idea_label_test.exs b/test/mindwendel/idea_idea_label_test.exs index 79bbb9ee..e6f1e72e 100644 --- a/test/mindwendel/idea_idea_label_test.exs +++ b/test/mindwendel/idea_idea_label_test.exs @@ -1,5 +1,5 @@ defmodule Mindwendel.IdeaIdeaLabelTest do - use Mindwendel.DataCase + use Mindwendel.DataCase, async: true alias Mindwendel.Factory alias Mindwendel.Brainstormings.IdeaIdeaLabel diff --git a/test/mindwendel/idea_labels_test.exs b/test/mindwendel/idea_labels_test.exs index 53bc5f80..83991037 100644 --- a/test/mindwendel/idea_labels_test.exs +++ b/test/mindwendel/idea_labels_test.exs @@ -1,5 +1,5 @@ defmodule Mindwendel.IdeaLabelsTest do - use Mindwendel.DataCase + use Mindwendel.DataCase, async: true alias Mindwendel.Factory alias Mindwendel.Ideas diff --git a/test/mindwendel/idea_test.exs b/test/mindwendel/idea_test.exs index ac5eae7d..f7c4cd35 100644 --- a/test/mindwendel/idea_test.exs +++ b/test/mindwendel/idea_test.exs @@ -1,5 +1,5 @@ defmodule Mindwendel.IdeaTest do - use Mindwendel.DataCase + use Mindwendel.DataCase, async: true alias Mindwendel.Factory alias Mindwendel.Ideas diff --git a/test/mindwendel/ideas_test.exs b/test/mindwendel/ideas_test.exs index 786cbd3c..f3f466bf 100644 --- a/test/mindwendel/ideas_test.exs +++ b/test/mindwendel/ideas_test.exs @@ -1,5 +1,5 @@ defmodule Mindwendel.IdeasTest do - use Mindwendel.DataCase + use Mindwendel.DataCase, async: true alias Mindwendel.Factory alias Mindwendel.Ideas diff --git a/test/mindwendel/lanes_test.exs b/test/mindwendel/lanes_test.exs index b81b0063..e10dbc68 100644 --- a/test/mindwendel/lanes_test.exs +++ b/test/mindwendel/lanes_test.exs @@ -1,5 +1,5 @@ defmodule Mindwendel.LanesTest do - use Mindwendel.DataCase + use Mindwendel.DataCase, async: true alias Mindwendel.Lanes alias Mindwendel.Brainstormings.Lane import Mindwendel.BrainstormingsFixtures diff --git a/test/mindwendel/likes_test.exs b/test/mindwendel/likes_test.exs index b6e356ce..883ececb 100644 --- a/test/mindwendel/likes_test.exs +++ b/test/mindwendel/likes_test.exs @@ -1,5 +1,5 @@ defmodule Mindwendel.LikesTest do - use Mindwendel.DataCase + use Mindwendel.DataCase, async: true alias Mindwendel.Factory alias Mindwendel.Likes diff --git a/test/mindwendel/url_preview_test.exs b/test/mindwendel/url_preview_test.exs index ed67d47d..3b86ac7b 100644 --- a/test/mindwendel/url_preview_test.exs +++ b/test/mindwendel/url_preview_test.exs @@ -1,5 +1,5 @@ defmodule MindwendelServices.UrlPreviewTest do - use ExUnit.Case + use ExUnit.Case, async: true alias Mindwendel.UrlPreview setup do diff --git a/test/mindwendel_web/channels/brainstorming_channel_test.exs b/test/mindwendel_web/channels/brainstorming_channel_test.exs index 97405458..45d408be 100644 --- a/test/mindwendel_web/channels/brainstorming_channel_test.exs +++ b/test/mindwendel_web/channels/brainstorming_channel_test.exs @@ -1,5 +1,5 @@ defmodule MindwendelWeb.BrainstormingChannelTest do - use MindwendelWeb.ChannelCase + use MindwendelWeb.ChannelCase, async: true alias Mindwendel.Factory alias Mindwendel.Brainstormings diff --git a/test/mindwendel_web/controllers/brainstorming_controller_test.exs b/test/mindwendel_web/controllers/brainstorming_controller_test.exs index 296c0357..6f41bb4b 100644 --- a/test/mindwendel_web/controllers/brainstorming_controller_test.exs +++ b/test/mindwendel_web/controllers/brainstorming_controller_test.exs @@ -1,5 +1,5 @@ defmodule MindwendelWeb.BrainstormingControllerTest do - use MindwendelWeb.ConnCase + use MindwendelWeb.ConnCase, async: true import Ecto.Query diff --git a/test/mindwendel_web/controllers/static_page_controller_test.exs b/test/mindwendel_web/controllers/static_page_controller_test.exs index 1c4d2e89..11411609 100644 --- a/test/mindwendel_web/controllers/static_page_controller_test.exs +++ b/test/mindwendel_web/controllers/static_page_controller_test.exs @@ -1,5 +1,5 @@ defmodule MindwendelWeb.StaticPageControllerTest do - use MindwendelWeb.ConnCase + use MindwendelWeb.ConnCase, async: true alias Mindwendel.Factory alias Mindwendel.Repo diff --git a/test/mindwendel_web/live/admin/brainstorming_live/edit_test.exs b/test/mindwendel_web/live/admin/brainstorming_live/edit_test.exs index 2b2d037c..ce89c6e2 100644 --- a/test/mindwendel_web/live/admin/brainstorming_live/edit_test.exs +++ b/test/mindwendel_web/live/admin/brainstorming_live/edit_test.exs @@ -1,5 +1,5 @@ defmodule MindwendelWeb.Admin.BrainstormingLive.EditTest do - use MindwendelWeb.ConnCase + use MindwendelWeb.ConnCase, async: true import Phoenix.LiveViewTest alias Mindwendel.Factory diff --git a/test/mindwendel_web/live/brainstorming_live/show_idea_delete_test.exs b/test/mindwendel_web/live/brainstorming_live/show_idea_delete_test.exs index 18023cde..17b2f763 100644 --- a/test/mindwendel_web/live/brainstorming_live/show_idea_delete_test.exs +++ b/test/mindwendel_web/live/brainstorming_live/show_idea_delete_test.exs @@ -1,5 +1,5 @@ defmodule MindwendelWeb.BrainstormingLive.ShowIdeaDeleteTest do - use MindwendelWeb.ConnCase + use MindwendelWeb.ConnCase, async: true import Phoenix.LiveViewTest alias Mindwendel.Brainstormings diff --git a/test/mindwendel_web/live/brainstorming_live/show_idea_edit_test.exs b/test/mindwendel_web/live/brainstorming_live/show_idea_edit_test.exs index 65bc150e..317f6b22 100644 --- a/test/mindwendel_web/live/brainstorming_live/show_idea_edit_test.exs +++ b/test/mindwendel_web/live/brainstorming_live/show_idea_edit_test.exs @@ -1,5 +1,5 @@ defmodule MindwendelWeb.BrainstormingLive.ShowIdeaEditTest do - use MindwendelWeb.ConnCase + use MindwendelWeb.ConnCase, async: true import Phoenix.LiveViewTest alias Mindwendel.Brainstormings alias Mindwendel.Accounts.User diff --git a/test/mindwendel_web/live/brainstorming_live/show_sort_by_label_test.exs b/test/mindwendel_web/live/brainstorming_live/show_sort_by_label_test.exs index 0e5046d8..5c95dad0 100644 --- a/test/mindwendel_web/live/brainstorming_live/show_sort_by_label_test.exs +++ b/test/mindwendel_web/live/brainstorming_live/show_sort_by_label_test.exs @@ -1,5 +1,5 @@ defmodule MindwendelWeb.BrainstormingLive.ShowSortByLabelTest do - use MindwendelWeb.ConnCase + use MindwendelWeb.ConnCase, async: true import Phoenix.LiveViewTest alias Mindwendel.Brainstormings diff --git a/test/mindwendel_web/live/brainstorming_live_test.exs b/test/mindwendel_web/live/brainstorming_live_test.exs index a27e260d..b385b395 100644 --- a/test/mindwendel_web/live/brainstorming_live_test.exs +++ b/test/mindwendel_web/live/brainstorming_live_test.exs @@ -1,5 +1,5 @@ defmodule MindwendelWeb.BrainstormingLiveTest do - use MindwendelWeb.ConnCase + use MindwendelWeb.ConnCase, async: true import Phoenix.LiveViewTest alias Mindwendel.Brainstormings diff --git a/test/mindwendel_web/live/label_live/captions_test.exs b/test/mindwendel_web/live/label_live/captions_test.exs index c4cc258f..036fd102 100644 --- a/test/mindwendel_web/live/label_live/captions_test.exs +++ b/test/mindwendel_web/live/label_live/captions_test.exs @@ -1,7 +1,7 @@ defmodule MindwendelWeb.LabelLive.CaptionsTest do - alias MindwendelWeb.LabelLive.CaptionsComponent alias Mindwendel.Brainstormings - use MindwendelWeb.ConnCase + alias MindwendelWeb.LabelLive.CaptionsComponent + use MindwendelWeb.ConnCase, async: true import Phoenix.LiveViewTest alias Mindwendel.Factory diff --git a/test/mindwendel_web/live/live_helpers_test.exs b/test/mindwendel_web/live/live_helpers_test.exs index f5149b9e..267e6436 100644 --- a/test/mindwendel_web/live/live_helpers_test.exs +++ b/test/mindwendel_web/live/live_helpers_test.exs @@ -1,5 +1,5 @@ defmodule MindwendelWeb.LiveHelpersTest do - use MindwendelWeb.ConnCase + use MindwendelWeb.ConnCase, async: true import Phoenix.LiveViewTest alias Mindwendel.Factory diff --git a/test/mindwendel_web/live/response_header_content_security_policy_test.exs b/test/mindwendel_web/live/response_header_content_security_policy_test.exs index 199430f6..90f2d557 100644 --- a/test/mindwendel_web/live/response_header_content_security_policy_test.exs +++ b/test/mindwendel_web/live/response_header_content_security_policy_test.exs @@ -1,5 +1,5 @@ defmodule MindwendelWeb.ResponseHeaderContentSecurityPolicyTest do - use MindwendelWeb.ConnCase + use MindwendelWeb.ConnCase, async: true alias Mindwendel.Factory diff --git a/test/repo/data_migrations/migrate_add_lanes_to_brainstormings_test.exs b/test/repo/data_migrations/migrate_add_lanes_to_brainstormings_test.exs index e2f20fc6..b19384a0 100644 --- a/test/repo/data_migrations/migrate_add_lanes_to_brainstormings_test.exs +++ b/test/repo/data_migrations/migrate_add_lanes_to_brainstormings_test.exs @@ -1,7 +1,7 @@ defmodule Mindwendel.Repo.DataMigrations.MigrateAddLanesToBrainstormingsTest do Code.require_file("./priv/repo/data_migrations/migrate_add_lanes_to_brainstormings.exs") - use Mindwendel.DataCase + use Mindwendel.DataCase, async: true alias Mindwendel.Factory alias Mindwendel.Lanes alias Mindwendel.Repo.DataMigrations.MigrateAddLanesToBrainstormings diff --git a/test/repo/data_migrations/migrate_idea_labels_test.exs b/test/repo/data_migrations/migrate_idea_labels_test.exs index 672a2993..1262a5d0 100644 --- a/test/repo/data_migrations/migrate_idea_labels_test.exs +++ b/test/repo/data_migrations/migrate_idea_labels_test.exs @@ -1,7 +1,7 @@ defmodule Mindwendel.Repo.DataMigrations.MigrateIdealLabelsTest do Code.require_file("./priv/repo/data_migrations/migrate_idea_labels.exs") - use Mindwendel.DataCase + use Mindwendel.DataCase, async: true alias Mindwendel.Factory alias Mindwendel.Repo alias Mindwendel.Brainstormings.Brainstorming diff --git a/test/repo/data_migrations/migrate_idea_labels_to_idea_idea_labels_test.exs b/test/repo/data_migrations/migrate_idea_labels_to_idea_idea_labels_test.exs index c23dce64..5f96ce5b 100644 --- a/test/repo/data_migrations/migrate_idea_labels_to_idea_idea_labels_test.exs +++ b/test/repo/data_migrations/migrate_idea_labels_to_idea_idea_labels_test.exs @@ -1,7 +1,7 @@ defmodule Mindwendel.Repo.DataMigrations.MigrateIdeaLabelsToIdeaIdeaLabelsTest do Code.require_file("./priv/repo/data_migrations/migrate_idea_labels_to_idea_idea_labels.exs") - use Mindwendel.DataCase + use Mindwendel.DataCase, async: true alias Mindwendel.Factory alias Mindwendel.Repo