Skip to content

Added test demonstrating timer behavior in PersistentActor constructor should fail #7666

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: dev
Choose a base branch
from
Draft
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
49 changes: 49 additions & 0 deletions src/core/Akka.Persistence.Tests/TimerPersistentActorSpec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@
using System.Linq;
using Akka.Actor;
using Akka.Configuration;
using Akka.Event;
using Akka.TestKit;
using Akka.TestKit.TestActors;
using Akka.Util.Internal;
using FluentAssertions;
using Xunit;
using System.Threading.Tasks;

namespace Akka.Persistence.Tests
{
Expand Down Expand Up @@ -45,6 +47,22 @@ public void PersistentActor_with_Timer_must_handle_AutoReceivedMessages_automati
ExpectTerminated(pa);
}

[Fact]
public async Task PersistentActor_must_fire_timer_set_in_constructor()
{
var probe = CreateTestProbe();
var deadLetterProbe = CreateTestProbe();
Sys.EventStream.Subscribe(deadLetterProbe.Ref, typeof(DeadLetter));

var actor = ActorOf(Props.Create(() => new TimerInCtorActor(probe.Ref)));

// According to issue #7650, the timer message should not arrive
await probe.ExpectNoMsgAsync();

// Check if the message ended up in dead letters instead
await deadLetterProbe.ExpectMsgAsync<DeadLetter>(dl => dl.Message.Equals("timer_fired"));
}

#region Actors

internal class Scheduled
Expand Down Expand Up @@ -111,6 +129,37 @@ protected override bool ReceiveCommand(object message)
}
}

internal class TimerInCtorActor : ReceivePersistentActor, IWithTimers
{
private readonly IActorRef _probe;
private readonly ILoggingAdapter _log;
public ITimerScheduler Timers { get; set; }
public override string PersistenceId => "timer-test";

public TimerInCtorActor(IActorRef probe)
{
_probe = probe;
_log = Context.GetLogger();

// Set up timer in constructor
_log.Info("Setting up timer in constructor");
Timers.StartSingleTimer("test-timer", "timer_fired", TimeSpan.FromSeconds(1));

// Log all messages received
Command<object>(msg =>
{
_log.Info("Received command message: {0}", msg);
if (msg is string str)
_probe.Tell(str);
});

Recover<object>(msg =>
{
_log.Info("Received recovery message: {0}", msg);
});
}
}

#endregion
}
}
Loading