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
15 changes: 10 additions & 5 deletions lib/event_store/subscriptions/subscription.ex
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ defmodule EventStore.Subscriptions.Subscription do
:stream_uuid,
:subscription_name,
:subscription,
:retry_interval
:retry_interval,
:retry_timer
]

def start_link(opts) do
Expand Down Expand Up @@ -116,7 +117,7 @@ defmodule EventStore.Subscriptions.Subscription do
def handle_info(:subscribe_to_stream, %Subscription{} = state) do
Logger.debug(describe(state) <> " subscribe to stream")

%Subscription{subscription: subscription} = state
%Subscription{subscription: subscription} = state = %Subscription{state | retry_timer: nil}

state =
subscription
Expand Down Expand Up @@ -339,11 +340,15 @@ defmodule EventStore.Subscriptions.Subscription do
%Subscription{subscription: %SubscriptionFsm{state: fsm}} = state
)
when fsm in [:initial, :disconnected] do
%Subscription{retry_interval: retry_interval} = state
case state do
%Subscription{retry_timer: nil, retry_interval: retry_interval} ->
retry_timer = Process.send_after(self(), :subscribe_to_stream, retry_interval)

_ref = Process.send_after(self(), :subscribe_to_stream, retry_interval)
%Subscription{state | retry_timer: retry_timer}

state
%Subscription{} ->
state
end
end

defp handle_subscription_state(
Expand Down
52 changes: 50 additions & 2 deletions test/subscriptions/subscription_locking_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ defmodule EventStore.Subscriptions.SubscriptionLockingTest do

import ExUnit.CaptureLog

alias EventStore.{Config, EventFactory, Storage, UUID, Wait}
alias EventStore.{Config, EventFactory, RecordedEvent, Storage, UUID, Wait}
alias EventStore.Subscriptions.{FencingToken, Subscription}

@conn TestEventStore.Postgrex
Expand Down Expand Up @@ -288,6 +288,28 @@ defmodule EventStore.Subscriptions.SubscriptionLockingTest do
end
end

describe "subscription retrying while disconnected" do
@describetag retry_interval: 60_000

setup [:create_subscription]

test "keeps a single retry pending however many events it hears about", %{
subscription: subscription
} do
assert_receive {:subscribed, ^subscription}

trace_retries(subscription)

:ok = disconnect(subscription)

for event_number <- 1..10 do
send(subscription, {:events, [%RecordedEvent{event_number: event_number}]})
end

assert retries_scheduled(subscription) == 1
end
end

describe "subscription stopped after another owner took over" do
test "should leave the progress of the new owner untouched", %{
conn: conn,
Expand Down Expand Up @@ -371,7 +393,7 @@ defmodule EventStore.Subscriptions.SubscriptionLockingTest do
serializer: serializer,
correlation_id_type: correlation_id_type,
causation_id_type: causation_id_type,
retry_interval: 1_000,
retry_interval: Map.get(context, :retry_interval, 1_000),
stream_uuid: "$all",
subscription_name: subscription_name,
buffer_size: 3,
Expand Down Expand Up @@ -424,6 +446,32 @@ defmodule EventStore.Subscriptions.SubscriptionLockingTest do
state
end

defp trace_retries(subscription) do
:erlang.trace_pattern({:erlang, :send_after, :_}, true, [:global])
:erlang.trace(subscription, true, [:call])

on_exit(fn -> :erlang.trace_pattern({:erlang, :send_after, :_}, false, [:global]) end)
end

defp retries_scheduled(subscription) do
_state = :sys.get_state(subscription)

ref = :erlang.trace_delivered(subscription)
assert_receive {:trace_delivered, ^subscription, ^ref}

count_retries(subscription, 0)
end

defp count_retries(subscription, count) do
receive do
{:trace, ^subscription, :call,
{:erlang, :send_after, [_time, _dest, :subscribe_to_stream | _opts]}} ->
count_retries(subscription, count + 1)
after
0 -> count
end
end

defp wait_for_new_lock_ref(subscription, old_lock_ref) do
Wait.until(2_000, fn ->
lock_ref = lock_ref(subscription)
Expand Down
Loading