Skip to content

Commit

Permalink
Add more startup handling
Browse files Browse the repository at this point in the history
  • Loading branch information
whitfin committed Oct 14, 2024
1 parent 71f134b commit bad30bd
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
7 changes: 5 additions & 2 deletions lib/cachex.ex
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,10 @@ defmodule Cachex do
#
# Without this, Cachex is not compatible per Elixir's documentation.
@spec start_link(atom | Keyword.t()) :: {atom, pid} | {:error, :invalid_name}
def start_link([name | options]) when is_atom(name) and is_list(options),
def start_link([name]) when is_atom(name),
do: start_link(name)

def start_link([name, options]) when is_atom(name) and is_list(options),
do: start_link(name, options)

def start_link(options) when is_list(options) do
Expand All @@ -300,7 +303,7 @@ defmodule Cachex do
end

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

@doc """
Creates a new Cachex cache service tree.
Expand Down
28 changes: 22 additions & 6 deletions test/cachex_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -225,14 +225,30 @@ defmodule CachexTest do
# 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`
# check the default ways of starting a cache
{:ok, _pid} = Cachex.start_link(:child_spec1)
{:ok, _pid} = Cachex.start_link(name: :child_spec2)
{:ok, _pid} = Cachex.start_link(:child_spec2, transactions: true)

# check the format {Cachex, [:child_spec3]}
{:ok, _pid} = Cachex.start_link([:child_spec3])

# verify that both are at least alive in some way
{:ok, _cache1} = Cachex.inspect(:child_spec1, :cache)
{:ok, _cache2} = Cachex.inspect(:child_spec2, :cache)
{:ok, _cache2} = Cachex.inspect(:child_spec3, :cache)
# check the format {Cachex, [:child_spec4, []]}
{:ok, _pid} = Cachex.start_link([:child_spec4, [transactions: true]])

# check the format {Cachex, [name: :child_spec5]}
{:ok, _pid} = Cachex.start_link(name: :child_spec5)

# check the format {Cachex, [name: :child_spec6, transactions: true]}
{:ok, _pid} = Cachex.start_link(name: :child_spec6, transactions: true)

# verify the caches that are created only from a name
{:ok, cache()} = Cachex.inspect(:child_spec1, :cache)
{:ok, cache()} = Cachex.inspect(:child_spec3, :cache)
{:ok, cache()} = Cachex.inspect(:child_spec5, :cache)

# double check those with options provided by double checking the value
{:ok, cache(transactions: true)} = Cachex.inspect(:child_spec2, :cache)
{:ok, cache(transactions: true)} = Cachex.inspect(:child_spec4, :cache)
{:ok, cache(transactions: true)} = Cachex.inspect(:child_spec6, :cache)
end
end

0 comments on commit bad30bd

Please sign in to comment.