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
[{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

View File

@ -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

View File

@ -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

View File