Skip to content

Commit

Permalink
Throw clearer error message during Stash() in null message cases (#…
Browse files Browse the repository at this point in the history
…7425)

close #7938
  • Loading branch information
Aaronontheweb authored Dec 19, 2024
1 parent 487bb85 commit 7e85cac
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
47 changes: 47 additions & 0 deletions src/core/Akka.Tests/Actor/Stash/Bugfix7398Specs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// -----------------------------------------------------------------------
// <copyright file="Bugfix7398Specs.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 Akka.Actor;
using Akka.TestKit;
using Xunit;
using Xunit.Abstractions;

namespace Akka.Tests.Actor.Stash;

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

private class IllegalStashActor : UntypedActor, IWithStash
{
protected override void OnReceive(object message)
{

}

protected override void PreStart()
{
// ILLEGAL
Stash.Stash();
}

public IStash Stash { get; set; }
}

[Fact]
public void Should_throw_exception_when_stashing_in_PreStart()
{
EventFilter.Exception<ActorInitializationException>().ExpectOne(() =>
{
var actor = Sys.ActorOf(Props.Create<IllegalStashActor>());
actor.Tell("hello");
});
}
}
4 changes: 3 additions & 1 deletion src/core/Akka/Actor/Stash/Internal/AbstractStash.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,11 @@ public void Stash()
{
var currMsg = _actorCell.CurrentMessage;
var sender = _actorCell.Sender;

if (_actorCell.CurrentEnvelopeId == _currentEnvelopeId)
{
if(currMsg is null)
throw new InvalidOperationException("There is no message to stash right now. Stash() must be called inside an actor's Receive methods.");
throw new IllegalActorStateException($"Can't stash the same message {currMsg} more than once");
}
_currentEnvelopeId = _actorCell.CurrentEnvelopeId;
Expand Down

0 comments on commit 7e85cac

Please sign in to comment.