Skip to content
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

Added reproduction for missing Stash.Stash messages in Akka.Persistence actors #7374

Merged
merged 2 commits into from
Nov 1, 2024
Merged
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
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;
}
}
}
}
Loading