I’m using the ideas from this post to setup my tests
This is my start_link
def start_link(init, opts \\ []) do
name = Keyword.get(opts, :name, __MODULE__)
GenServer.start_link(__MODULE__, init, name: name)
end
And I have this setup on my tests
setup do
child_spec = %{
id: :test_store,
start: {Mini.Store, :start_link, [%{}, [name: :test_store]]}
}
start_supervised!(child_spec)
:ok
end
This is the failing test,
test "insert multiple values under same key" do
Mini.Store.put(:key, %{my: :map}, name: :test_store)
Mini.Store.put(:key, %{other: :map}, name: :test_store)
assert Mini.Store.get(:key, name: :test_store) == [%{my: :map}, %{other: :map}]
end
When I run the assertion I only get the second map [%{other: :map}]
At first I thought it was a bug on my code, but when I try it on iex it works as expected.
This is the put
and get
implementations, its very ugly dont pay attention to that, I just want to point out that I get my server name from the opts
param.
def put(key, value, opts \\ []) do
sleep = Keyword.get(opts, :sleep, 0)
name = Keyword.get(opts, :name, __MODULE__)
existing = get(key)
concat = existing ++ [value]
new_list = Enum.reject(concat, fn x -> x == nil end)
dedup_list = Enum.dedup(new_list)
GenServer.cast(name, {:put, key, dedup_list, sleep})
end
def get(key, opts \\ []) do
timeout = Keyword.get(opts, :timeout, :infinity)
name = Keyword.get(opts, :name, __MODULE__)
try do
GenServer.call(name, {:get, key}, timeout)
catch
:exit, value ->
{:not_ready, value}
end
end
Again, on iex it works as expected
iex(1)> Mini.Store.get(:key)
[]
iex(2)> Mini.Store.put(:key, %{my: :map})
:ok
iex(3)> Mini.Store.put(:key, %{other: :map})
:ok
iex(4)> Mini.Store.get(:key)
[%{my: :map}, %{other: :map}]