Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clear cache users between tests #342

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion lib/teiserver/libs/test_lib.ex
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@
# This is because upon disconnecting, the server does a bunch of DB call for logging
# and telemetry. The disconnection happen when the tcp socket is closed, and by
# that time, the test has ended and the SQL sandbox closed.
ExUnit.Callbacks.on_exit(fn -> Teiserver.Client.disconnect(user.id) end)

Check warning on line 124 in lib/teiserver/libs/test_lib.ex

View workflow job for this annotation

GitHub Actions / Dialyzer

unknown_function

Function ExUnit.Callbacks.on_exit/1 does not exist.
%{socket: socket, user: user, pid: pid}
end

Expand Down Expand Up @@ -660,7 +660,12 @@
:telemetry_simple_client_event_types_cache,
:telemetry_simple_lobby_event_types_cache,
:telemetry_simple_match_event_types_cache,
:telemetry_simple_server_event_types_cache
:telemetry_simple_server_event_types_cache,
:users,
:users_lookup_id_with_discord,
:users_lookup_id_with_email,
:users_lookup_id_with_name,
:users_lookup_name_with_id
]

Enum.each(cache_list, fn cache ->
Expand Down
1 change: 1 addition & 0 deletions test/support/data_case.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule Teiserver.DataCase do

Check warning on line 1 in test/support/data_case.ex

View workflow job for this annotation

GitHub Actions / Dialyzer

unknown_function

Function ExUnit.Callbacks.__merge__/3 does not exist.
@moduledoc """
This module defines the setup for tests requiring
access to the application's data layer.
Expand All @@ -16,7 +16,7 @@

use ExUnit.CaseTemplate

using do

Check warning on line 19 in test/support/data_case.ex

View workflow job for this annotation

GitHub Actions / Dialyzer

unknown_function

Function ExUnit.CaseTemplate.__proxy__/2 does not exist.
quote do
alias Teiserver.Repo

Expand All @@ -32,11 +32,12 @@
"""
def setup_sandbox(tags) do
pid = Ecto.Adapters.SQL.Sandbox.start_owner!(Teiserver.Repo, shared: not tags[:async])
on_exit(fn -> Ecto.Adapters.SQL.Sandbox.stop_owner(pid) end)

Check warning on line 35 in test/support/data_case.ex

View workflow job for this annotation

GitHub Actions / Dialyzer

unknown_function

Function ExUnit.Callbacks.on_exit/1 does not exist.
end

setup tags do
setup_sandbox(tags)
on_exit(&Teiserver.TeiserverTestLib.clear_all_con_caches/0)

Check warning on line 40 in test/support/data_case.ex

View workflow job for this annotation

GitHub Actions / Dialyzer

unknown_function

Function ExUnit.Callbacks.on_exit/1 does not exist.
:ok
end

Expand Down
25 changes: 25 additions & 0 deletions test/teiserver/caches_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
defmodule Teiserver.CachesTest do
use Teiserver.DataCase, async: false

# This module is merely here to check that
# Teiserver.TeiserverTestLib.clear_all_con_caches does indeed clear the advertised
# cache across tests.
# because all queries rely heavily on caches, it's important to clear them between
# tests so as not to pollute other tests

test "Clear user caches 1" do
name = "ClearDbEachTestUser"
assert is_nil(Teiserver.CacheUser.get_user_by_name(name))
user = Teiserver.TeiserverTestLib.new_user(name)
result = Teiserver.CacheUser.get_user_by_id(user.id)
assert result[:name] == name
end

test "Clear user caches 2" do
name = "ClearDbEachTestUser"
assert is_nil(Teiserver.CacheUser.get_user_by_name(name))
user = Teiserver.TeiserverTestLib.new_user(name)
result = Teiserver.CacheUser.get_user_by_id(user.id)
assert result[:name] == name
end
end
Loading