Skip to content

Commit

Permalink
fix: format unformatted code and fix telemetry tests causing log mess…
Browse files Browse the repository at this point in the history
…ages
  • Loading branch information
MikaAK committed Sep 16, 2023
1 parent 9937467 commit 507d0fd
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 115 deletions.
4 changes: 1 addition & 3 deletions lib/request_cache/middleware.ex
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ if absinthe_loaded? do
resolution = resolve_resolver_func_middleware(resolution, opts)

if resolution.context[RequestCache.Config.conn_private_key()][:enabled?] do
if RequestCache.Config.verbose?() do
Util.verbose_log("[RequestCache.Middleware] Enabling cache for resolution")
end
Util.verbose_log("[RequestCache.Middleware] Enabling cache for resolution")

%{resolution |
value: resolution.value || opts[:value],
Expand Down
10 changes: 1 addition & 9 deletions lib/request_cache/util.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,6 @@ defmodule RequestCache.Util do

@whitelisted_modules [DateTime, NaiveDateTime, Date, Time, File.Stat, MapSet, Regex, URI, Version]


# def parse_gql_name(query_string) do
# case Regex.run(~r/^(?:query) ([^\({]+(?=\(|{))/, query_string, capture: :all_but_first) do
# [query_name] -> String.trim(query_name)
# _ -> nil
# end
# end

def create_key(url_path, query_string) do
"#{url_path}:#{hash_string(query_string)}"
end
Expand All @@ -22,7 +14,7 @@ defmodule RequestCache.Util do
end

def log_cache_disabled_message do
Logger.debug("RequestCache requested but hasn't been enabled, ensure query has a name and the RequestCache.Plug is part of your Endpoint")
Logger.warning("RequestCache requested but hasn't been enabled, ensure RequestCache.Plug is part of your endpoint.ex file")
end

def verbose_log(message) do
Expand Down
7 changes: 7 additions & 0 deletions test/request_cache/metrics_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ defmodule RequestCache.TelemetryMetricsTest do
%{conn: conn}
end

@tag capture_log: true
test "cache miss", %{parent_pid: parent_pid, test: test, conn: conn} do
start_telemetry_listener(parent_pid, test, @expected_graphql_cache_miss_event_name)

Expand All @@ -38,6 +39,7 @@ defmodule RequestCache.TelemetryMetricsTest do
@expected_measurements, _metadata}
end

@tag capture_log: true
test "cache miss with labels", %{parent_pid: parent_pid, test: test, conn: conn} do
start_telemetry_listener(parent_pid, test, @expected_graphql_cache_miss_event_name)

Expand Down Expand Up @@ -67,6 +69,7 @@ defmodule RequestCache.TelemetryMetricsTest do
%{conn: conn}
end

@tag capture_log: true
test "cache hit", %{parent_pid: parent_pid, test: test, conn: conn} do
start_telemetry_listener(parent_pid, test, @expected_graphql_cache_hit_event_name)

Expand All @@ -76,6 +79,7 @@ defmodule RequestCache.TelemetryMetricsTest do
@expected_measurements, _metadata}
end

@tag capture_log: true
test "cache hit with labels", %{parent_pid: parent_pid, test: test, conn: conn} do
start_telemetry_listener(parent_pid, test, @expected_graphql_cache_hit_event_name)

Expand All @@ -91,6 +95,7 @@ defmodule RequestCache.TelemetryMetricsTest do
end

describe "REST RequestCache.Plug.call/2" do
@tag capture_log: true
test "cache miss", %{parent_pid: parent_pid, test: test} do
start_telemetry_listener(parent_pid, test, @expected_rest_cache_miss_event_name)

Expand All @@ -102,6 +107,7 @@ defmodule RequestCache.TelemetryMetricsTest do
@expected_measurements, _metadata}
end

@tag capture_log: true
test "cache hit", %{parent_pid: parent_pid, test: test} do
RequestCache.ConCacheStore.put(
nil,
Expand All @@ -122,6 +128,7 @@ defmodule RequestCache.TelemetryMetricsTest do
end

describe "metrics/0" do
@tag capture_log: true
test "metric definitions are correct" do
assert [
%Telemetry.Metrics.Counter{
Expand Down
36 changes: 18 additions & 18 deletions test/request_cache_absinthe_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,10 @@ defmodule RequestCacheAbsintheTest do

assert_raise Plug.Conn.WrapperError, fn ->
conn = :get
|> conn(graphql_url(@uncached_query))
|> RequestCache.Support.Utils.ensure_default_opts()
|> Absinthe.Plug.put_options(context: %{call_pid: pid})
|> Router.call([])
|> conn(graphql_url(@uncached_query))
|> RequestCache.Support.Utils.ensure_default_opts()
|> Absinthe.Plug.put_options(context: %{call_pid: pid})
|> Router.call([])

assert [] === get_resp_header(conn, RequestCache.Plug.request_cache_header())
end
Expand All @@ -144,10 +144,10 @@ defmodule RequestCacheAbsintheTest do

assert_raise Plug.Conn.WrapperError, fn ->
conn = :get
|> conn(graphql_url(@uncached_error_query))
|> RequestCache.Support.Utils.ensure_default_opts()
|> Absinthe.Plug.put_options(context: %{call_pid: pid})
|> Router.call([])
|> conn(graphql_url(@uncached_error_query))
|> RequestCache.Support.Utils.ensure_default_opts()
|> Absinthe.Plug.put_options(context: %{call_pid: pid})
|> Router.call([])

assert [] === get_resp_header(conn, RequestCache.Plug.request_cache_header())
end
Expand All @@ -162,11 +162,11 @@ defmodule RequestCacheAbsintheTest do
|> Router.call([])

assert ["HIT"] = :get
|> conn(graphql_url(@cached_all_error_query))
|> RequestCache.Support.Utils.ensure_default_opts(request: [cached_errors: :all])
|> Absinthe.Plug.put_options(context: %{call_pid: pid})
|> Router.call([])
|> get_resp_header(RequestCache.Plug.request_cache_header())
|> conn(graphql_url(@cached_all_error_query))
|> RequestCache.Support.Utils.ensure_default_opts(request: [cached_errors: :all])
|> Absinthe.Plug.put_options(context: %{call_pid: pid})
|> Router.call([])
|> get_resp_header(RequestCache.Plug.request_cache_header())
end

@tag capture_log: true
Expand All @@ -178,11 +178,11 @@ defmodule RequestCacheAbsintheTest do
|> Router.call([])

assert ["HIT"] = :get
|> conn(graphql_url(@cached_not_found_error_query))
|> RequestCache.Support.Utils.ensure_default_opts(request: [cached_errors: [:not_found]])
|> Absinthe.Plug.put_options(context: %{call_pid: pid})
|> Router.call([])
|> get_resp_header(RequestCache.Plug.request_cache_header())
|> conn(graphql_url(@cached_not_found_error_query))
|> RequestCache.Support.Utils.ensure_default_opts(request: [cached_errors: [:not_found]])
|> Absinthe.Plug.put_options(context: %{call_pid: pid})
|> Router.call([])
|> get_resp_header(RequestCache.Plug.request_cache_header())
end

@tag capture_log: true
Expand Down
160 changes: 75 additions & 85 deletions test/request_cache_plug_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -171,143 +171,133 @@ defmodule RequestCachePlugTest do
caller_pid: pid
} do
route = "/my_route/html"
assert %Plug.Conn{resp_headers: uncached_headers} =
:get
|> conn(route)
|> RequestCache.Support.Utils.ensure_default_opts()
|> put_private(:call_pid, pid)
|> Router.call([])
assert %Plug.Conn{resp_headers: uncached_headers} = :get
|> conn(route)
|> RequestCache.Support.Utils.ensure_default_opts()
|> put_private(:call_pid, pid)
|> Router.call([])

assert uncached_headers === [
{"cache-control", "max-age=0, private, must-revalidate"}
]

assert %Plug.Conn{resp_headers: resp_headers} =
:get
|> conn(route)
|> RequestCache.Support.Utils.ensure_default_opts()
|> put_private(:call_pid, pid)
|> Router.call([])
assert %Plug.Conn{resp_headers: resp_headers} = :get
|> conn(route)
|> RequestCache.Support.Utils.ensure_default_opts()
|> put_private(:call_pid, pid)
|> Router.call([])

assert resp_headers === [
{"cache-control", "max-age=0, private, must-revalidate"},
{"rc-cache-status", "HIT"},
{"content-type", "application/json; charset=utf-8"}
]
{"cache-control", "max-age=0, private, must-revalidate"},
{"rc-cache-status", "HIT"},
{"content-type", "application/json; charset=utf-8"}
]
end

@tag capture_log: true
test "allows for for custom content-type header and returns it when served from the cache", %{
caller_pid: pid
} do
route = "/my_route/cache"
assert %Plug.Conn{resp_headers: uncached_headers} =
:get
|> conn(route)
|> RequestCache.Support.Utils.ensure_default_opts()
|> put_private(:call_pid, pid)
|> Router.call([])
assert %Plug.Conn{resp_headers: uncached_headers} = :get
|> conn(route)
|> RequestCache.Support.Utils.ensure_default_opts()
|> put_private(:call_pid, pid)
|> Router.call([])

assert uncached_headers === [
{"cache-control", "max-age=0, private, must-revalidate"}
{"cache-control", "max-age=0, private, must-revalidate"}
]

assert %Plug.Conn{resp_headers: resp_headers} =
:get
|> conn(route)
|> RequestCache.Support.Utils.ensure_default_opts()
|> put_private(:call_pid, pid)
|> put_resp_content_type("text/html")
|> Router.call([])
assert %Plug.Conn{resp_headers: resp_headers} = :get
|> conn(route)
|> RequestCache.Support.Utils.ensure_default_opts()
|> put_private(:call_pid, pid)
|> put_resp_content_type("text/html")
|> Router.call([])

assert resp_headers === [
{"cache-control", "max-age=0, private, must-revalidate"},
{"content-type", "text/html; charset=utf-8"},
{"rc-cache-status", "HIT"}
]
{"cache-control", "max-age=0, private, must-revalidate"},
{"content-type", "text/html; charset=utf-8"},
{"rc-cache-status", "HIT"}
]
end

test "doesn't cache errors if error caching not enabled", %{caller_pid: pid} do
route = "/error-route/no-error-cache-enabled"

assert %Plug.Conn{resp_headers: uncached_headers} =
:get
|> conn(route)
|> RequestCache.Support.Utils.ensure_default_opts(request: [cached_errors: []])
|> put_private(:call_pid, pid)
|> Router.call([])
assert %Plug.Conn{resp_headers: uncached_headers} = :get
|> conn(route)
|> RequestCache.Support.Utils.ensure_default_opts(request: [cached_errors: []])
|> put_private(:call_pid, pid)
|> Router.call([])

assert uncached_headers === [
{"cache-control", "max-age=0, private, must-revalidate"}
{"cache-control", "max-age=0, private, must-revalidate"}
]

assert %Plug.Conn{resp_headers: expected_uncached_headers} =
:get
|> conn(route)
|> RequestCache.Support.Utils.ensure_default_opts()
|> put_private(:call_pid, pid)
|> Router.call([])
assert %Plug.Conn{resp_headers: expected_uncached_headers} = :get
|> conn(route)
|> RequestCache.Support.Utils.ensure_default_opts()
|> put_private(:call_pid, pid)
|> Router.call([])

assert expected_uncached_headers === [
{"cache-control", "max-age=0, private, must-revalidate"}
]
{"cache-control", "max-age=0, private, must-revalidate"}
]
end

test "caches errors if error codes supplied and is error", %{caller_pid: pid} do
route = "/error-route/caching-errors-enabled"

assert %Plug.Conn{resp_headers: uncached_headers} =
:get
|> conn(route)
|> RequestCache.Support.Utils.ensure_default_opts(request: [cached_errors: [:not_found]])
|> put_private(:call_pid, pid)
|> Router.call([])
assert %Plug.Conn{resp_headers: uncached_headers} = :get
|> conn(route)
|> RequestCache.Support.Utils.ensure_default_opts(request: [cached_errors: [:not_found]])
|> put_private(:call_pid, pid)
|> Router.call([])

assert uncached_headers === [
{"cache-control", "max-age=0, private, must-revalidate"}
{"cache-control", "max-age=0, private, must-revalidate"}
]

assert %Plug.Conn{resp_headers: expected_cached_headers} =
:get
|> conn(route)
|> RequestCache.Support.Utils.ensure_default_opts(request: [cached_errors: [:not_found]])
|> put_private(:call_pid, pid)
|> put_resp_content_type("text/html")
|> Router.call([])
assert %Plug.Conn{resp_headers: expected_cached_headers} = :get
|> conn(route)
|> RequestCache.Support.Utils.ensure_default_opts(request: [cached_errors: [:not_found]])
|> put_private(:call_pid, pid)
|> put_resp_content_type("text/html")
|> Router.call([])

assert expected_cached_headers === [
{"cache-control", "max-age=0, private, must-revalidate"},
{"content-type", "text/html; charset=utf-8"},
{"rc-cache-status", "HIT"}
]
{"cache-control", "max-age=0, private, must-revalidate"},
{"content-type", "text/html; charset=utf-8"},
{"rc-cache-status", "HIT"}
]
end

test "caches errors if error caching enabled", %{caller_pid: pid} do
route = "/error-route/all-errors-enabled"

assert %Plug.Conn{resp_headers: uncached_headers} =
:get
|> conn(route)
|> RequestCache.Support.Utils.ensure_default_opts(request: [cached_errors: :all])
|> put_private(:call_pid, pid)
|> Router.call([])
assert %Plug.Conn{resp_headers: uncached_headers} = :get
|> conn(route)
|> RequestCache.Support.Utils.ensure_default_opts(request: [cached_errors: :all])
|> put_private(:call_pid, pid)
|> Router.call([])

assert uncached_headers === [
{"cache-control", "max-age=0, private, must-revalidate"}
{"cache-control", "max-age=0, private, must-revalidate"}
]

assert %Plug.Conn{resp_headers: expected_cached_headers} =
:get
|> conn(route)
|> RequestCache.Support.Utils.ensure_default_opts(request: [cached_errors: :all])
|> put_private(:call_pid, pid)
|> put_resp_content_type("text/html")
|> Router.call([])
assert %Plug.Conn{resp_headers: expected_cached_headers} = :get
|> conn(route)
|> RequestCache.Support.Utils.ensure_default_opts(request: [cached_errors: :all])
|> put_private(:call_pid, pid)
|> put_resp_content_type("text/html")
|> Router.call([])

assert expected_cached_headers === [
{"cache-control", "max-age=0, private, must-revalidate"},
{"content-type", "text/html; charset=utf-8"},
{"rc-cache-status", "HIT"}
]
{"cache-control", "max-age=0, private, must-revalidate"},
{"content-type", "text/html; charset=utf-8"},
{"rc-cache-status", "HIT"}
]
end
end

0 comments on commit 507d0fd

Please sign in to comment.