diff options
author | José Valim <jose.valim@plataformatec.com.br> | 2012-12-06 10:02:39 +0100 |
---|---|---|
committer | José Valim <jose.valim@plataformatec.com.br> | 2012-12-06 10:02:39 +0100 |
commit | 5a1bc1e333ad00fb8871786e92f36e00457b872e (patch) | |
tree | 6be9f4aafa0ca8f9c4903e7736fecb3b72f417bd | |
parent | 5507b3b36a53a35ce02e14bbc1bc7f4512205e30 (diff) | |
download | elixir-5a1bc1e333ad00fb8871786e92f36e00457b872e.tar.gz |
Improve coverage for Supervisor.Behaviour
-rw-r--r-- | lib/elixir/test/elixir/gen_server/behaviour_test.exs | 44 | ||||
-rw-r--r-- | lib/elixir/test/elixir/supervisor/behaviour_test.exs | 91 |
2 files changed, 113 insertions, 22 deletions
diff --git a/lib/elixir/test/elixir/gen_server/behaviour_test.exs b/lib/elixir/test/elixir/gen_server/behaviour_test.exs index c9e764234..bb92a078e 100644 --- a/lib/elixir/test/elixir/gen_server/behaviour_test.exs +++ b/lib/elixir/test/elixir/gen_server/behaviour_test.exs @@ -1,36 +1,36 @@ Code.require_file "../../test_helper.exs", __FILE__ -defmodule GenServer.Sample do - use GenServer.Behaviour +defmodule GenServer.BehaviourTest do + use ExUnit.Case, async: true - # Callbacks + defmodule Sample do + use GenServer.Behaviour - def handle_call(:pop, _from, [h|t]) do - { :reply, h, t } - end + # Callbacks - def handle_call(:terminate, _from, config) do - { :stop, :normal, :ok, config } - end + def handle_call(:pop, _from, [h|t]) do + { :reply, h, t } + end - def handle_call(_request, _from, _config) do - super - end + def handle_call(:terminate, _from, config) do + { :stop, :normal, :ok, config } + end - def handle_cast({ :push, item }, config) do - { :noreply, [item|config] } - end + def handle_call(_request, _from, _config) do + super + end - def handle_cast(request, config) do - super(request, config) - end -end + def handle_cast({ :push, item }, config) do + { :noreply, [item|config] } + end -defmodule GenServer.BehaviourTest do - use ExUnit.Case, async: true + def handle_cast(request, config) do + super(request, config) + end + end test :using do - assert { :ok, pid } = :gen_server.start_link(GenServer.Sample, [:hello], []) + assert { :ok, pid } = :gen_server.start_link(Sample, [:hello], []) assert :gen_server.call(pid, :pop) == :hello assert :gen_server.cast(pid, { :push, :world }) == :ok assert :gen_server.call(pid, :pop) == :world diff --git a/lib/elixir/test/elixir/supervisor/behaviour_test.exs b/lib/elixir/test/elixir/supervisor/behaviour_test.exs new file mode 100644 index 000000000..c4b1b38ef --- /dev/null +++ b/lib/elixir/test/elixir/supervisor/behaviour_test.exs @@ -0,0 +1,91 @@ +Code.require_file "../../test_helper.exs", __FILE__ + +defmodule Supervisor.BehaviourTest do + use ExUnit.Case, async: true + + defmodule Server do + use GenServer.Behaviour + + def start_link do + :gen_server.start_link(__MODULE__, [], []) + end + end + + defmodule Sup do + use Supervisor.Behaviour + + def init(:ok) do + supervise [worker(Server, [])], strategy: :one_for_one + end + + def init(:noop) do + :ignore + end + end + + import Supervisor.Behaviour + + test :start_link do + assert :ignore = :supervisor.start_link(Sup, :noop) + assert { :ok, pid } = :supervisor.start_link(Sup, :ok) + assert is_pid(pid) + end + + test :worker do + assert worker(Foo, [1,2,3]) == { + Foo, + { Foo, :start_link, [1,2,3] }, + :permanent, + 5000, + :worker, + [Foo] + } + + opts = [id: :sample, function: :start, modules: :dynamic, + restart: :temporary, shutdown: :brutal_kill] + + assert worker(Foo, [1,2,3], opts) == { + :sample, + { Foo, :start, [1,2,3] }, + :temporary, + :brutal_kill, + :worker, + :dynamic + } + end + + test :supervisor do + assert supervisor(Foo, [1,2,3]) == { + Foo, + { Foo, :start_link, [1,2,3] }, + :permanent, + 5000, + :supervisor, + [Foo] + } + + opts = [id: :sample, function: :start, modules: :dynamic, + restart: :temporary, shutdown: :brutal_kill] + + assert supervisor(Foo, [1,2,3], opts) == { + :sample, + { Foo, :start, [1,2,3] }, + :temporary, + :brutal_kill, + :supervisor, + :dynamic + } + end + + test :supervise do + assert supervise([], strategy: :one_for_one) == { + :ok, { { :one_for_one, 5, 5 }, [] } + } + + opts = [strategy: :one_for_all, max_restarts: 1, max_seconds: 1] + + assert supervise([:sample], opts) == { + :ok, { { :one_for_all, 1, 1 }, [:sample] } + } + end +end |