Handle all possibilities that arise in test cases
This commit is contained in:
parent
feff0b29cd
commit
b8169a592d
|
@ -44,9 +44,14 @@ defmodule Chronoscope.NTS do
|
|||
|
||||
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
|
||||
|
||||
[] ->
|
||||
{: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, _}] ->
|
||||
if Process.alive?(pid) do
|
||||
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
|
||||
|> DynamicSupervisor.start_child({NTS.Client, host: host, port: port, name: {:via, Registry, {NTS.Registry, name}}})
|
||||
|> then(fn {:ok, pid} -> pid end)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue