Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion lib/postgrex/notifications.ex
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,13 @@ defmodule Postgrex.Notifications do
if map_size(state.listener_channels[channel]) == 0 do
{_, state} = pop_in(state.listener_channels[channel])

{:query, ~s(UNLISTEN #{quote_channel(channel)}), %{state | from: from}}
if state.connected do
{:query, ~s(UNLISTEN #{quote_channel(channel)}), %{state | from: from}}
else
from && SimpleConnection.reply(from, :ok)

{:noreply, state}
end
else
from && SimpleConnection.reply(from, :ok)

Expand Down
15 changes: 11 additions & 4 deletions test/notification_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,26 @@ defmodule NotificationTest do
end

test "does not fail on sync connection with auto reconnect" do
Process.flag(:trap_exit, true)
assert {:ok, pid} = PN.start_link(database: "nobody_knows_it", auto_reconnect: true)
assert {:eventually, _} = PN.listen(pid, "channel")
assert Process.alive?(pid)
end

test "does not fail on async connection with auto reconnect" do
Process.flag(:trap_exit, true)

assert {:ok, pid} =
PN.start_link(database: "nobody_knows_it", auto_reconnect: true, sync_connect: false)

assert {:eventually, _} = PN.listen(pid, "channel")
refute_receive {:EXIT, _, ^pid}, 100
assert Process.alive?(pid)
end

test "does not fail on unlisten while disconnected" do
assert {:ok, pid} =
PN.start_link(database: "nobody_knows_it", auto_reconnect: true, sync_connect: false)

assert {:eventually, ref} = PN.listen(pid, "channel")
assert :ok = PN.unlisten(pid, ref)
assert Process.alive?(pid)
end

test "listening", context do
Expand Down
Loading