Remove unnecessary process complexity
This commit is contained in:
parent
b8169a592d
commit
e61f75d86f
|
@ -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,24 +33,22 @@ 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)
|
||||
[{pid, _}] -> pid
|
||||
[] -> start_client(server, name)
|
||||
end
|
||||
end
|
||||
|
||||
[] ->
|
||||
start_client(server, name)
|
||||
end
|
||||
defp client_name(%{host: host, port: port}) do
|
||||
"#{host}:#{port}"
|
||||
end
|
||||
|
||||
defp start_client(%{host: host, port: port}, name) do
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue