Handle all possibilities that arise in test cases

This commit is contained in:
Mike Cifelli 2024-04-14 19:01:08 -04:00
parent feff0b29cd
commit b8169a592d
Signed by: mike
GPG Key ID: 6B08C6BE47D08E4C
4 changed files with 53 additions and 10 deletions

View File

@ -44,9 +44,14 @@ defmodule Chronoscope.NTS do
case Registry.lookup(NTS.Registry, name) do case Registry.lookup(NTS.Registry, name) do
[{pid, _}] -> [{pid, _}] ->
if Process.alive?(pid) do
pid pid
|> GenServer.call(:terminate) |> GenServer.call(:terminate)
|> wait_for_termination() |> wait_for_termination()
else
# Registry.unregister(NTS.Registry, name)
{:error, :notfound}
end
[] -> [] ->
{:error, :notfound} {:error, :notfound}
@ -55,20 +60,29 @@ 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, port), :key_establishment) GenServer.call(client_pid(%{host: host, port: port}), :key_establishment)
end end
defp client_pid(host, port) do defp client_pid(server) do
name = "#{host}:#{port}" name = "#{server.host}:#{server.port}"
case Registry.lookup(NTS.Registry, name) do case Registry.lookup(NTS.Registry, name) do
[{pid, _}] -> [{pid, _}] ->
if Process.alive?(pid) do
pid pid
else
# Registry.unregister(NTS.Registry, name)
start_client(server, name)
end
[] -> [] ->
start_client(server, name)
end
end
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}}})
|> then(fn {:ok, pid} -> pid end) |> then(fn {:ok, pid} -> pid end)
end end
end
end end

View File

@ -31,6 +31,10 @@ defmodule Chronoscope.NTS.ClientTest do
end end
describe "Chronoscope.NTS.Client.handle_call()" do 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 test ":list" do
assert handle_call(:list, nil, %{state: true}) == {:reply, %{state: true}, %{state: true}} assert handle_call(:list, nil, %{state: true}) == {:reply, %{state: true}, %{state: true}}
end end

View File

@ -59,6 +59,31 @@ defmodule Chronoscope.NTSTest do
assert remove("localhost", 1111) == {:error, :notfound} assert remove("localhost", 1111) == {:error, :notfound}
assert [%{host: "localhost", key_establishment_response: _, last_key_establishment: _, port: 4444}] = list() assert [%{host: "localhost", key_establishment_response: _, last_key_establishment: _, port: 4444}] = list()
end 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 end
describe "Chronoscope.NTS.key_establishment()" do describe "Chronoscope.NTS.key_establishment()" do

View File