-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[DI] DI fails to throw an exception when DI tried to create an actor …
…with missing constructor parameter (#5735) * Add bug spec for DI bug * Fix unit test to reflect the correct failure behaviour
- Loading branch information
Showing
1 changed file
with
101 additions
and
0 deletions.
There are no files selected for viewing
101 changes: 101 additions & 0 deletions
101
src/contrib/dependencyinjection/Akka.DependencyInjection.Tests/BugFixSpec.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,101 @@ | ||
// //----------------------------------------------------------------------- | ||
// // <copyright file="BugFix.cs" company="Akka.NET Project"> | ||
// // Copyright (C) 2009-2022 Lightbend Inc. <http://www.lightbend.com> | ||
// // Copyright (C) 2013-2022 .NET Foundation <https://github.com/akkadotnet/akka.net> | ||
// // </copyright> | ||
// //----------------------------------------------------------------------- | ||
|
||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Akka.Actor; | ||
using Akka.Event; | ||
using Akka.TestKit; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Hosting; | ||
using Xunit; | ||
using Xunit.Abstractions; | ||
using FluentAssertions; | ||
using static FluentAssertions.FluentActions; | ||
|
||
namespace Akka.DependencyInjection.Tests | ||
{ | ||
public class BugFixSpec: AkkaSpec, IAsyncLifetime | ||
{ | ||
private readonly IServiceProvider _serviceProvider; | ||
private readonly AkkaService _akkaService; | ||
|
||
public BugFixSpec(ITestOutputHelper output) : base(output) | ||
{ | ||
var services = new ServiceCollection() | ||
.AddSingleton<AkkaService>() | ||
.AddHostedService<AkkaService>(); | ||
|
||
_serviceProvider = services.BuildServiceProvider(); | ||
_akkaService = _serviceProvider.GetRequiredService<AkkaService>(); | ||
} | ||
|
||
[Fact(DisplayName = "DI should log an error if DI provider does not contain required parameter")] | ||
public void ShouldLogAnErrorIfParameterInjectionFailed() | ||
{ | ||
var system = _serviceProvider.GetRequiredService<AkkaService>().ActorSystem; | ||
var probe = CreateTestProbe(system); | ||
system.EventStream.Subscribe(probe, typeof(Error)); | ||
|
||
var props = DependencyResolver.For(system).Props<TestDiActor>(); | ||
var actor = system.ActorOf(props.WithDeploy(Deploy.Local), "testDIActor"); | ||
|
||
probe.ExpectMsg<Error>().Cause.Should().BeOfType<ActorInitializationException>(); | ||
} | ||
|
||
internal class TestDiActor : ReceiveActor | ||
{ | ||
public TestDiActor(NotInServices doesNotExistInDi) | ||
{ | ||
} | ||
} | ||
|
||
internal class NotInServices | ||
{ | ||
} | ||
|
||
|
||
public async Task InitializeAsync() | ||
{ | ||
await _akkaService.StartAsync(default); | ||
InitializeLogger(_akkaService.ActorSystem); | ||
} | ||
|
||
public async Task DisposeAsync() | ||
{ | ||
var sys = _serviceProvider.GetRequiredService<AkkaService>().ActorSystem; | ||
await sys.Terminate(); | ||
} | ||
|
||
internal class AkkaService : IHostedService | ||
{ | ||
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(TestKitBase.DefaultConfig)); | ||
|
||
ActorSystem = ActorSystem.Create("TestSystem", setup); | ||
return Task.CompletedTask; | ||
} | ||
|
||
public async Task StopAsync(CancellationToken cancellationToken) | ||
{ | ||
await ActorSystem.Terminate(); | ||
} | ||
} | ||
} | ||
} |