From: lain Date: Fri, 18 Dec 2020 18:49:01 +0000 (+0100) Subject: Tests: Use NullCache for async tests. X-Git-Url: https://git.squeep.com/?a=commitdiff_plain;h=95a9bdfc374a013be47e74b25bdba5d91f51948b;p=akkoma Tests: Use NullCache for async tests. Caching can't work in async tests, so for them it is mocked to a null cache that is always empty. Synchronous tests are stubbed with the real Cachex, which is emptied after every test. --- diff --git a/config/test.exs b/config/test.exs index 2a20a03e7..397bc688e 100644 --- a/config/test.exs +++ b/config/test.exs @@ -121,6 +121,8 @@ config :tzdata, :autoupdate, :disabled config :pleroma, :mrf, policies: [] +config :pleroma, :cachex, provider: Pleroma.CachexMock + if File.exists?("./config/test.secret.exs") do import_config "test.secret.exs" else diff --git a/lib/pleroma/emoji/pack.ex b/lib/pleroma/emoji/pack.ex index 5a1a1a6c6..ec97aa652 100644 --- a/lib/pleroma/emoji/pack.ex +++ b/lib/pleroma/emoji/pack.ex @@ -417,7 +417,7 @@ defmodule Pleroma.Emoji.Pack do ttl_per_file = Pleroma.Config.get!([:emoji, :shared_pack_cache_seconds_per_file]) overall_ttl = :timer.seconds(ttl_per_file * Enum.count(files)) - @cachex.put!( + @cachex.put( :emoji_packs_cache, pack.name, # if pack.json MD5 changes, the cache is not valid anymore diff --git a/lib/pleroma/web/admin_api/controllers/media_proxy_cache_controller.ex b/lib/pleroma/web/admin_api/controllers/media_proxy_cache_controller.ex index ecd369037..2f712fb8c 100644 --- a/lib/pleroma/web/admin_api/controllers/media_proxy_cache_controller.ex +++ b/lib/pleroma/web/admin_api/controllers/media_proxy_cache_controller.ex @@ -40,7 +40,7 @@ defmodule Pleroma.Web.AdminAPI.MediaProxyCacheController do defp fetch_entries(params) do MediaProxy.cache_table() - |> @cachex.stream!(@cachex.Query.create(true, :key)) + |> @cachex.stream!(Cachex.Query.create(true, :key)) |> filter_entries(params[:query]) end diff --git a/test/pleroma/reverse_proxy_test.exs b/test/pleroma/reverse_proxy_test.exs index 8df63de65..0a2c169ce 100644 --- a/test/pleroma/reverse_proxy_test.exs +++ b/test/pleroma/reverse_proxy_test.exs @@ -3,8 +3,7 @@ # SPDX-License-Identifier: AGPL-3.0-only defmodule Pleroma.ReverseProxyTest do - use Pleroma.Web.ConnCase, async: true - + use Pleroma.Web.ConnCase import ExUnit.CaptureLog import Mox diff --git a/test/pleroma/web/media_proxy/invalidation_test.exs b/test/pleroma/web/media_proxy/invalidation_test.exs index b9f1066f3..b7be36b47 100644 --- a/test/pleroma/web/media_proxy/invalidation_test.exs +++ b/test/pleroma/web/media_proxy/invalidation_test.exs @@ -3,8 +3,7 @@ # SPDX-License-Identifier: AGPL-3.0-only defmodule Pleroma.Web.MediaProxy.InvalidationTest do - use ExUnit.Case - use Pleroma.Tests.Helpers + use Pleroma.DataCase alias Pleroma.Config alias Pleroma.Web.MediaProxy.Invalidation diff --git a/test/pleroma/web/plugs/idempotency_plug_test.exs b/test/pleroma/web/plugs/idempotency_plug_test.exs index 4a7835993..910ecd9c1 100644 --- a/test/pleroma/web/plugs/idempotency_plug_test.exs +++ b/test/pleroma/web/plugs/idempotency_plug_test.exs @@ -3,7 +3,7 @@ # SPDX-License-Identifier: AGPL-3.0-only defmodule Pleroma.Web.Plugs.IdempotencyPlugTest do - use ExUnit.Case, async: true + use Pleroma.DataCase use Plug.Test alias Pleroma.Web.Plugs.IdempotencyPlug diff --git a/test/support/cachex_proxy.ex b/test/support/cachex_proxy.ex new file mode 100644 index 000000000..e296b5c6a --- /dev/null +++ b/test/support/cachex_proxy.ex @@ -0,0 +1,40 @@ +# Pleroma: A lightweight social networking server +# Copyright © 2017-2020 Pleroma Authors +# SPDX-License-Identifier: AGPL-3.0-only + +defmodule Pleroma.CachexProxy do + @behaviour Pleroma.Caching + + @impl true + defdelegate get!(cache, key), to: Cachex + + @impl true + defdelegate stream!(cache, key), to: Cachex + + @impl true + defdelegate put(cache, key, value, options), to: Cachex + + @impl true + defdelegate put(cache, key, value), to: Cachex + + @impl true + defdelegate get_and_update(cache, key, func), to: Cachex + + @impl true + defdelegate get(cache, key), to: Cachex + + @impl true + defdelegate fetch!(cache, key, func), to: Cachex + + @impl true + defdelegate expire_at(cache, str, num), to: Cachex + + @impl true + defdelegate exists?(cache, key), to: Cachex + + @impl true + defdelegate del(cache, key), to: Cachex + + @impl true + defdelegate execute!(cache, func), to: Cachex +end diff --git a/test/support/channel_case.ex b/test/support/channel_case.ex index 114184a9f..f4696adb3 100644 --- a/test/support/channel_case.ex +++ b/test/support/channel_case.ex @@ -33,8 +33,14 @@ defmodule Pleroma.Web.ChannelCase do setup tags do :ok = Ecto.Adapters.SQL.Sandbox.checkout(Pleroma.Repo) - unless tags[:async] do + if tags[:async] do + Mox.stub_with(Pleroma.CachexMock, Pleroma.NullCache) + Mox.set_mox_private() + else Ecto.Adapters.SQL.Sandbox.mode(Pleroma.Repo, {:shared, self()}) + Mox.stub_with(Pleroma.CachexMock, Pleroma.CachexProxy) + Mox.set_mox_global() + Pleroma.DataCase.clear_cachex() end :ok diff --git a/test/support/conn_case.ex b/test/support/conn_case.ex index b5bd71809..a7cebf971 100644 --- a/test/support/conn_case.ex +++ b/test/support/conn_case.ex @@ -118,8 +118,13 @@ defmodule Pleroma.Web.ConnCase do setup tags do :ok = Ecto.Adapters.SQL.Sandbox.checkout(Pleroma.Repo) - unless tags[:async] do + if tags[:async] do + Mox.stub_with(Pleroma.CachexMock, Pleroma.NullCache) + Mox.set_mox_private() + else Ecto.Adapters.SQL.Sandbox.mode(Pleroma.Repo, {:shared, self()}) + Mox.stub_with(Pleroma.CachexMock, Pleroma.CachexProxy) + Mox.set_mox_global() Pleroma.DataCase.clear_cachex() end diff --git a/test/support/data_case.ex b/test/support/data_case.ex index 1f1d40863..a3ce9e282 100644 --- a/test/support/data_case.ex +++ b/test/support/data_case.ex @@ -65,8 +65,13 @@ defmodule Pleroma.DataCase do setup tags do :ok = Ecto.Adapters.SQL.Sandbox.checkout(Pleroma.Repo) - unless tags[:async] do + if tags[:async] do + Mox.stub_with(Pleroma.CachexMock, Pleroma.NullCache) + Mox.set_mox_private() + else Ecto.Adapters.SQL.Sandbox.mode(Pleroma.Repo, {:shared, self()}) + Mox.stub_with(Pleroma.CachexMock, Pleroma.CachexProxy) + Mox.set_mox_global() clear_cachex() end diff --git a/test/support/mocks.ex b/test/support/mocks.ex new file mode 100644 index 000000000..d790553cd --- /dev/null +++ b/test/support/mocks.ex @@ -0,0 +1,5 @@ +# Pleroma: A lightweight social networking server +# Copyright © 2017-2020 Pleroma Authors +# SPDX-License-Identifier: AGPL-3.0-only + +Mox.defmock(Pleroma.CachexMock, for: Pleroma.Caching) diff --git a/test/support/null_cache.ex b/test/support/null_cache.ex new file mode 100644 index 000000000..72e7c996a --- /dev/null +++ b/test/support/null_cache.ex @@ -0,0 +1,47 @@ +# Pleroma: A lightweight social networking server +# Copyright © 2017-2020 Pleroma Authors +# SPDX-License-Identifier: AGPL-3.0-only + +defmodule Pleroma.NullCache do + @moduledoc """ + A module simulating a permanently empty cache. + """ + @behaviour Pleroma.Caching + + @impl true + def get!(_, _), do: nil + + @impl true + def put(_, _, _, _ \\ nil), do: {:ok, true} + + @impl true + def stream!(_, _), do: [] + + @impl true + def get(_, _), do: {:ok, nil} + + @impl true + def fetch!(_, _, func) do + {_, res} = func.() + res + end + + @impl true + def get_and_update(_, _, func) do + func.(nil) + end + + @impl true + def expire_at(_, _, _), do: {:ok, true} + + @impl true + def exists?(_, _), do: {:ok, false} + + @impl true + def execute!(_, func) do + func.(:nothing) + end + + @impl true + def del(_, _), do: {:ok, true} +end diff --git a/test/test_helper.exs b/test/test_helper.exs index 25f0ecba6..ee880e226 100644 --- a/test/test_helper.exs +++ b/test/test_helper.exs @@ -3,7 +3,7 @@ # SPDX-License-Identifier: AGPL-3.0-only os_exclude = if :os.type() == {:unix, :darwin}, do: [skip_on_mac: true], else: [] -ExUnit.start(exclude: [:test] ++ [:federated | os_exclude], include: [async: true]) +ExUnit.start(exclude: [:federated | os_exclude]) Ecto.Adapters.SQL.Sandbox.mode(Pleroma.Repo, :manual)