diff --git a/lib/exirc/exirc.ex b/lib/exirc/exirc.ex index 7f1ff63..719c9ed 100644 --- a/lib/exirc/exirc.ex +++ b/lib/exirc/exirc.ex @@ -1,64 +1,68 @@ defmodule ExIrc do @moduledoc """ Supervises IRC client processes Usage: # Start the supervisor (started automatically when ExIrc is run as an application) ExIrc.start_link # Start a new IRC client {:ok, client} = ExIrc.start_client! # Connect to an IRC server ExIrc.Client.connect! client, "localhost", 6667 # Logon ExIrc.Client.logon client, "password", "nick", "user", "name" # Join a channel (password is optional) ExIrc.Client.join client, "#channel", "password" # Send a message ExIrc.Client.msg client, :privmsg, "#channel", "Hello world!" # Quit (message is optional) ExIrc.Client.quit client, "message" # Stop and close the client connection ExIrc.Client.stop! client """ use Supervisor + import Supervisor.Spec ############## # Public API ############## @doc """ Start the ExIrc supervisor. """ @spec start! :: {:ok, pid} | {:error, term} def start! do :supervisor.start_link({:local, :exirc}, __MODULE__, []) end @doc """ Start a new ExIrc client """ @spec start_client! :: {:ok, pid} | {:error, term} def start_client! do # Start the client worker - :supervisor.start_child(:exirc, worker(ExIrc.Client, [])) + :supervisor.start_child(:exirc, []) end ############## # Supervisor API ############## @spec init(any) :: {:ok, pid} | {:error, term} def init(_) do - supervise [], strategy: :one_for_one + children = [ + worker(ExIrc.Client, [], restart: :transient) + ] + supervise children, strategy: :simple_one_for_one end end diff --git a/test/client_test.exs b/test/client_test.exs new file mode 100644 index 0000000..6cff0b7 --- /dev/null +++ b/test/client_test.exs @@ -0,0 +1,11 @@ +defmodule ExIrc.ClientTest do + use ExUnit.Case + + + test "start multiple clients" do + {:ok, pid} = ExIrc.start_client! + {:ok, pid2} = ExIrc.start_client! + assert pid != pid2 + end + +end