add test stack
This commit is contained in:
parent
8293234a24
commit
e53f5ef04d
19
lib/stack.ex
Normal file
19
lib/stack.ex
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
defmodule MyStack do
|
||||||
|
use GenServer
|
||||||
|
|
||||||
|
def start_link(args) do
|
||||||
|
GenServer.start_link(__MODULE__, nil, name: args[:name])
|
||||||
|
end
|
||||||
|
|
||||||
|
def init(_) do
|
||||||
|
{:ok, []}
|
||||||
|
end
|
||||||
|
|
||||||
|
def handle_info({:push, el}, state) do
|
||||||
|
{:noreply, [el] ++ state}
|
||||||
|
end
|
||||||
|
|
||||||
|
def handle_call(:pop, _from, [head | tail]) do
|
||||||
|
{:reply, head, tail}
|
||||||
|
end
|
||||||
|
end
|
1
test/test_helper.exs
Normal file
1
test/test_helper.exs
Normal file
|
@ -0,0 +1 @@
|
||||||
|
ExUnit.start()
|
40
test/test_stack.exs
Normal file
40
test/test_stack.exs
Normal file
|
@ -0,0 +1,40 @@
|
||||||
|
defmodule MyStack.Test do
|
||||||
|
|
||||||
|
require MyStack
|
||||||
|
|
||||||
|
use ExUnit.Case, async: true
|
||||||
|
|
||||||
|
defmodule MySupervisor do
|
||||||
|
use Supervisor
|
||||||
|
|
||||||
|
def start_link do
|
||||||
|
Supervisor.start_link(__MODULE__, :ok)
|
||||||
|
end
|
||||||
|
|
||||||
|
def init(:ok) do
|
||||||
|
children = [
|
||||||
|
%{id: 1, start: {MyStack, :start_link, [[name: :my_server]]}},
|
||||||
|
%{id: 2, start: {MyStack, :start_link, [[name: :my_server2]]}}
|
||||||
|
]
|
||||||
|
|
||||||
|
Supervisor.init(children, strategy: :one_for_one)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
setup do
|
||||||
|
{:ok, supervisor: MySupervisor.start_link()}
|
||||||
|
end
|
||||||
|
|
||||||
|
test "server starts with an empty client list", %{supervisor: supervisor} do
|
||||||
|
send(:my_server, {:push, 2})
|
||||||
|
assert GenServer.call(:my_server, :pop) == 2
|
||||||
|
send(:my_server, {:push, 1})
|
||||||
|
send(:my_server, {:push, 4})
|
||||||
|
send(:my_server2, {:push, 3})
|
||||||
|
send(:my_server2, {:push, 8})
|
||||||
|
assert GenServer.call(:my_server2, :pop) == 8
|
||||||
|
assert GenServer.call(:my_server, :pop) == 4
|
||||||
|
assert GenServer.call(:my_server2, :pop) == 3
|
||||||
|
assert GenServer.call(:my_server, :pop) == 1
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in a new issue