diff --git a/lib/chronoscope/nts.ex b/lib/chronoscope/nts.ex index 8e8c013..e370c18 100644 --- a/lib/chronoscope/nts.ex +++ b/lib/chronoscope/nts.ex @@ -44,9 +44,14 @@ defmodule Chronoscope.NTS do case Registry.lookup(NTS.Registry, name) do [{pid, _}] -> - pid - |> GenServer.call(:terminate) - |> wait_for_termination() + if Process.alive?(pid) do + pid + |> GenServer.call(:terminate) + |> wait_for_termination() + else + # Registry.unregister(NTS.Registry, name) + {:error, :notfound} + end [] -> {:error, :notfound} @@ -55,20 +60,29 @@ defmodule Chronoscope.NTS do @impl true def key_establishment(host, port) do - GenServer.call(client_pid(host, port), :key_establishment) + GenServer.call(client_pid(%{host: host, port: port}), :key_establishment) end - defp client_pid(host, port) do - name = "#{host}:#{port}" + defp client_pid(server) do + name = "#{server.host}:#{server.port}" case Registry.lookup(NTS.Registry, name) do [{pid, _}] -> - pid + if Process.alive?(pid) do + pid + else + # Registry.unregister(NTS.Registry, name) + start_client(server, name) + end [] -> - NTS.DynamicSupervisor - |> DynamicSupervisor.start_child({NTS.Client, host: host, port: port, name: {:via, Registry, {NTS.Registry, name}}}) - |> then(fn {:ok, pid} -> pid end) + start_client(server, name) end end + + defp start_client(%{host: host, port: port}, name) do + NTS.DynamicSupervisor + |> DynamicSupervisor.start_child({NTS.Client, host: host, port: port, name: {:via, Registry, {NTS.Registry, name}}}) + |> then(fn {:ok, pid} -> pid end) + end end diff --git a/test/chronoscope/nts/client_test.exs b/test/chronoscope/nts/client_test.exs index e05340e..55a6e00 100644 --- a/test/chronoscope/nts/client_test.exs +++ b/test/chronoscope/nts/client_test.exs @@ -31,6 +31,10 @@ defmodule Chronoscope.NTS.ClientTest do end describe "Chronoscope.NTS.Client.handle_call()" do + test ":terminate" do + assert handle_call(:terminate, nil, %{state: true}) == {:stop, :normal, self(), %{state: true}} + end + test ":list" do assert handle_call(:list, nil, %{state: true}) == {:reply, %{state: true}, %{state: true}} end diff --git a/test/chronoscope/nts_test.exs b/test/chronoscope/nts_test.exs index 9614077..48f283e 100644 --- a/test/chronoscope/nts_test.exs +++ b/test/chronoscope/nts_test.exs @@ -59,6 +59,31 @@ defmodule Chronoscope.NTSTest do assert remove("localhost", 1111) == {:error, :notfound} assert [%{host: "localhost", key_establishment_response: _, last_key_establishment: _, port: 4444}] = list() end + + test "remove several clients" do + expect_key_establishment("localhost", 4444) + expect_key_establishment("localhost", 2222) + expect_key_establishment("localhost", 4444) + expect_key_establishment("localhost", 4444) + expect_key_establishment("localhost", 4444) + expect_key_establishment("localhost", 5555) + + key_establishment("localhost", 4444) + key_establishment("localhost", 2222) + remove("localhost", 4444) + key_establishment("localhost", 4444) + remove("localhost", 4444) + key_establishment("localhost", 4444) + key_establishment("localhost", 4444) + remove("localhost", 4444) + remove("localhost", 4444) + key_establishment("localhost", 4444) + remove("localhost", 4444) + key_establishment("localhost", 5555) + remove("localhost", 5555) + remove("localhost", 2222) + assert list() == [] + end end describe "Chronoscope.NTS.key_establishment()" do diff --git a/test/support/ssl.ex b/test/support/ssl.ex deleted file mode 100644 index e69de29..0000000