Skip to content

Commit

Permalink
TimerPersistentActorSpec added
Browse files Browse the repository at this point in the history
  • Loading branch information
zbynek001 committed May 9, 2019
1 parent af41aba commit 42b490b
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 19 deletions.
114 changes: 114 additions & 0 deletions src/core/Akka.Persistence.Tests/TimerPersistentActorSpec.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
//-----------------------------------------------------------------------
// <copyright file="ManyRecoveriesSpec.cs" company="Akka.NET Project">
// Copyright (C) 2009-2018 Lightbend Inc. <http://www.lightbend.com>
// Copyright (C) 2013-2018 .NET Foundation <https://github.com/akkadotnet/akka.net>
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Linq;
using Akka.Actor;
using Akka.Configuration;
using Akka.TestKit;
using Akka.TestKit.TestActors;
using Akka.Util.Internal;
using FluentAssertions;
using Xunit;

namespace Akka.Persistence.Tests
{
public class TimerPersistentActorSpec : PersistenceSpec
{
public TimerPersistentActorSpec() : base(ConfigurationFactory.ParseString(@"
akka.persistence.journal.plugin = ""akka.persistence.journal.inmem""
# snapshot store plugin is NOT defined, things should still work
akka.persistence.snapshot-store.plugin = ""akka.persistence.no-snapshot-store""
akka.persistence.snapshot-store.local.dir = ""target/snapshots-" + typeof(RecoveryPermitterSpec).FullName + "/"))
{
}

[Fact]
public void PersistentActor_with_Timer_must_not_discard_timer_msg_due_to_stashing()
{
var pa = ActorOf(TestPersistentActor.TestProps("p1"));
pa.Tell("msg1");
ExpectMsg<string>("msg1");
}

[Fact]
public void PersistentActor_with_Timer_must_handle_AutoReceivedMessages_automatically()
{
var pa = ActorOf(TestPersistentActor.TestProps("p3"));
Watch(pa);
pa.Tell(new AutoReceivedMessageWrapper(PoisonPill.Instance));
ExpectTerminated(pa);
}

#region Actors

internal class Scheduled
{
public object Msg { get; }
public IActorRef ReplyTo { get; }

public Scheduled(object msg, IActorRef replyTo)
{
Msg = msg;
ReplyTo = replyTo;
}
}

internal class AutoReceivedMessageWrapper
{
public IAutoReceivedMessage Msg { get; }

public AutoReceivedMessageWrapper(IAutoReceivedMessage msg)
{
Msg = msg;
}
}

internal class TestPersistentActor : PersistentActor
{
public static Props TestProps(string name)
{
return Props.Create(() => new TestPersistentActor(name));
}

private readonly string name;

public override string PersistenceId => name;

public TestPersistentActor(string name)
{
this.name = name;
}

protected override bool ReceiveRecover(object message)
{
return true;
}

protected override bool ReceiveCommand(object message)
{
switch (message)
{
case Scheduled m:
m.ReplyTo.Tell(m.Msg);
return true;

case AutoReceivedMessageWrapper m:
Timers.StartSingleTimer("PoisonPill", PoisonPill.Instance, TimeSpan.Zero);
return true;
default:
Timers.StartSingleTimer("key", new Scheduled(message, Sender), TimeSpan.Zero);
Persist(message, _ => { });
return true;
}
}
}

#endregion
}
}
38 changes: 19 additions & 19 deletions src/core/Akka.Tests/Actor/TimerSpec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -190,11 +190,11 @@ public void Must_handle_AutoReceivedMessages_automatically()

#region Actors

public interface ICommand
internal interface ICommand
{
}

public class Tick : ICommand
internal class Tick : ICommand
{
public int N { get; }

Expand All @@ -204,7 +204,7 @@ public Tick(int n)
}
}

public class Bump : ICommand
internal class Bump : ICommand
{
public static readonly Bump Instance = new Bump();

Expand All @@ -213,7 +213,7 @@ private Bump()
}
}

public class SlowThenBump : ICommand, INoSerializationVerificationNeeded
internal class SlowThenBump : ICommand, INoSerializationVerificationNeeded
{
public TestLatch Latch { get; }

Expand All @@ -223,7 +223,7 @@ public SlowThenBump(TestLatch latch)
}
}

public class End : ICommand
internal class End : ICommand
{
public static readonly End Instance = new End();

Expand All @@ -232,7 +232,7 @@ private End()
}
}

public class Throw : ICommand
internal class Throw : ICommand
{
public Exception E { get; }

Expand All @@ -242,7 +242,7 @@ public Throw(Exception e)
}
}

public class Cancel : ICommand
internal class Cancel : ICommand
{
public static readonly Cancel Instance = new Cancel();

Expand All @@ -251,7 +251,7 @@ private Cancel()
}
}

public class SlowThenThrow : ICommand, INoSerializationVerificationNeeded
internal class SlowThenThrow : ICommand, INoSerializationVerificationNeeded
{
public TestLatch Latch { get; }
public Exception E { get; }
Expand All @@ -263,7 +263,7 @@ public SlowThenThrow(TestLatch latch, Exception e)
}
}

public class AutoReceive : ICommand
internal class AutoReceive : ICommand
{
public static readonly AutoReceive Instance = new AutoReceive();

Expand All @@ -272,11 +272,11 @@ private AutoReceive()
}
}

public interface IEvent
internal interface IEvent
{
}

public class Tock : IEvent
internal class Tock : IEvent
{
public int N { get; }

Expand All @@ -285,7 +285,7 @@ public Tock(int n)
N = n;
}
}
public class GotPostStop : IEvent
internal class GotPostStop : IEvent
{
public bool TimerActive { get; }

Expand All @@ -295,7 +295,7 @@ public GotPostStop(bool timerActive)
}
}

public class GotPreRestart : IEvent
internal class GotPreRestart : IEvent
{
public bool TimerActive { get; }

Expand All @@ -305,15 +305,15 @@ public GotPreRestart(bool timerActive)
}
}

public class Exc : Exception
internal class Exc : Exception
{
public Exc()
: base("simulated exc")
{
}
}

public class Target : ActorBase
internal class Target : ActorBase
{
private IActorRef monitor;
private TimeSpan interval;
Expand Down Expand Up @@ -393,7 +393,7 @@ protected override bool Receive(object message)
}
}

public class TheState
internal class TheState
{
public static readonly TheState Instance = new TheState();

Expand All @@ -402,7 +402,7 @@ private TheState()
}
}

public class FsmTarget : FSM<TheState, int>
internal class FsmTarget : FSM<TheState, int>
{
private readonly IActorRef monitor;
private readonly TimeSpan interval;
Expand Down Expand Up @@ -506,7 +506,7 @@ public void Timers_combined_with_stashing_should_work()

#region actors

public class StopStashing
internal class StopStashing
{
public static readonly StopStashing Instance = new StopStashing();

Expand All @@ -515,7 +515,7 @@ private StopStashing()
}
}

public class ActorWithTimerAndStash : ActorBase, IWithUnboundedStash
internal class ActorWithTimerAndStash : ActorBase, IWithUnboundedStash
{
private IActorRef probe;

Expand Down

0 comments on commit 42b490b

Please sign in to comment.