-
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
Added reproduction spec for missing custom mailbox when using DI #7402
Merged
+185
−59
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
109 changes: 109 additions & 0 deletions
109
src/contrib/dependencyinjection/Akka.DependencyInjection.Tests/ActorTestServices.cs
This file contains 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,109 @@ | ||
// ----------------------------------------------------------------------- | ||
// <copyright file="ActorTestServices.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; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Akka.Actor; | ||
using Akka.Configuration; | ||
using Akka.Dispatch; | ||
using Akka.TestKit; | ||
using Akka.Util.Internal; | ||
using Microsoft.Extensions.Hosting; | ||
|
||
namespace Akka.DependencyInjection.Tests; | ||
|
||
internal class TestDiActor : ReceiveActor | ||
{ | ||
public static readonly AtomicCounter Counter = new(0); | ||
|
||
public TestDiActor(InjectedService injected) | ||
{ | ||
long count = Counter.GetAndIncrement(); | ||
Receive<GetMessage>(_ => Sender.Tell(new Message { Value = injected.Message, Counter = count })); | ||
} | ||
} | ||
|
||
internal class InjectedEchoActor : ReceiveActor | ||
{ | ||
public InjectedEchoActor(InjectedService injected, string thing) | ||
{ | ||
Receive<string>(str => Sender.Tell(new Message { Value = injected.Message + thing, Counter = 0 })); | ||
Receive<int>(i => Sender.Tell(new Message { Value = injected.Message + thing, Counter = 0 })); | ||
} | ||
} | ||
|
||
internal class Message | ||
{ | ||
public string Value { get; set; } | ||
public long Counter { get; set; } | ||
} | ||
|
||
internal class GetMessage | ||
{ | ||
public static readonly GetMessage Instance = new(); | ||
|
||
private GetMessage() | ||
{ | ||
} | ||
} | ||
|
||
internal class InjectedService | ||
{ | ||
public string Message => "I was injected"; | ||
} | ||
|
||
internal class CustomMailbox : UnboundedPriorityMailbox | ||
{ | ||
public CustomMailbox(Settings settings, Config config) : base(settings, config) | ||
{ | ||
} | ||
|
||
protected override int PriorityGenerator(object message) | ||
{ | ||
return message switch | ||
{ | ||
string => 1, | ||
int => 2, | ||
_ => 3 | ||
}; | ||
} | ||
} | ||
|
||
internal class AkkaService : IHostedService | ||
{ | ||
public const string CustomMailboxName = "custom-mailbox"; | ||
|
||
private readonly Config _customMailboxHocon = $$""" | ||
custom-mailbox { | ||
mailbox-type = "{{typeof(CustomMailbox).AssemblyQualifiedName}}" | ||
} | ||
"""; | ||
|
||
public ActorSystem ActorSystem { get; private set; } | ||
|
||
private readonly IServiceProvider _serviceProvider; | ||
|
||
public AkkaService(IServiceProvider serviceProvider) | ||
{ | ||
_serviceProvider = serviceProvider; | ||
} | ||
|
||
public Task StartAsync(CancellationToken cancellationToken) | ||
{ | ||
var setup = DependencyResolverSetup.Create(_serviceProvider) | ||
.And(BootstrapSetup.Create().WithConfig(_customMailboxHocon.WithFallback(TestKitBase.DefaultConfig))); | ||
|
||
ActorSystem = ActorSystem.Create("TestSystem", setup); | ||
return Task.CompletedTask; | ||
} | ||
|
||
public async Task StopAsync(CancellationToken cancellationToken = default) | ||
{ | ||
await ActorSystem.Terminate(); | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
src/contrib/dependencyinjection/Akka.DependencyInjection.Tests/DiPropsSpecs.cs
This file contains 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,73 @@ | ||
// ----------------------------------------------------------------------- | ||
// <copyright file="DiPropsSpecs.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; | ||
using System.Threading.Tasks; | ||
using Akka.Actor; | ||
using Akka.Configuration; | ||
using Akka.Dispatch; | ||
using Akka.Util.Internal; | ||
using FluentAssertions; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Xunit; | ||
using Xunit.Abstractions; | ||
|
||
namespace Akka.DependencyInjection.Tests; | ||
|
||
public class DiPropsSpecs : IAsyncLifetime | ||
{ | ||
private readonly IServiceProvider _serviceProvider; | ||
private readonly AkkaService _akkaService; | ||
private readonly ITestOutputHelper _output; | ||
private TestKit.Xunit2.TestKit _testKit; | ||
|
||
public DiPropsSpecs(ITestOutputHelper output) | ||
{ | ||
_output = output; | ||
var services = new ServiceCollection() | ||
.AddSingleton<InjectedService>() | ||
.AddSingleton<AkkaService>() | ||
.AddHostedService<AkkaService>(); | ||
|
||
_serviceProvider = services.BuildServiceProvider(); | ||
_akkaService = _serviceProvider.GetRequiredService<AkkaService>(); | ||
} | ||
|
||
|
||
[Fact(DisplayName = "DI should work with custom mailbox")] | ||
public async Task ShouldWorkWithCustomMailbox() | ||
{ | ||
var system = _serviceProvider.GetRequiredService<AkkaService>().ActorSystem; | ||
var resolver = DependencyResolver.For(system); | ||
const string thing = "foobar"; | ||
var props = resolver.Props<InjectedEchoActor>(thing).WithMailbox(AkkaService.CustomMailboxName); | ||
var actor = system.ActorOf(props, "testDIActorWithCustomMailbox"); | ||
|
||
var probe = _testKit.CreateTestProbe(system); | ||
actor.Tell(1, probe); | ||
actor.Tell("test", probe); | ||
var result1 = await probe.ExpectMsgAsync<Message>(); | ||
result1.Value.Should().Be("I was injected" + thing); | ||
var result2 = await probe.ExpectMsgAsync<Message>(); | ||
result2.Value.Should().Be("I was injected" + thing); | ||
|
||
// Verify that the custom mailbox was used | ||
var actorRef = (RepointableActorRef)actor; | ||
actorRef.MailboxType.GetType().Should().Be(typeof(CustomMailbox)); | ||
} | ||
|
||
public async Task InitializeAsync() | ||
{ | ||
await _akkaService.StartAsync(default); | ||
_testKit = new TestKit.Xunit2.TestKit(_akkaService.ActorSystem, _output); | ||
} | ||
|
||
public async Task DisposeAsync() | ||
{ | ||
await _akkaService.StopAsync(); | ||
} | ||
} |
This file contains 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
This file contains 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 |
---|---|---|
|
@@ -10,6 +10,7 @@ | |
[assembly: System.Runtime.CompilerServices.InternalsVisibleToAttribute("Akka.Cluster.Tests.MultiNode")] | ||
[assembly: System.Runtime.CompilerServices.InternalsVisibleToAttribute("Akka.Cluster.Tools")] | ||
[assembly: System.Runtime.CompilerServices.InternalsVisibleToAttribute("Akka.DependencyInjection")] | ||
[assembly: System.Runtime.CompilerServices.InternalsVisibleToAttribute("Akka.DependencyInjection.Tests")] | ||
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. Had to expose friend assembly bits here in order to make the test workable, and that's what triggered the API check-in. |
||
[assembly: System.Runtime.CompilerServices.InternalsVisibleToAttribute("Akka.DistributedData")] | ||
[assembly: System.Runtime.CompilerServices.InternalsVisibleToAttribute("Akka.Docs.Tests")] | ||
[assembly: System.Runtime.CompilerServices.InternalsVisibleToAttribute("Akka.MultiNodeTestRunner.Shared.Tests")] | ||
|
This file contains 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
This file contains 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
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.
Fixed how we assert for the presence of the custom mailbox to match what we do in the core AkkaSpecs and this passes now - I was also able to verify with the debugger that the custom mailbox was used.