Current section
Files
Jump to
Current section
Files
lib/retort/connection_case.ex
defmodule Retort.ConnectionCase do
@moduledoc """
Helpers for tests that manipulate the `Retort.Connection`
"""
@dialyzer {:nowarn_function, capture_reconnect_log: 0}
alias Retort.Connection
import ExUnit.CaptureLog
use ExUnit.CaseTemplate
using do
quote do
import Retort.ConnectionCase
end
end
# Callbacks
setup do
# await for a new connection in case the previous test killed the connection
Connection.await
on_exit fn ->
# ensures the connection exits for the last test in the suite, so that RPC server tests aren't broken by these
# tests
Connection.await
end
:ok
end
# Functions
@spec capture_reconnect_log() :: String.t
def capture_reconnect_log do
assert {:ok, connection = %AMQP.Connection{pid: connection_pid}} = Connection.get()
connection_timeout = 5
override_connection_timeout(connection_timeout) # milliseconds
original_url = override_url("amqp://guest:guest@example.com:5672")
capture_log fn ->
Process.monitor connection_pid
AMQP.Connection.close(connection)
receive do
{:DOWN, _, :process, ^connection_pid, _} -> :ok
end
{:error, :disconnected} = Connection.get(5_000)
Process.sleep connection_timeout * 5
Connection.put_url(original_url)
# ensure original url is being used before existing capture_log, so errors don't appear
assert {:ok, _} = Connection.await
end
end
@spec log_queue_error_count(String.t, String.t) :: non_neg_integer
def log_queue_error_count(log, queue) do
log
|> String.split("\n")
|> Enum.count(
&String.contains?(
&1,
"queue=#{queue} [error]"
)
)
end
@spec override_connection_timeout(timeout) :: timeout
def override_connection_timeout(connection_timeout) do
original_connection_timeout = Connection.get_connection_timeout()
on_exit fn ->
Connection.put_connection_timeout(original_connection_timeout)
end
Connection.put_connection_timeout(connection_timeout)
original_connection_timeout
end
@spec override_url(String.t) :: String.t
def override_url(url) do
original_url = Connection.get_url()
on_exit fn ->
Connection.put_url(original_url)
end
Connection.put_url(url)
original_url
end
end