Skip to content

Commit

Permalink
Bug fix: switch to :disconnected if conn has closed
Browse files Browse the repository at this point in the history
  • Loading branch information
bokner committed Jul 17, 2023
1 parent 9c6cbf1 commit b5db135
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
16 changes: 14 additions & 2 deletions lib/mllp/client.ex
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,7 @@ defmodule MLLP.Client do
def receiving(:info, {transport, socket, incoming}, %{socket: socket} = data)
when transport in [:tcp, :ssl] do
new_data = handle_received(incoming, data)
next_state = (new_data.caller && :receiving) || :connected
next_state = next_after_receiving(new_data)
{:next_state, next_state, new_data}
end

Expand All @@ -565,6 +565,18 @@ defmodule MLLP.Client do
unexpected_message(:receiving, event, unknown)
end

defp next_after_receiving(%State{socket: nil}) do
:disconnected
end

defp next_after_receiving(%State{caller: nil}) do
:connected
end

defp next_after_receiving(%State{} = _state) do
:receiving
end

########################################
### End of :gen_statem callbacks ###
########################################
Expand Down Expand Up @@ -707,7 +719,7 @@ defmodule MLLP.Client do
data
)

Logger.debug("Stopping connection: #{format_error(error)}}")
Logger.debug("Stopping connection: #{format_error(error)}")

if error == :unexpected_packet_received do
:ok = :inet.setopts(socket, linger: {true, 0})
Expand Down
13 changes: 10 additions & 3 deletions test/client_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -175,13 +175,20 @@ defmodule ClientTest do
log =
capture_log([level: :debug], fn ->
Kernel.send(pid, {:tcp, socket, "what's up?"})
Process.sleep(100)

{:error, _} =
Client.send(
pid,
"this should fail as the connection will be dropped on unexpected packet"
)

refute Client.is_connected?(pid)
Client.reconnect(pid)
{:ok, _} = Client.send(pid, "ok")
end)

assert String.contains?(log, Client.format_error(:unexpected_packet_received))
assert String.contains?(log, "Connection closed")

refute Client.is_connected?(pid)
end
end

Expand Down

0 comments on commit b5db135

Please sign in to comment.