From e61f75d86f18daac3051971de31f5354fc1eae47 Mon Sep 17 00:00:00 2001 From: Mike Cifelli Date: Sun, 14 Apr 2024 19:12:14 -0400 Subject: [PATCH] Remove unnecessary process complexity --- lib/chronoscope/nts.ex | 55 ++++++-------------------- test/chronoscope/nts_test.exs | 74 ----------------------------------- 2 files changed, 13 insertions(+), 116 deletions(-) diff --git a/lib/chronoscope/nts.ex b/lib/chronoscope/nts.ex index e370c18..0e84813 100644 --- a/lib/chronoscope/nts.ex +++ b/lib/chronoscope/nts.ex @@ -14,44 +14,17 @@ defmodule Chronoscope.NTS do end def list() do - call_all_clients(:list) - end - - def clear() do - :terminate - |> call_all_clients() - |> Enum.map(&wait_for_termination/1) - end - - defp wait_for_termination(pid) do - ref = Process.monitor(pid) - - receive do - {:DOWN, ^ref, _, _, _} -> {:ok, pid} - after - 1000 -> {:error, pid} - end - end - - defp call_all_clients(message) do NTS.DynamicSupervisor |> DynamicSupervisor.which_children() - |> Enum.map(fn {_, pid, _, _} -> GenServer.call(pid, message) end) + |> Enum.map(fn {_, pid, _, _} -> GenServer.call(pid, :list) end) end def remove(host, port) do - name = "#{host}:#{port}" + name = client_name(%{host: host, port: port}) case Registry.lookup(NTS.Registry, name) do [{pid, _}] -> - if Process.alive?(pid) do - pid - |> GenServer.call(:terminate) - |> wait_for_termination() - else - # Registry.unregister(NTS.Registry, name) - {:error, :notfound} - end + GenServer.call(pid, :terminate) [] -> {:error, :notfound} @@ -60,26 +33,24 @@ defmodule Chronoscope.NTS do @impl true def key_establishment(host, port) do - GenServer.call(client_pid(%{host: host, port: port}), :key_establishment) + %{host: host, port: port} + |> client_pid() + |> GenServer.call(:key_establishment) end defp client_pid(server) do - name = "#{server.host}:#{server.port}" + name = client_name(server) case Registry.lookup(NTS.Registry, name) do - [{pid, _}] -> - if Process.alive?(pid) do - pid - else - # Registry.unregister(NTS.Registry, name) - start_client(server, name) - end - - [] -> - start_client(server, name) + [{pid, _}] -> pid + [] -> start_client(server, name) end end + defp client_name(%{host: host, port: port}) do + "#{host}:#{port}" + 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}}}) diff --git a/test/chronoscope/nts_test.exs b/test/chronoscope/nts_test.exs index 48f283e..c8fbefc 100644 --- a/test/chronoscope/nts_test.exs +++ b/test/chronoscope/nts_test.exs @@ -1,28 +1,7 @@ defmodule Chronoscope.NTSTest do use Chronoscope.Case - alias Chronoscope.NTS.SSLMock - import Chronoscope.NTS - import Mox - - setup :verify_on_exit! - setup :set_mox_global - - defp expect_key_establishment(host, port) do - host_charlist = to_charlist(host) - - SSLMock - |> expect(:connect, fn ^host_charlist, ^port, _, _ -> {:ok, :socket} end) - |> expect(:send, fn :socket, _ -> send_ssl_response([]) end) - |> expect(:peercert, fn :socket -> {:ok, peercert()} end) - |> expect(:close, fn :socket -> :ok end) - end - - setup do - clear() - on_exit(fn -> clear() end) - end describe "Chronoscope.NTS.healthy?()" do test "is healthy" do @@ -34,64 +13,11 @@ defmodule Chronoscope.NTSTest do test "shows empty client list" do assert list() == [] end - - test "shows all clients" do - expect_key_establishment("localhost", 4444) - key_establishment("localhost", 4444) - - assert [%{host: "localhost", key_establishment_response: _, last_key_establishment: _, port: 4444}] = list() - end end describe "Chronoscope.NTS.remove()" do - test "removes a client" do - expect_key_establishment("localhost", 4444) - key_establishment("localhost", 4444) - - assert {:ok, _} = remove("localhost", 4444) - assert list() == [] - end - test "does nothing if the client doesn't exist" do - expect_key_establishment("localhost", 4444) - key_establishment("localhost", 4444) - 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 - test "creates and reuses a client" do - expect_key_establishment("localhost", 4444) - - assert {:ok, %{cert_expiration: _}} = key_establishment("localhost", 4444) - assert {:ok, %{cert_expiration: _}} = key_establishment("localhost", 4444) end end end