Ensure we always close the port
This commit is contained in:
parent
6672860b19
commit
001151e7d9
2 changed files with 15 additions and 0 deletions
|
@ -273,6 +273,16 @@ defmodule GenMagic.Server do
|
||||||
{:next_state, :starting, %{data | port: nil, cycles: 0}}
|
{:next_state, :starting, %{data | port: nil, cycles: 0}}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@doc false
|
||||||
|
@impl :gen_statem
|
||||||
|
def terminate(_, _, %{port: port}) do
|
||||||
|
Kernel.send(port, {self(), :close})
|
||||||
|
end
|
||||||
|
|
||||||
|
def terminate(_, _, _) do
|
||||||
|
:ok
|
||||||
|
end
|
||||||
|
|
||||||
defp send(port, command) do
|
defp send(port, command) do
|
||||||
Kernel.send(port, {self(), {:command, :erlang.term_to_binary(command)}})
|
Kernel.send(port, {self(), {:command, :erlang.term_to_binary(command)}})
|
||||||
end
|
end
|
||||||
|
|
|
@ -6,11 +6,13 @@ defmodule GenMagic.ApprenticeTest do
|
||||||
|
|
||||||
test "sends ready" do
|
test "sends ready" do
|
||||||
port = Port.open(GenMagic.Config.get_port_name(), GenMagic.Config.get_port_options([]))
|
port = Port.open(GenMagic.Config.get_port_name(), GenMagic.Config.get_port_options([]))
|
||||||
|
on_exit(fn() -> send(port, {self(), :close}) end)
|
||||||
assert_ready(port)
|
assert_ready(port)
|
||||||
end
|
end
|
||||||
|
|
||||||
test "stops" do
|
test "stops" do
|
||||||
port = Port.open(GenMagic.Config.get_port_name(), GenMagic.Config.get_port_options([]))
|
port = Port.open(GenMagic.Config.get_port_name(), GenMagic.Config.get_port_options([]))
|
||||||
|
on_exit(fn() -> send(port, {self(), :close}) end)
|
||||||
assert_ready(port)
|
assert_ready(port)
|
||||||
send(port, {self(), {:command, :erlang.term_to_binary({:stop, :stop})}})
|
send(port, {self(), {:command, :erlang.term_to_binary({:stop, :stop})}})
|
||||||
assert_receive {^port, {:exit_status, 0}}
|
assert_receive {^port, {:exit_status, 0}}
|
||||||
|
@ -19,6 +21,7 @@ defmodule GenMagic.ApprenticeTest do
|
||||||
test "exits with no database" do
|
test "exits with no database" do
|
||||||
opts = [:use_stdio, :binary, :exit_status, {:packet, 2}, {:args, []}]
|
opts = [:use_stdio, :binary, :exit_status, {:packet, 2}, {:args, []}]
|
||||||
port = Port.open(GenMagic.Config.get_port_name(), opts)
|
port = Port.open(GenMagic.Config.get_port_name(), opts)
|
||||||
|
on_exit(fn() -> send(port, {self(), :close}) end)
|
||||||
assert_receive {^port, {:exit_status, 1}}
|
assert_receive {^port, {:exit_status, 1}}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -32,12 +35,14 @@ defmodule GenMagic.ApprenticeTest do
|
||||||
]
|
]
|
||||||
|
|
||||||
port = Port.open(GenMagic.Config.get_port_name(), opts)
|
port = Port.open(GenMagic.Config.get_port_name(), opts)
|
||||||
|
on_exit(fn() -> send(port, {self(), :close}) end)
|
||||||
assert_receive {^port, {:exit_status, 3}}
|
assert_receive {^port, {:exit_status, 3}}
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "port" do
|
describe "port" do
|
||||||
setup do
|
setup do
|
||||||
port = Port.open(GenMagic.Config.get_port_name(), GenMagic.Config.get_port_options([]))
|
port = Port.open(GenMagic.Config.get_port_name(), GenMagic.Config.get_port_options([]))
|
||||||
|
on_exit(fn() -> send(port, {self(), :close}) end)
|
||||||
assert_ready(port)
|
assert_ready(port)
|
||||||
%{port: port}
|
%{port: port}
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue