Skip to content

Commit

Permalink
Added reproduction for missing Stash.Stash messages in Akka.,Persis…
Browse files Browse the repository at this point in the history
…tence actors (#7374)

reproduces #7373
  • Loading branch information
Aaronontheweb authored Nov 1, 2024
1 parent 67fad90 commit 3700809
Showing 1 changed file with 81 additions and 0 deletions.
81 changes: 81 additions & 0 deletions src/core/Akka.Persistence.Tests/Bugfix7373Specs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// -----------------------------------------------------------------------
// <copyright file="Bugfix7373Specs.cs" company="Akka.NET Project">
// Copyright (C) 2009-2024 Lightbend Inc. <http://www.lightbend.com>
// Copyright (C) 2013-2024 .NET Foundation <https://github.com/akkadotnet/akka.net>
// </copyright>
// -----------------------------------------------------------------------

using System.Threading.Tasks;
using Akka.Actor;
using Akka.TestKit;
using Xunit;
using Xunit.Abstractions;

namespace Akka.Persistence.Tests;

public class Bugfix7373Specs : AkkaSpec
{
public Bugfix7373Specs(ITestOutputHelper output) : base(output)
{
}

/// <summary>
/// Reproduction for https://github.com/akkadotnet/akka.net/issues/7373
/// </summary>
[Fact]
public async Task ShouldDeliverAllStashedMessages()
{
// arrange
var actor = Sys.ActorOf(Props.Create<MinimalStashingActor>());

// act
var msg = new Msg(1);
actor.Tell(msg);
actor.Tell(msg);

actor.Tell("Initialize");

// assert
await ExpectMsgAsync($"Processed: {msg}");
await ExpectMsgAsync($"Processed: {msg}");
}

public sealed record Msg(int Id);

public class MinimalStashingActor : UntypedPersistentActor, IWithStash
{
public override string PersistenceId => "minimal-stashing-actor";

protected override void OnCommand(object message)
{
Sender.Tell($"Processed: {message}");
}

private void Ready(object message)
{
switch (message)
{
case "Initialize":
Persist("init", e =>
{
Stash.UnstashAll(); // Unstash all stashed messages
Become(OnCommand); // Transition to ready state
});
break;
default:
Stash.Stash(); // Stash messages until initialized
break;
}
}

protected override void OnRecover(object message)
{
switch (message)
{
case RecoveryCompleted:
Become(Ready);
break;
}
}
}
}

0 comments on commit 3700809

Please sign in to comment.