Skip to content

Commit

Permalink
Fix flaky test (disconnect on server side could result either in :clo…
Browse files Browse the repository at this point in the history
…sed or :einval depending on timing of send call)
  • Loading branch information
bokner committed Jul 11, 2023
1 parent 2daeb38 commit 903d779
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
13 changes: 7 additions & 6 deletions lib/mllp/client.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ defmodule MLLP.ClientContract do
MLLP.ClientContract provides the behavior implemented by MLLP.Client. It may be useful
for testing in your own application with tools such as [`Mox`](https://hexdocs.pm/mox/)
"""
@type error_type :: :connect_failure | :send_error | :recv_error
@type error_type :: :tcp_error | :send_error | :recv_error
@type error_reason :: :closed | :timeout | :no_socket | :inet.posix()

@type client_error :: MLLP.Client.Error.t()
Expand Down Expand Up @@ -162,7 +162,7 @@ defmodule MLLP.Client do
pid: nil,
telemetry_module: nil,
tcp: nil,
connect_failure: nil,
tcp_error: nil,
host_string: nil,
send_opts: %{},
tls_opts: [],
Expand Down Expand Up @@ -404,7 +404,7 @@ defmodule MLLP.Client do
end

def disconnected({:call, from}, {:send, _message, _options}, state) do
actions = [{:reply, from, {:error, new_error(:send, state.connect_failure)}}]
actions = [{:reply, from, {:error, new_error(:send, state.tcp_error)}}]
{:keep_state_and_data, actions}
end

Expand Down Expand Up @@ -603,7 +603,7 @@ defmodule MLLP.Client do
|> tap(fn state ->
telemetry(
:status,
%{status: :disconnected, error: format_error(reason), context: "send message failure"},
%{status: :disconnected, error: format_error(reason)},
state
)
end)
Expand Down Expand Up @@ -645,6 +645,7 @@ defmodule MLLP.Client do

state
|> Map.put(:socket, nil)
|> Map.put(:tcp_error, error)
end

defp backoff_succeed(%State{backoff: nil} = state), do: state
Expand All @@ -665,7 +666,7 @@ defmodule MLLP.Client do
|> backoff_succeed()

telemetry(:status, %{status: :connected}, state1)
{:ok, %{state1 | socket: socket, connect_failure: nil}}
{:ok, %{state1 | socket: socket, tcp_error: nil}}

{:error, reason} ->
message = format_error(reason)
Expand All @@ -680,7 +681,7 @@ defmodule MLLP.Client do
{:error,
state
|> maybe_update_reconnection_timeout()
|> Map.put(:connect_failure, reason)}
|> Map.put(:tcp_error, reason)}
end
end

Expand Down
12 changes: 8 additions & 4 deletions test/client_and_receiver_integration_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -221,10 +221,14 @@ defmodule ClientAndReceiverIntegrationTest do
MLLP.Receiver.stop(port)
refute Process.alive?(receiver_pid)

assert match?(
{:error, %Error{context: _, message: "connection closed"}},
MLLP.Client.send(client_pid, "Simple message")
)
{:error, %Error{context: context, message: message}} =
MLLP.Client.send(client_pid, "Simple message")

assert context in [:send, :recv]
assert message in [MLLP.Client.format_error(:closed), MLLP.Client.format_error(:einval)]

refute MLLP.Client.is_connected?(client_pid)

end

test "with a larger message" do
Expand Down

0 comments on commit 903d779

Please sign in to comment.