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.
This commit is contained in:
lain 2020-12-18 19:49:01 +01:00
parent 713612c377
commit 95a9bdfc37
13 changed files with 119 additions and 11 deletions

View file

@ -121,6 +121,8 @@
config :pleroma, :mrf, policies: []
config :pleroma, :cachex, provider: Pleroma.CachexMock
if File.exists?("./config/test.secret.exs") do
import_config "test.secret.exs"
else

View file

@ -417,7 +417,7 @@ defp create_archive_and_cache(pack, hash) 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

View file

@ -40,7 +40,7 @@ def index(%{assigns: %{user: _}} = conn, params) 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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -0,0 +1,40 @@
# Pleroma: A lightweight social networking server
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
# 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

View file

@ -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

View file

@ -118,8 +118,13 @@ defp json_response_and_validate_schema(conn, _status) 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

View file

@ -65,8 +65,13 @@ def clear_cachex 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

5
test/support/mocks.ex Normal file
View file

@ -0,0 +1,5 @@
# Pleroma: A lightweight social networking server
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
Mox.defmock(Pleroma.CachexMock, for: Pleroma.Caching)

View file

@ -0,0 +1,47 @@
# Pleroma: A lightweight social networking server
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
# 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

View file

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