-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Akka.Actor: IStash
API and configuration enhancements
#6660
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -695,6 +695,12 @@ public void Prepend(IEnumerable<Envelope> envelopes) | |
{ | ||
_userStash.Prepend(envelopes); | ||
} | ||
|
||
public int Count => _userStash.Count; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For the |
||
public bool IsEmpty => _userStash.IsEmpty; | ||
public bool NonEmpty => _userStash.NonEmpty; | ||
public bool IsFull => _userStash.IsFull; | ||
public int Capacity => _userStash.Capacity; | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
//----------------------------------------------------------------------- | ||
// <copyright file="StashCapacitySpecs.cs" company="Akka.NET Project"> | ||
// Copyright (C) 2009-2023 Lightbend Inc. <http://www.lightbend.com> | ||
// Copyright (C) 2013-2023 .NET Foundation <https://github.com/akkadotnet/akka.net> | ||
// </copyright> | ||
//----------------------------------------------------------------------- | ||
|
||
using System; | ||
using System.Threading.Tasks; | ||
using Akka.Actor; | ||
using Akka.Configuration; | ||
using Akka.TestKit; | ||
using Xunit; | ||
using Xunit.Abstractions; | ||
using FluentAssertions; | ||
|
||
namespace Akka.Tests.Actor.Stash; | ||
|
||
public class StashCapacitySpecs : AkkaSpec | ||
{ | ||
public StashCapacitySpecs(ITestOutputHelper output) : base(Config.Empty, output: output) | ||
{ | ||
|
||
} | ||
|
||
[Fact] | ||
public async Task ShouldGetAccurateStashReadingForUnboundedStash() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Basic happy path stash spec. Going to add one for dealing with bounded stashes once I get the other half of #6658 |
||
{ | ||
var stashActor = Sys.ActorOf(Props.Create(() => new StashActor())); | ||
stashActor.Tell(new StashActor.StashMessage("1")); | ||
stashActor.Tell(new StashActor.StashMessage("2")); | ||
stashActor.Tell(StashActor.GetStashReadout.Instance); | ||
var readout1 = await ExpectMsgAsync<StashActor.StashReadout>(); | ||
readout1.Capacity.Should().Be(-1); // unbounded stash returns -1 for capacity | ||
readout1.Size.Should().Be(2); | ||
readout1.IsEmpty.Should().BeFalse(); | ||
readout1.IsFull.Should().BeFalse(); | ||
|
||
stashActor.Tell(StashActor.UnstashMessage.Instance); | ||
stashActor.Tell(StashActor.GetStashReadout.Instance); | ||
var readout2 = await ExpectMsgAsync<StashActor.StashReadout>(); | ||
readout2.Capacity.Should().Be(-1); | ||
readout2.Size.Should().Be(1); | ||
readout2.IsEmpty.Should().BeFalse(); | ||
readout2.IsFull.Should().BeFalse(); | ||
|
||
stashActor.Tell(StashActor.UnstashMessage.Instance); | ||
stashActor.Tell(StashActor.GetStashReadout.Instance); | ||
var readout3 = await ExpectMsgAsync<StashActor.StashReadout>(); | ||
readout3.Capacity.Should().Be(-1); | ||
readout3.Size.Should().Be(0); | ||
readout3.IsEmpty.Should().BeTrue(); | ||
readout3.IsFull.Should().BeFalse(); | ||
} | ||
|
||
private class StashActor : UntypedActorWithStash | ||
{ | ||
public class StashMessage | ||
{ | ||
public StashMessage(string message) | ||
{ | ||
Message = message; | ||
} | ||
|
||
public string Message { get; } | ||
} | ||
|
||
public class UnstashMessage | ||
{ | ||
private UnstashMessage() | ||
{ | ||
|
||
} | ||
public static readonly UnstashMessage Instance = new(); | ||
} | ||
|
||
public class GetStashReadout | ||
{ | ||
private GetStashReadout() | ||
{ | ||
|
||
} | ||
public static readonly GetStashReadout Instance = new(); | ||
} | ||
|
||
public class StashReadout | ||
{ | ||
public StashReadout(int capacity, int size, bool isEmpty, bool isFull) | ||
{ | ||
Capacity = capacity; | ||
Size = size; | ||
IsEmpty = isEmpty; | ||
IsFull = isFull; | ||
} | ||
|
||
public int Capacity { get; } | ||
public int Size { get; } | ||
|
||
public bool IsEmpty { get; } | ||
|
||
public bool IsFull { get; } | ||
} | ||
|
||
protected override void OnReceive(object message) | ||
{ | ||
switch (message) | ||
{ | ||
case StashMessage msg: | ||
Stash.Stash(); | ||
break; | ||
case UnstashMessage _: | ||
Stash.Unstash(); | ||
Context.Become(Unstashing); // switch behaviors so we're not stuck in stash loop | ||
break; | ||
case GetStashReadout _: | ||
Sender.Tell(new StashReadout(Stash.Capacity, Stash.Count, Stash.IsEmpty, Stash.IsFull)); | ||
break; | ||
default: | ||
Unhandled(message); | ||
break; | ||
} | ||
} | ||
|
||
private void Unstashing(object message) | ||
{ | ||
switch (message) | ||
{ | ||
case StashMessage msg: // do nothing | ||
break; | ||
case UnstashMessage when Stash.NonEmpty: | ||
Stash.Unstash(); | ||
break; | ||
case UnstashMessage _: // when the stash is empty, go back to stashing | ||
Context.Become(OnReceive); | ||
break; | ||
case GetStashReadout _: | ||
Sender.Tell(new StashReadout(Stash.Capacity, Stash.Count, Stash.IsEmpty, Stash.IsFull)); | ||
break; | ||
default: | ||
Unhandled(message); | ||
break; | ||
} | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
New stashing APIs. Pretty self-explanatory.