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 e19a2d0
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 116 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ Take a look at [error_message](https://github.com/MikaAK/elixir_error_message) f

### Notes/Gotchas
- In order for this caching to work, we cannot be using POST requests as specced out by GraphQL, not for queries at least, fortunately this doesn't actually matter since we can use any http method we want (there will be a limit to query size), in a production app you may be doing this already due to the caching you gain from CloudFlare
- Caches for gql are stored via the name parameter that comes back from the query (for now) so you must name your queries to get caching
- Caches are stored via a MD5 hashed key that correlates to your query in GraphQL, or in REST your url path + query parameters
- Absinthe and ConCache are optional dependencies, if you don't have them you won't have access to `RequestCache.Middleware` or `RequestCache.ConCacheStore`
- If no ConCache is found, you must set `config :request_cache_module` to something else

Expand Down
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 e19a2d0

Please sign in to comment.