From 8cc544e7eaf7e8e0627611e3d784dc88e08ab63c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Charvet=20=E9=BB=91=E7=93=9C?= Date: Wed, 26 Jun 2024 17:32:38 +0200 Subject: [PATCH 1/2] Clear cache users between tests This must be done for data cases as well as server cases. Because most of the querying logic rely under the hood on the caches, when someone creates a user, it won't be persisted to the DB after the test, but the cache will not be rolled back. --- lib/teiserver/libs/test_lib.ex | 7 ++++++- test/support/data_case.ex | 1 + 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/teiserver/libs/test_lib.ex b/lib/teiserver/libs/test_lib.ex index 3be42994b..9f9e3c951 100644 --- a/lib/teiserver/libs/test_lib.ex +++ b/lib/teiserver/libs/test_lib.ex @@ -660,7 +660,12 @@ defmodule Teiserver.TeiserverTestLib do :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 -> diff --git a/test/support/data_case.ex b/test/support/data_case.ex index 24240bdd7..3d45fb861 100644 --- a/test/support/data_case.ex +++ b/test/support/data_case.ex @@ -37,6 +37,7 @@ defmodule Teiserver.DataCase do setup tags do setup_sandbox(tags) + on_exit(&Teiserver.TeiserverTestLib.clear_all_con_caches/0) :ok end From dedf5a433b4249d69fcc867991c366452e0672c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Charvet=20=E9=BB=91=E7=93=9C?= Date: Mon, 1 Jul 2024 11:20:22 +0100 Subject: [PATCH 2/2] Add a simple test for clearing caches --- test/teiserver/caches_test.exs | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 test/teiserver/caches_test.exs diff --git a/test/teiserver/caches_test.exs b/test/teiserver/caches_test.exs new file mode 100644 index 000000000..73672671e --- /dev/null +++ b/test/teiserver/caches_test.exs @@ -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