Skip to content

Commit

Permalink
Fix the typo on Application.Connection and make sure the server stops…
Browse files Browse the repository at this point in the history
… on each test
  • Loading branch information
ono committed Sep 1, 2021
1 parent 0046555 commit f2b2aba
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
9 changes: 8 additions & 1 deletion lib/amqp/application/connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ defmodule AMQP.Application.Connection do
GenServer.start_link(__MODULE__, init_arg, name: name)
end

@doc false
def start(opts) do
{name, init_arg} = link_opts_to_init_arg(opts)

GenServer.start(__MODULE__, init_arg, name: name)
end

defp link_opts_to_init_arg(opts) do
proc_name = Keyword.get(opts, :proc_name, :default)
server_name = get_server_name(proc_name)
Expand Down Expand Up @@ -145,7 +152,7 @@ defmodule AMQP.Application.Connection do
%{state | connection: nil, monitor_ref: nil}
end

defp close(%{connection: %Connection{} = conn, monior_ref: ref}) do
defp close(%{connection: %Connection{} = conn, monitor_ref: ref}) do
if Process.alive?(conn.pid) do
Process.demonitor(ref)
Connection.close(conn)
Expand Down
9 changes: 5 additions & 4 deletions test/application/channel_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ defmodule AMQP.Application.ChnnelTest do
alias AMQP.Application.Channel, as: AppChan

setup do
{:ok, app_conn_pid} = AppConn.start_link([])
{:ok, app_conn_pid} = AppConn.start([])

on_exit(fn ->
Process.exit(app_conn_pid, :normal)
GenServer.stop(app_conn_pid)
end)

[app_conn: app_conn_pid]
Expand All @@ -18,17 +18,18 @@ defmodule AMQP.Application.ChnnelTest do
{:ok, pid} = AppChan.start_link(opts)

assert {:ok, %AMQP.Channel{}} = AppChan.get_channel(:test_chan)
Process.exit(pid, :normal)
GenServer.stop(pid, :normal)
end

test "reconnects when the channel is gone" do
opts = [connection: :default, proc_name: :test_chan]
{:ok, _pid} = AppChan.start_link(opts)
{:ok, pid} = AppChan.start_link(opts)
{:ok, %AMQP.Channel{} = chan1} = AppChan.get_channel(:test_chan)
AMQP.Channel.close(chan1)
:timer.sleep(50)

assert {:ok, %AMQP.Channel{} = chan2} = AppChan.get_channel(:test_chan)
refute chan1 == chan2
GenServer.stop(pid)
end
end
5 changes: 3 additions & 2 deletions test/application/connection_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,17 @@ defmodule AMQP.Application.ConnectionTest do
{:ok, conn} = AppConn.get_connection(:my_conn)
assert %AMQP.Connection{} = conn

Process.exit(pid, :normal)
GenServer.stop(pid)
end

test "reconnects when the connection is gone" do
{:ok, _pid} = AppConn.start_link([])
{:ok, pid} = AppConn.start_link([])
{:ok, %AMQP.Connection{} = conn1} = AppConn.get_connection()
AMQP.Connection.close(conn1)
:timer.sleep(50)

assert {:ok, %AMQP.Connection{} = conn2} = AppConn.get_connection()
refute conn1 == conn2
GenServer.stop(pid)
end
end

0 comments on commit f2b2aba

Please sign in to comment.