-
Notifications
You must be signed in to change notification settings - Fork 1.1k
[V2] ReceiveActor - Removing MatchBuilder and making use of handlers. #7556
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
Closed
Aaronontheweb
wants to merge
17
commits into
akkadotnet:v1.6
from
Aaronontheweb:ReceiveActorHandlers-ordering
Closed
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
520ea0b
Initial working approach but very rough.
mhbuck 586cd7d
Consolidate
mhbuck 4d5a53c
Potentially cleaner approach
mhbuck 9ac82d3
Some clean up and comments.
mhbuck 8480ac6
Remove using
mhbuck 9dd025c
Added test and logic to prevent Any handler from being added twice
mhbuck 76b4a1f
Wanting to confirm these tests are also passing on build
mhbuck 0e5f8d3
TryHandle looks much neater
mhbuck ff1f8f1
Closer to what I think the final layout will be
mhbuck 0a55a59
Moved classes out and starting on tests
mhbuck 8c59776
Test update.
mhbuck bc7beca
More refinement
mhbuck 2e938f7
Getting more tests
mhbuck 2cdeaa7
Covering a bunch of the test cases
mhbuck 5db8c97
Minor Tweak
mhbuck a41ba78
v1.6 - adjust `ReceiveActorHandlers` to ensure preservation of declar…
Aaronontheweb f4742ad
removed comment
Aaronontheweb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,209 @@ | ||
| // ----------------------------------------------------------------------- | ||
| // <copyright file="ReceiveActorHandlersTests.cs" company="Akka.NET Project"> | ||
| // Copyright (C) 2009-2025 Lightbend Inc. <http://www.lightbend.com> | ||
| // Copyright (C) 2013-2025 .NET Foundation <https://github.com/akkadotnet/akka.net> | ||
| // </copyright> | ||
| // ----------------------------------------------------------------------- | ||
|
|
||
| using System; | ||
| using Akka.Actor; | ||
| using Xunit; | ||
|
|
||
| namespace Akka.Tests.Actor; | ||
|
|
||
| public class ReceiveActorHandlersTests | ||
| { | ||
| [Fact] | ||
| public void Given_ReceiveAnyHandler_Added_When_Adding_Any_Other_Handler_Then_Should_Fail() | ||
| { | ||
| var handlers = new ReceiveActorHandlers(); | ||
| handlers.AddReceiveAnyHandler(_ => { }); | ||
|
|
||
| // A ReceiveAny handler has been added, so adding any other handler should fail | ||
| Assert.Throws<InvalidOperationException>(() => | ||
| handlers.AddReceiveAnyHandler(_ => { })); | ||
| Assert.Throws<InvalidOperationException>(() => | ||
| handlers.AddTypedReceiveHandler(typeof(object), null, _ => true)); | ||
| Assert.Throws<InvalidOperationException>(() => | ||
| handlers.AddTypedReceiveHandler(typeof(int), null, _ => true)); | ||
| Assert.Throws<InvalidOperationException>(() => | ||
| handlers.AddGenericReceiveHandler<bool>(null, _ => true)); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Given_TypedReceiveHandlerWithPredicate_When_Adding_ReceiveAnyHandler_Then_Should_Succeed() | ||
| { | ||
| var handlers = new ReceiveActorHandlers(); | ||
| handlers.AddTypedReceiveHandler(typeof(object), _ => true, _ => true); | ||
|
|
||
| // As the object handler has a predicate, adding a ReceiveAny handler should be allowed | ||
| // as the object handler might not handle all objects. | ||
| handlers.AddReceiveAnyHandler(_ => { }); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Given_TypedReceiveHandler_When_Adding_SameTypedReceiveHandler_Then_Should_Fail() | ||
| { | ||
| var handlers = new ReceiveActorHandlers(); | ||
| handlers.AddTypedReceiveHandler(typeof(object), null, _ => true); | ||
|
|
||
| // As a handler for the type of object with no predicate is added, | ||
| // adding another handler for the same type combination should fail with an exception | ||
| Assert.Throws<InvalidOperationException>(() => | ||
| handlers.AddTypedReceiveHandler(typeof(object), null, _ => true)); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Given_TypedReceiveHandlerWithPredicate_When_Adding_SameTypedReceiveHandlerWithPredicate_Then_Should_Succeed() | ||
| { | ||
| var handlers = new ReceiveActorHandlers(); | ||
| handlers.AddTypedReceiveHandler(typeof(object), _ => true, _ => true); | ||
|
|
||
| // The handler added has a predicate which makes it uncertain if it will handle the message. | ||
| // Adding another handler for the same type combination should be allowed. | ||
| handlers.AddTypedReceiveHandler(typeof(object), null, _ => true); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Given_ObjectTypedReceiveHandlerWithNoPredicate_When_Adding_Any_Other_ReceiveHandler_Then_Should_Fail() | ||
| { | ||
| var handlers = new ReceiveActorHandlers(); | ||
| handlers.AddTypedReceiveHandler(typeof(object), null, _ => true); | ||
|
|
||
| // This should throw because the object handler is already added and would catch this before. | ||
| Assert.Throws<InvalidOperationException>(() => | ||
| handlers.AddTypedReceiveHandler(typeof(int), _ => true, _ => true)); | ||
| Assert.Throws<InvalidOperationException>(() => | ||
| handlers.AddGenericReceiveHandler<bool>(_ => true, _ => true)); | ||
| } | ||
|
|
||
| // TODO Confirm use case - This is theoretically a breaking change. Conceptually it should not be because Object handler | ||
| // with no predicate is the same as a ReceiveAny handler. | ||
| [Fact] | ||
| public void Given_ObjectTypedReceiveHandlerWithNoPredicate_When_Adding_AnyReceiveHandler_Then_Should_Fail() | ||
| { | ||
| var handlers = new ReceiveActorHandlers(); | ||
| handlers.AddTypedReceiveHandler(typeof(object), null, _ => true); | ||
|
|
||
| // This should throw because the object handler is already added and would catch this before. | ||
| Assert.Throws<InvalidOperationException>(() => | ||
| handlers.AddReceiveAnyHandler(_ => { })); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Given_GenericReceiveHandlerWithPredicate_When_Adding_SameGenericReceiveHandlerWithPredicate_Then_Should_Succeed() | ||
| { | ||
| var handlers = new ReceiveActorHandlers(); | ||
| handlers.AddGenericReceiveHandler<int>(_ => true, _ => true); | ||
|
|
||
| // The handler added has a predicate which makes it uncertain if it will handle the message. | ||
| // Adding another handler for the same type combination should be allowed. | ||
| handlers.AddGenericReceiveHandler<int>(null, _ => true); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Given_TypedReceiveHandler_When_Adding_DifferentTypedReceiveHandler_Then_Should_Succeed() | ||
| { | ||
| var handlers = new ReceiveActorHandlers(); | ||
| handlers.AddTypedReceiveHandler(typeof(string), _ => true, _ => true); | ||
|
|
||
| handlers.AddTypedReceiveHandler(typeof(int), _ => true, _ => true); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Given_GenericReceiveHandler_When_Adding_DifferentGenericReceiveHandler_Then_Should_Succeed() | ||
| { | ||
| var handlers = new ReceiveActorHandlers(); | ||
| handlers.AddGenericReceiveHandler<string>(null, _ => true); | ||
|
|
||
| handlers.AddGenericReceiveHandler<int>(_ => true, _ => true); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Given_TypedReceiveHandlerWithPredicate_When_Adding_DifferentTypedReceiveHandlerWithPredicate_Then_Should_Succeed() | ||
| { | ||
| var handlers = new ReceiveActorHandlers(); | ||
| handlers.AddTypedReceiveHandler(typeof(object), _ => true, _ => true); | ||
|
|
||
| // This should be allowed because the object handler is already but it has a predicate that might not match. | ||
| handlers.AddTypedReceiveHandler(typeof(int), _ => true, _ => true); | ||
| } | ||
|
|
||
| /* | ||
| * IFoo | ||
| * Bar: IFoo | ||
| * | ||
| * Receive<IFoo> | ||
| * Receive<Bar> | ||
| */ | ||
|
|
||
| private interface IFoo { } | ||
| private class Bar : IFoo { } | ||
| private class Baz : IFoo { } | ||
|
|
||
| private static readonly Predicate<IFoo> FooPredicate = _ => true; | ||
| private static readonly Predicate<Baz> BazPredicate = _ => true; | ||
|
|
||
| [Theory] | ||
| [InlineData(true)] | ||
| [InlineData(false)] | ||
| public void Given_TypedReceiveHandler_can_match_interface_on_ConcreteTypes(bool usePredicate) | ||
| { | ||
| var handlers1 = new ReceiveActorHandlers(); | ||
|
|
||
| var setBaz = false; | ||
| Func<Baz, bool> bazHandler = _ => | ||
| { | ||
| setBaz = true; | ||
| return true; | ||
| }; | ||
|
|
||
| var setInterface = false; | ||
| var interfaceHandler = new Func<IFoo, bool>(_ => | ||
| { | ||
| setInterface = true; | ||
| return true; | ||
| }); | ||
|
|
||
| // ensure that the interface handler is called when a concrete type is passed | ||
| handlers1.AddGenericReceiveHandler(usePredicate ? FooPredicate : null, interfaceHandler); | ||
|
|
||
| handlers1.TryHandle(new Bar()); | ||
| Assert.True(setInterface); | ||
|
|
||
| // now add the Baz handler | ||
| setInterface = false; // reset | ||
| handlers1.AddGenericReceiveHandler(usePredicate ? BazPredicate : null, bazHandler); | ||
|
|
||
| // demonstrate the matcher ordering is preserved - interface handler should still be called | ||
| handlers1.TryHandle(new Baz()); | ||
| Assert.False(setBaz); | ||
| Assert.True(setInterface); | ||
|
|
||
| // reset | ||
| setInterface = false; | ||
|
|
||
| // create a new match handler | ||
| var handlers2 = new ReceiveActorHandlers(); | ||
|
|
||
| // set in a "correct" / non-greedy order | ||
| handlers2.AddGenericReceiveHandler(usePredicate ? BazPredicate : null, bazHandler); | ||
| handlers2.AddGenericReceiveHandler(usePredicate ? FooPredicate : null, interfaceHandler); | ||
|
|
||
| // demonstrate the matcher ordering is preserved - Baz handler should be called | ||
| handlers2.TryHandle(new Baz()); | ||
|
|
||
| Assert.True(setBaz); | ||
| Assert.False(setInterface); | ||
|
|
||
| // reset | ||
| setBaz = false; | ||
|
|
||
| // handle Bar | ||
| handlers2.TryHandle(new Bar()); | ||
|
|
||
| // demonstrate the matcher ordering is preserved - interface handler should still be called | ||
| Assert.True(setInterface); | ||
| Assert.False(setBaz); // just a sanity check | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This spec at the bottom covers two scenarios:
Receive<T>declaration orderType-assignability code from ReceiveActor - Removing MatchBuilder and making use of handlers. #7498 works