Remove unnecessary process complexity
This commit is contained in:
parent
b8169a592d
commit
e61f75d86f
|
@ -14,44 +14,17 @@ defmodule Chronoscope.NTS do
|
||||||
end
|
end
|
||||||
|
|
||||||
def list() do
|
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
|
NTS.DynamicSupervisor
|
||||||
|> DynamicSupervisor.which_children()
|
|> DynamicSupervisor.which_children()
|
||||||
|> Enum.map(fn {_, pid, _, _} -> GenServer.call(pid, message) end)
|
|> Enum.map(fn {_, pid, _, _} -> GenServer.call(pid, :list) end)
|
||||||
end
|
end
|
||||||
|
|
||||||
def remove(host, port) do
|
def remove(host, port) do
|
||||||
name = "#{host}:#{port}"
|
name = client_name(%{host: host, port: port})
|
||||||
|
|
||||||
case Registry.lookup(NTS.Registry, name) do
|
case Registry.lookup(NTS.Registry, name) do
|
||||||
[{pid, _}] ->
|
[{pid, _}] ->
|
||||||
if Process.alive?(pid) do
|
GenServer.call(pid, :terminate)
|
||||||
pid
|
|
||||||
|> GenServer.call(:terminate)
|
|
||||||
|> wait_for_termination()
|
|
||||||
else
|
|
||||||
# Registry.unregister(NTS.Registry, name)
|
|
||||||
{:error, :notfound}
|
|
||||||
end
|
|
||||||
|
|
||||||
[] ->
|
[] ->
|
||||||
{:error, :notfound}
|
{:error, :notfound}
|
||||||
|
@ -60,26 +33,24 @@ defmodule Chronoscope.NTS do
|
||||||
|
|
||||||
@impl true
|
@impl true
|
||||||
def key_establishment(host, port) do
|
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
|
end
|
||||||
|
|
||||||
defp client_pid(server) do
|
defp client_pid(server) do
|
||||||
name = "#{server.host}:#{server.port}"
|
name = client_name(server)
|
||||||
|
|
||||||
case Registry.lookup(NTS.Registry, name) do
|
case Registry.lookup(NTS.Registry, name) do
|
||||||
[{pid, _}] ->
|
[{pid, _}] -> pid
|
||||||
if Process.alive?(pid) do
|
[] -> start_client(server, name)
|
||||||
pid
|
|
||||||
else
|
|
||||||
# Registry.unregister(NTS.Registry, name)
|
|
||||||
start_client(server, name)
|
|
||||||
end
|
|
||||||
|
|
||||||
[] ->
|
|
||||||
start_client(server, name)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
defp client_name(%{host: host, port: port}) do
|
||||||
|
"#{host}:#{port}"
|
||||||
|
end
|
||||||
|
|
||||||
defp start_client(%{host: host, port: port}, name) do
|
defp start_client(%{host: host, port: port}, name) do
|
||||||
NTS.DynamicSupervisor
|
NTS.DynamicSupervisor
|
||||||
|> DynamicSupervisor.start_child({NTS.Client, host: host, port: port, name: {:via, Registry, {NTS.Registry, name}}})
|
|> DynamicSupervisor.start_child({NTS.Client, host: host, port: port, name: {:via, Registry, {NTS.Registry, name}}})
|
||||||
|
|
|
@ -1,28 +1,7 @@
|
||||||
defmodule Chronoscope.NTSTest do
|
defmodule Chronoscope.NTSTest do
|
||||||
use Chronoscope.Case
|
use Chronoscope.Case
|
||||||
|
|
||||||
alias Chronoscope.NTS.SSLMock
|
|
||||||
|
|
||||||
import Chronoscope.NTS
|
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
|
describe "Chronoscope.NTS.healthy?()" do
|
||||||
test "is healthy" do
|
test "is healthy" do
|
||||||
|
@ -34,64 +13,11 @@ defmodule Chronoscope.NTSTest do
|
||||||
test "shows empty client list" do
|
test "shows empty client list" do
|
||||||
assert list() == []
|
assert list() == []
|
||||||
end
|
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
|
end
|
||||||
|
|
||||||
describe "Chronoscope.NTS.remove()" do
|
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
|
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 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
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue