Skip to content

Add MS Test support to Akka.Hosting.TestKit #432

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

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions Akka.Hosting.sln
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,20 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Akka.Persistence.Hosting.Te
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Akka.Hosting.LoggingDemo", "src\Examples\Akka.Hosting.LoggingDemo\Akka.Hosting.LoggingDemo.csproj", "{4F79325B-9EE7-4501-800F-7A1F8DFBCC80}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Akka.Hosting.TestKit", "src\Akka.Hosting.TestKit\Akka.Hosting.TestKit.csproj", "{E28D4F3C-6C34-497B-BDC8-F2B3EA8BA309}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Akka.Hosting.TestKit.Xunit2", "src\Akka.Hosting.TestKit.Xunit2\Akka.Hosting.TestKit.Xunit2.csproj", "{E28D4F3C-6C34-497B-BDC8-F2B3EA8BA309}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Akka.Hosting.TestKit.Tests", "src\Akka.Hosting.TestKit.Tests\Akka.Hosting.TestKit.Tests.csproj", "{3883AD08-B981-4943-8153-1E7FFD2C3127}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Akka.Hosting.TestKit.Xunit2.Tests", "src\Akka.Hosting.TestKit.Xunit2.Tests\Akka.Hosting.TestKit.Xunit2.Tests.csproj", "{3883AD08-B981-4943-8153-1E7FFD2C3127}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Akka.Hosting.API.Tests", "src\Akka.Hosting.API.Tests\Akka.Hosting.API.Tests.csproj", "{410065E0-6636-4F34-85AE-4C4BA413ADB4}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Akka.Remote.Hosting.Tests", "src\Akka.Remote.Hosting.Tests\Akka.Remote.Hosting.Tests.csproj", "{11062839-0674-4DF9-AB8B-B7901A564ADC}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Akka.Hosting.TestKit.MsTest", "src\Akka.Hosting.TestKit.MsTest\Akka.Hosting.TestKit.MsTest.csproj", "{6AC61E0C-D5D4-4E9D-8439-A6AD33140F7A}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Akka.Hosting.TestKit.MsTest.Tests", "src\Akka.Hosting.TestKit.MsTest.Tests\Akka.Hosting.TestKit.MsTest.Tests.csproj", "{9CF6226E-DD91-433D-9B9E-D1212B727251}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Akka.Hosting.TestKit", "src\Akka.Hosting.TestKit\Akka.Hosting.TestKit.csproj", "{0257FD4C-ABE9-4F05-87DB-67C9F6625912}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -106,6 +112,18 @@ Global
{11062839-0674-4DF9-AB8B-B7901A564ADC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{11062839-0674-4DF9-AB8B-B7901A564ADC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{11062839-0674-4DF9-AB8B-B7901A564ADC}.Release|Any CPU.Build.0 = Release|Any CPU
{6AC61E0C-D5D4-4E9D-8439-A6AD33140F7A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6AC61E0C-D5D4-4E9D-8439-A6AD33140F7A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6AC61E0C-D5D4-4E9D-8439-A6AD33140F7A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6AC61E0C-D5D4-4E9D-8439-A6AD33140F7A}.Release|Any CPU.Build.0 = Release|Any CPU
{9CF6226E-DD91-433D-9B9E-D1212B727251}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9CF6226E-DD91-433D-9B9E-D1212B727251}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9CF6226E-DD91-433D-9B9E-D1212B727251}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9CF6226E-DD91-433D-9B9E-D1212B727251}.Release|Any CPU.Build.0 = Release|Any CPU
{0257FD4C-ABE9-4F05-87DB-67C9F6625912}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0257FD4C-ABE9-4F05-87DB-67C9F6625912}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0257FD4C-ABE9-4F05-87DB-67C9F6625912}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0257FD4C-ABE9-4F05-87DB-67C9F6625912}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

<ItemGroup>
<ProjectReference Include="..\Akka.Cluster.Hosting\Akka.Cluster.Hosting.csproj" />
<ProjectReference Include="..\Akka.Hosting.TestKit\Akka.Hosting.TestKit.csproj" />
<ProjectReference Include="..\Akka.Hosting.TestKit.Xunit2\Akka.Hosting.TestKit.Xunit2.csproj" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

namespace Akka.Cluster.Hosting.Tests;

public class ClusterSingletonWithDiSpecs : Akka.Hosting.TestKit.TestKit
public class ClusterSingletonWithDiSpecs : Akka.Hosting.TestKit.Xunit2.TestKit
{
#region Actor and DI impls

Expand Down
2 changes: 1 addition & 1 deletion src/Akka.Hosting.API.Tests/Akka.Hosting.API.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

<ItemGroup>
<ProjectReference Include="..\Akka.Cluster.Hosting\Akka.Cluster.Hosting.csproj" />
<ProjectReference Include="..\Akka.Hosting.TestKit\Akka.Hosting.TestKit.csproj" />
<ProjectReference Include="..\Akka.Hosting.TestKit.Xunit2\Akka.Hosting.TestKit.Xunit2.csproj" />
<ProjectReference Include="..\Akka.Hosting\Akka.Hosting.csproj" />
<ProjectReference Include="..\Akka.Persistence.Hosting\Akka.Persistence.Hosting.csproj" />
<ProjectReference Include="..\Akka.Remote.Hosting\Akka.Remote.Hosting.csproj" />
Expand Down
2 changes: 1 addition & 1 deletion src/Akka.Hosting.API.Tests/CoreApiSpec.cs
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is causing one of the test failures, I am not sure what this is doing, or where to start on a fix so any direction would be very much appreciated.

Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public Task ApproveCore()
[Fact]
public Task ApproveTestKit()
{
return VerifyAssembly<TestKit.TestKit>();
return VerifyAssembly<TestKit.Xunit2.TestKit>();
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>$(TestsNetCoreFramework)</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="7.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="$(TestSdkVersion)" />
<PackageReference Include="FluentAssertions" Version="6.12.0" />
<PackageReference Include="MSTest.TestAdapter" Version="3.2.1" />
<PackageReference Include="MSTest.TestFramework" Version="$(MsTestVersion)" />
<PackageReference Include="Microsoft.TestPlatform.TestHost" Version="$(MicrosoftTestPlatformTestHostVersion)" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Akka.Hosting.TestKit.MsTest\Akka.Hosting.TestKit.MsTest.csproj" />
</ItemGroup>

</Project>
44 changes: 44 additions & 0 deletions src/Akka.Hosting.TestKit.MsTest.Tests/DiPropsFailTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// -----------------------------------------------------------------------
// <copyright file="DiPropsFailTest.cs" company="Akka.NET Project">
// Copyright (C) 2013-2023 .NET Foundation <https://github.com/akkadotnet/akka.net>
// </copyright>
// -----------------------------------------------------------------------

using Akka.DependencyInjection;
using Microsoft.Extensions.Logging;

namespace Akka.Hosting.TestKit.MsTest.Tests;

// Regression test for https://github.com/akkadotnet/Akka.Hosting/issues/343

[TestClass]
public class DiPropsFailTest: TestKit
{
public DiPropsFailTest() : base(nameof(DiPropsFailTest))
{}

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

[TestMethod]
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);
});
}
}
}
7 changes: 7 additions & 0 deletions src/Akka.Hosting.TestKit.MsTest.Tests/GlobalUsings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
global using Akka.Actor;
global using FluentAssertions;
global using Microsoft.VisualStudio.TestTools.UnitTesting;
global using System;
global using System.Collections;
global using System.Threading.Tasks;
global using static FluentAssertions.FluentActions;
57 changes: 57 additions & 0 deletions src/Akka.Hosting.TestKit.MsTest.Tests/HostingSpecSpec.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// -----------------------------------------------------------------------
// <copyright file="HostingSpecSpec.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 Akka.Event;
using LogLevel = Microsoft.Extensions.Logging.LogLevel;

namespace Akka.Hosting.TestKit.MsTest.Tests;

[TestClass]
public class HostingSpecSpec: TestKit
{
private enum Echo
{ }

public HostingSpecSpec()
: base(nameof(HostingSpecSpec), logLevel: LogLevel.Debug)
{
}

protected override void ConfigureAkka(AkkaConfigurationBuilder builder, IServiceProvider provider)
{
builder.WithActors((system, registry) =>
{
var echo = system.ActorOf(Props.Create(() => new SimpleEchoActor()));
registry.Register<Echo>(echo);
});
}

[TestMethod]
public void ActorTest()
{
var echo = ActorRegistry.Get<Echo>();
var probe = CreateTestProbe();

echo.Tell("TestMessage", probe);
var msg = probe.ExpectMsg("TestMessage");
Log.Info(msg);
}

private class SimpleEchoActor : ReceiveActor
{
public SimpleEchoActor()
{
var log = Context.GetLogger();

ReceiveAny(msg =>
{
log.Info($"Received {msg}");
Sender.Tell(msg);
});
}
}
}
79 changes: 79 additions & 0 deletions src/Akka.Hosting.TestKit.MsTest.Tests/LoggerSpec.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// -----------------------------------------------------------------------
// <copyright file="LoggerSpec.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 Akka.Event;
using Akka.Hosting.TestKit.Internals;
using LogLevel = Microsoft.Extensions.Logging.LogLevel;

namespace Akka.Hosting.TestKit.MsTest.Tests;

[TestClass]
public class LoggerSpec: TestKit
{
protected override bool ShouldUseCustomLogger => true;

public LoggerSpec(): base(logLevel: LogLevel.Debug)
{
}

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

protected override async Task LoggerHook(ActorSystem system, IActorRegistry registry)
{
var extSystem = (ExtendedActorSystem)system;
var logger = extSystem.SystemActorOf(Props.Create(() => new MockLogger()), "log-test");
registry.Register<MockLogger>(logger);
await logger.Ask<LoggerInitialized>(new InitializeLogger(system.EventStream));
}

[TestMethod(displayName: "TestKit ILoggerFactory logger should log messages")]
public void TestKitLoggerFactoryLoggerTest()
{
var loggerActor = ActorRegistry.Get<MockLogger>();
loggerActor.Tell(TestActor);

var logger = Event.Logging.GetLogger(Sys, "log-test");

logger.Debug("debug");
ExpectMsg<Debug>(i => i.Message.ToString() == "debug");

logger.Info("info");
ExpectMsg<Info>(i => i.Message.ToString() == "info");

logger.Warning("warn");
ExpectMsg<Warning>(i => i.Message.ToString() == "warn");

logger.Error("err");
ExpectMsg<Error>(i => i.Message.ToString() == "err");
}

private class MockLogger: TestKitLoggerFactoryLogger
{
private IActorRef? _probe;

protected override bool Receive(object message)
{
switch (message)
{
case IActorRef actor:
_probe = actor;
return true;
default:
return base.Receive(message);
}
}

protected override void Log(LogEvent log, ActorPath path)
{
if(log.LogSource.StartsWith("log-test"))
_probe.Tell(log);
base.Log(log, path);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,20 @@
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Threading.Tasks;
using Akka.Actor;
using Akka.Actor.Dsl;
using Akka.TestKit;
using FluentAssertions;
using Xunit;

namespace Akka.Hosting.TestKit.Tests;
namespace Akka.Hosting.TestKit.MsTest.Tests;

[TestClass]
public class NoImplicitSenderSpec : TestKit, INoImplicitSender
{
protected override void ConfigureAkka(AkkaConfigurationBuilder builder, IServiceProvider provider)
{

}

[Fact]
[TestMethod]
public void When_Not_ImplicitSender_then_testActor_is_not_sender()
{
var echoActor = Sys.ActorOf(c => c.ReceiveAny((m, ctx) => TestActor.Tell(ctx.Sender)));
Expand All @@ -33,14 +29,15 @@ public void When_Not_ImplicitSender_then_testActor_is_not_sender()

}

[TestClass]
public class ImplicitSenderSpec : TestKit
{
protected override void ConfigureAkka(AkkaConfigurationBuilder builder, IServiceProvider provider)
{

}

[Fact]
[TestMethod]
public void ImplicitSender_should_have_testActor_as_sender()
{
var echoActor = Sys.ActorOf(c => c.ReceiveAny((m, ctx) => TestActor.Tell(ctx.Sender)));
Expand All @@ -54,7 +51,7 @@ public void ImplicitSender_should_have_testActor_as_sender()
}


[Fact]
[TestMethod]
public void ImplicitSender_should_not_change_when_creating_Testprobes()
{
//Verifies that bug #459 has been fixed
Expand All @@ -64,7 +61,7 @@ public void ImplicitSender_should_not_change_when_creating_Testprobes()
LastSender.Should().Be(TestActor);
}

[Fact]
[TestMethod]
public void ImplicitSender_should_not_change_when_creating_TestActors()
{
var testActor2 = CreateTestActor("test2");
Expand Down
31 changes: 31 additions & 0 deletions src/Akka.Hosting.TestKit.MsTest.Tests/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//-----------------------------------------------------------------------
// <copyright file="AssemblyInfo.cs" company="Akka.NET Project">
// Copyright (C) 2009-2021 Lightbend Inc. <http://www.lightbend.com>
// Copyright (C) 2013-2021 .NET Foundation <https://github.com/akkadotnet/akka.net>
// </copyright>
//-----------------------------------------------------------------------

using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("b21496c0-a536-4953-9253-d2d0d526e42d")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
Loading