-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Proper Akka.NET Dependency Injection:
IRequiredActor<TActor>
(#170)
* created a proper DI implementation for actors * completed end to end DI prototype continuation of #144 * added negative test case
- Loading branch information
1 parent
40671c2
commit a91b72d
Showing
3 changed files
with
144 additions
and
3 deletions.
There are no files selected for viewing
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,108 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using Akka.Actor; | ||
using FluentAssertions; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Hosting; | ||
using Xunit; | ||
|
||
namespace Akka.Hosting.Tests; | ||
|
||
public class RequiredActorSpecs | ||
{ | ||
public sealed class MyActorType : ReceiveActor | ||
{ | ||
public MyActorType() | ||
{ | ||
ReceiveAny(_ => Sender.Tell(_)); | ||
} | ||
} | ||
|
||
public sealed class MyConsumer | ||
{ | ||
private readonly IActorRef _actor; | ||
|
||
public MyConsumer(IRequiredActor<MyActorType> actor) | ||
{ | ||
_actor = actor.ActorRef; | ||
} | ||
|
||
public async Task<string> Say(string word) | ||
{ | ||
return await _actor.Ask<string>(word, TimeSpan.FromSeconds(3)); | ||
} | ||
} | ||
|
||
public class MissingActor{} | ||
|
||
public sealed class BadConsumer | ||
{ | ||
private readonly IActorRef _actor; | ||
|
||
public BadConsumer(IRequiredActor<MissingActor> actor) | ||
{ | ||
_actor = actor.ActorRef; | ||
} | ||
|
||
public async Task<string> Say(string word) | ||
{ | ||
return await _actor.Ask<string>(word, TimeSpan.FromSeconds(3)); | ||
} | ||
} | ||
|
||
[Fact] | ||
public async Task ShouldRetrieveRequiredActorFromIServiceProvider() | ||
{ | ||
// arrange | ||
using var host = new HostBuilder() | ||
.ConfigureServices(services => | ||
{ | ||
services.AddAkka("MySys", (builder, provider) => | ||
{ | ||
builder.WithActors((system, registry) => | ||
{ | ||
var actor = system.ActorOf(Props.Create(() => new MyActorType()), "myactor"); | ||
registry.Register<MyActorType>(actor); | ||
}); | ||
}); | ||
services.AddScoped<MyConsumer>(); | ||
}) | ||
.Build(); | ||
await host.StartAsync(); | ||
|
||
// act | ||
var myConsumer = host.Services.GetRequiredService<MyConsumer>(); | ||
var input = "foo"; | ||
var spoken = await myConsumer.Say(input); | ||
|
||
// assert | ||
spoken.Should().Be(input); | ||
} | ||
|
||
[Fact] | ||
public async Task ShouldFailRetrieveRequiredActorWhenNotDefined() | ||
{ | ||
// arrange | ||
using var host = new HostBuilder() | ||
.ConfigureServices(services => | ||
{ | ||
services.AddAkka("MySys", (builder, provider) => | ||
{ | ||
builder.WithActors((system, registry) => | ||
{ | ||
var actor = system.ActorOf(Props.Create(() => new MyActorType()), "myactor"); | ||
registry.Register<MyActorType>(actor); | ||
}); | ||
}); | ||
services.AddScoped<BadConsumer>(); | ||
}) | ||
.Build(); | ||
await host.StartAsync(); | ||
|
||
// act | ||
Action shouldThrow = () => host.Services.GetRequiredService<BadConsumer>(); | ||
|
||
// assert | ||
shouldThrow.Should().Throw<MissingActorRegistryEntryException>(); | ||
} | ||
} |
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