Skip to content

Commit

Permalink
Fix missing actor context on test start (#346)
Browse files Browse the repository at this point in the history
  • Loading branch information
Arkatufus committed Aug 1, 2023
1 parent 5bd2832 commit 84886fa
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 86 deletions.
46 changes: 46 additions & 0 deletions src/Akka.Hosting.TestKit.Tests/DiPropsFailTest.cs
@@ -0,0 +1,46 @@
// -----------------------------------------------------------------------
// <copyright file="DiPropsFailTest.cs" company="Akka.NET Project">
// Copyright (C) 2013-2023 .NET Foundation <https://github.com/akkadotnet/akka.net>
// </copyright>
// -----------------------------------------------------------------------

using System;
using Akka.Actor;
using Akka.DependencyInjection;
using Microsoft.Extensions.Logging;
using Xunit;
using Xunit.Abstractions;

namespace Akka.Hosting.TestKit.Tests;

// Regression test for https://github.com/akkadotnet/Akka.Hosting/issues/343
public class DiPropsFailTest: TestKit
{
public DiPropsFailTest(ITestOutputHelper output) : base(nameof(DiPropsFailTest), output)
{}

protected override void ConfigureAkka(AkkaConfigurationBuilder builder, IServiceProvider provider)
{ }

[Fact]
public void DiTest()
{
var actor = Sys.ActorOf(NonRootActorWithDi.Props());
actor.Tell("test");
ExpectMsg<string>("test");
}

private class NonRootActorWithDi: ReceiveActor
{
public static Props Props() => DependencyResolver.For(Context.System).Props<NonRootActorWithDi>();

public NonRootActorWithDi(ILogger<NonRootActorWithDi> log)
{
ReceiveAny(msg =>
{
log.LogInformation("Received {Msg}", msg);
Sender.Tell(msg);
});
}
}
}
83 changes: 0 additions & 83 deletions src/Akka.Hosting.TestKit/ActorCellKeepingSynchronizationContext.cs

This file was deleted.

18 changes: 15 additions & 3 deletions src/Akka.Hosting.TestKit/TestKit.cs
Expand Up @@ -8,6 +8,7 @@
using System.Threading;
using System.Threading.Tasks;
using Akka.Actor;
using Akka.Actor.Internal;
using Akka.Actor.Setup;
using Akka.Annotations;
using Akka.Configuration;
Expand Down Expand Up @@ -53,7 +54,7 @@ public IHost Host
public ITestOutputHelper? Output { get; }
public LogLevel LogLevel { get; }

private TaskCompletionSource<Done> _initialized = new TaskCompletionSource<Done>();
private readonly TaskCompletionSource<Done> _initialized = new TaskCompletionSource<Done>();

protected TestKit(string? actorSystemName = null, ITestOutputHelper? output = null, TimeSpan? startupTimeout = null, LogLevel logLevel = LogLevel.Information)
: base(Assertions)
Expand Down Expand Up @@ -102,8 +103,15 @@ private void InternalConfigureServices(HostBuilderContext context, IServiceColle
builder.AddStartup((system, _) =>
{
base.InitializeTest(system, (ActorSystemSetup)null!, null, null);
_initialized.SetResult(Done.Instance);
try
{
base.InitializeTest(system, (ActorSystemSetup)null!, null, null);
_initialized.SetResult(Done.Instance);
}
catch (Exception e)
{
_initialized.SetException(e);
}
});
});
}
Expand Down Expand Up @@ -154,6 +162,10 @@ public async Task InitializeAsync()
}

await _initialized.Task;

if (this is not INoImplicitSender && InternalCurrentActorCellKeeper.Current is null)
InternalCurrentActorCellKeeper.Current = (ActorCell)((ActorRefWithCell)TestActor).Underlying;

await BeforeTestStart();
}

Expand Down

0 comments on commit 84886fa

Please sign in to comment.