Skip to content

Commit

Permalink
Replace old signatures for Supervisor.child_spec/1
Browse files Browse the repository at this point in the history
  • Loading branch information
whitfin committed Sep 25, 2024
1 parent b976fb3 commit 7aa2f5d
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 6 deletions.
19 changes: 15 additions & 4 deletions lib/cachex.ex
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ defmodule Cachex do
"""
@spec start_link(atom, Keyword.t()) :: {atom, pid}
def start_link(name, options \\ []) when is_atom(name) and is_list(options) do
def start_link(name, options) when is_atom(name) and is_list(options) do
with {:ok, true} <- ensure_started(),
{:ok, true} <- ensure_unused(name),
{:ok, cache} <- Options.parse(name, options),
Expand All @@ -281,6 +281,20 @@ defmodule Cachex do
end
end

# Cachex.start_link/1 exists for child specifications
def start_link(options) when is_list(options) do
case Keyword.fetch(options, :name) do
{:ok, name} ->
start_link(name, options)

_invalid ->
error(:invalid_name)
end
end

def start_link(name) when is_atom(name),
do: start_link(name: name)

@doc """
Creates a new Cachex cache service tree.
Expand Down Expand Up @@ -1441,7 +1455,4 @@ defmodule Cachex do

defp unwrap_unsafe({_state, value}),
do: value

defp unwrap_unsafe({_state, value, _opts}),
do: value
end
4 changes: 4 additions & 0 deletions lib/cachex/error.ex
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ defmodule Cachex.Error do
:invalid_hook,
:invalid_limit,
:invalid_match,
:invalid_name,
:invalid_option,
:invalid_pairs,
:invalid_router,
Expand Down Expand Up @@ -89,6 +90,9 @@ defmodule Cachex.Error do
def long_form(:invalid_match),
do: "Invalid match specification provided"

def long_form(:invalid_name),
do: "Invalid cache name provided"

def long_form(:invalid_option),
do: "Invalid option syntax provided"

Expand Down
4 changes: 2 additions & 2 deletions lib/cachex/query.ex
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ defmodule Cachex.Query do
This function accepts a subfilter to join to create more complex filters.
"""
@spec expired(filter :: boolean() | tuple()) :: tuple
@spec expired(filter :: boolean | tuple | nil) :: tuple
def expired(filter \\ nil),
do: wrap_condition(filter, {:not, unexpired()})

Expand All @@ -89,7 +89,7 @@ defmodule Cachex.Query do
This function accepts a subfilter to join to create more complex filters.
"""
@spec unexpired(filter :: boolean() | tuple()) :: tuple
@spec unexpired(filter :: boolean | tuple | nil) :: tuple
def unexpired(filter \\ nil),
do:
wrap_condition(
Expand Down
1 change: 1 addition & 0 deletions test/cachex/error_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ defmodule Cachex.ErrorTest do
invalid_hook: "Invalid hook definition provided",
invalid_limit: "Invalid limit fields provided",
invalid_match: "Invalid match specification provided",
invalid_name: "Invalid cache name provided",
invalid_option: "Invalid option syntax provided",
invalid_pairs: "Invalid insertion pairs provided",
invalid_router: "Invalid router definition provided",
Expand Down
27 changes: 27 additions & 0 deletions test/cachex_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,17 @@ defmodule CachexTest do
assert(reason == :not_started)
end

# This test does a simple check that a cache must be started with a valid atom
# cache name, otherwise an error is raised (an ArgumentError). The error should
# be a shorthand atom which can be used to debug what the issue was.
test "cache start with invalid cache name" do
# try to start the cache with an invalid name
{:error, reason} = Cachex.start_link([])

# we should've received an atom warning
assert reason == :invalid_name
end

# This test ensures that we handle option parsing errors gracefully. If anything
# goes wrong when parsing options, we exit early before starting the cache to
# avoid bloating the Supervision tree.
Expand Down Expand Up @@ -205,5 +216,21 @@ defmodule CachexTest do
raise RuntimeError, message: "Which old witch? The wicked witch!"
end)
end)

# verify both unpacking pac
nil = Cachex.get!(cache, "key")
nil = Cachex.fetch!(cache, "key", fn _ -> nil end)
end

# This test validates `Cachex.start_link/1` mtaintains compatibility
# with `Supervisor.child_spec/2` and handles the name as an option.
test "cache start with child_spec/1 compatibility" do
# create two caches using `Cachex.start_link/1`
{:ok, _pid} = Cachex.start_link(:child_spec1)
{:ok, _pid} = Cachex.start_link(name: :child_spec2)

# verify that both are at least alive in some way
{:ok, _cache1} = Cachex.inspect(:child_spec1, :cache)
{:ok, _cache2} = Cachex.inspect(:child_spec2, :cache)
end
end

0 comments on commit 7aa2f5d

Please sign in to comment.