File size: 4,204 Bytes
c9c49ab | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 | defmodule Plausible.Site.TrackerScriptCacheTest do
use Plausible.DataCase, async: false
@moduletag :ce_build_only
on_ce do
alias Plausible.Site.TrackerScriptConfiguration
alias Plausible.Site.TrackerScriptCache
describe "public cache interface" do
test "cache caches tracker scripts by id", %{test: test} do
{:ok, _} =
Supervisor.start_link(
[
{TrackerScriptCache,
[
cache_name: test,
child_id: :test_cache_tracker_script,
ets_options: [read_concurrency: true]
]}
],
strategy: :one_for_one,
name: :"cache_supervisor_#{test}"
)
site = new_site(domain: "site1.example.com")
config = create_config(site)
:ok = TrackerScriptCache.refresh_all(cache_name: test)
{:ok, _} = Plausible.Repo.delete(config)
assert TrackerScriptCache.size(test) == 1
assert result = TrackerScriptCache.get(config.id, force?: true, cache_name: test)
assert is_binary(result)
assert result =~ ~r/domain:\"#{site.domain}\"/
refute TrackerScriptCache.get("nonexistent", cache_name: test, force?: true)
end
test "refresh all and broadcast put works", %{test: test} do
{:ok, _} = start_test_cache(test)
site1 = new_site()
site2 = new_site()
configs = [create_config(site1), create_config(site2)]
cache_opts = [cache_name: test, force?: true]
for config <- configs do
assert TrackerScriptCache.get(config.id, cache_opts) == nil
end
assert :ok = TrackerScriptCache.refresh_all(cache_opts)
for config <- configs do
result = TrackerScriptCache.get(config.id, cache_opts)
assert is_binary(result)
assert result =~ ~r/domain:\"#{config.site.domain}\"/
assert result =~ ~r/fileDownloads:\!0/
end
[%TrackerScriptConfiguration{} = updated_config | _] = configs
TrackerScriptCache.broadcast_put(
updated_config.id,
TrackerScriptCache.cache_content(%TrackerScriptConfiguration{
updated_config
| file_downloads: false
}),
cache_opts
)
assert eventually(fn ->
result = TrackerScriptCache.get(updated_config.id, cache_opts)
{!(result =~ ~r/fileDownloads:\!0/), result}
end)
for config <- configs do
result = TrackerScriptCache.get(config.id, cache_opts)
on_ee do
assert result == true
else
assert result =~
~r/domain:\"#{config.site.domain}\"/
end
end
end
test "refreshes only recently added configurations", %{test: test} do
{:ok, _} = start_test_cache(test)
site1 = new_site()
site2 = new_site()
past_date = ~N[2021-01-01 00:00:00]
old_config = create_config(site1, inserted_at: past_date, updated_at: past_date)
new_config = create_config(site2)
cache_opts = [cache_name: test, force?: true]
assert TrackerScriptCache.get(old_config.id, cache_opts) == nil
assert TrackerScriptCache.get(new_config.id, cache_opts) == nil
assert :ok = TrackerScriptCache.refresh_updated_recently(cache_opts)
refute TrackerScriptCache.get(old_config.id, cache_opts)
assert TrackerScriptCache.get(new_config.id, cache_opts)
end
end
defp start_test_cache(cache_name) do
%{start: {m, f, a}} =
TrackerScriptCache.child_spec(
cache_name: cache_name,
ets_options: [read_concurrency: true]
)
apply(m, f, a)
end
defp create_config(site, opts \\ []) do
config = %TrackerScriptConfiguration{
site_id: site.id,
installation_type: :manual,
hash_based_routing: true,
outbound_links: true,
file_downloads: true,
form_submissions: true
}
config
|> Ecto.Changeset.change(opts)
|> Repo.insert!()
|> Repo.preload(:site)
end
end
end
|