-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adding
ClusterSingleton
hosting methods (#51)
* adding `ClusterSingleton` hosting methods * fixed compilation errors * added test output logger * fixed package metadata issue * fixed bug with singleton proxy startup * made tests more configurable * working on fixing specs * fixed ClusterSingletonProxy configuration * fixed bug with singleton and proxy method * Update AkkaClusterHostingExtensions.cs
- Loading branch information
1 parent
36363ac
commit a7ad10e
Showing
4 changed files
with
277 additions
and
2 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
17 changes: 17 additions & 0 deletions
17
src/Akka.Cluster.Hosting.Tests/Akka.Cluster.Hosting.Tests.csproj
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,17 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<TargetFramework>$(TestsNetCoreFramework)</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Akka.TestKit.Xunit2" Version="$(AkkaVersion)" /> | ||
<PackageReference Include="FluentAssertions" Version="6.7.0" /> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="$(TestSdkVersion)" /> | ||
<PackageReference Include="xunit" Version="$(XunitVersion)" /> | ||
<PackageReference Include="xunit.runner.visualstudio" Version="$(XunitVersion)" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Akka.Cluster.Hosting\Akka.Cluster.Hosting.csproj" /> | ||
</ItemGroup> | ||
</Project> |
130 changes: 130 additions & 0 deletions
130
src/Akka.Cluster.Hosting.Tests/ClusterSingletonSpecs.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,130 @@ | ||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Akka.Actor; | ||
using Akka.Event; | ||
using Akka.Hosting; | ||
using Akka.Remote.Hosting; | ||
using Akka.TestKit.Xunit2.Internals; | ||
using FluentAssertions; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Hosting; | ||
using Xunit; | ||
using Xunit.Abstractions; | ||
|
||
namespace Akka.Cluster.Hosting.Tests; | ||
|
||
public class ClusterSingletonSpecs | ||
{ | ||
public ClusterSingletonSpecs(ITestOutputHelper output) | ||
{ | ||
Output = output; | ||
} | ||
|
||
public ITestOutputHelper Output { get; } | ||
|
||
private class MySingletonActor : ReceiveActor | ||
{ | ||
public static Props MyProps => Props.Create(() => new MySingletonActor()); | ||
|
||
public MySingletonActor() | ||
{ | ||
ReceiveAny(_ => Sender.Tell(_)); | ||
} | ||
} | ||
|
||
private async Task<IHost> CreateHost(Action<AkkaConfigurationBuilder> specBuilder, ClusterOptions options) | ||
{ | ||
var tcs = new TaskCompletionSource(); | ||
using var cancellationTokenSource = new CancellationTokenSource(TimeSpan.FromSeconds(10)); | ||
|
||
var host = new HostBuilder() | ||
.ConfigureServices(collection => | ||
{ | ||
collection.AddAkka("TestSys", (configurationBuilder, provider) => | ||
{ | ||
configurationBuilder | ||
.WithRemoting("localhost", 0) | ||
.WithClustering(options) | ||
.WithActors((system, registry) => | ||
{ | ||
var extSystem = (ExtendedActorSystem)system; | ||
var logger = extSystem.SystemActorOf(Props.Create(() => new TestOutputLogger(Output)), "log-test"); | ||
logger.Tell(new InitializeLogger(system.EventStream)); | ||
}) | ||
.WithActors(async (system, registry) => | ||
{ | ||
var cluster = Cluster.Get(system); | ||
cluster.RegisterOnMemberUp(() => | ||
{ | ||
tcs.SetResult(); | ||
}); | ||
if (options.SeedNodes == null || options.SeedNodes.Length == 0) | ||
{ | ||
var myAddress = cluster.SelfAddress; | ||
await cluster.JoinAsync(myAddress); // force system to wait until we're up | ||
} | ||
}); | ||
specBuilder(configurationBuilder); | ||
}); | ||
}).Build(); | ||
|
||
await host.StartAsync(cancellationTokenSource.Token); | ||
await (tcs.Task.WaitAsync(cancellationTokenSource.Token)); | ||
|
||
return host; | ||
} | ||
|
||
[Fact] | ||
public async Task Should_launch_ClusterSingletonAndProxy() | ||
{ | ||
// arrange | ||
using var host = await CreateHost( | ||
builder => { builder.WithSingleton<MySingletonActor>("my-singleton", MySingletonActor.MyProps); }, | ||
new ClusterOptions(){ Roles = new[] { "my-host"}}); | ||
|
||
var registry = host.Services.GetRequiredService<ActorRegistry>(); | ||
var singletonProxy = registry.Get<MySingletonActor>(); | ||
|
||
// act | ||
|
||
// verify round-trip to the singleton proxy and back | ||
var respond = await singletonProxy.Ask<string>("hit", TimeSpan.FromSeconds(3)); | ||
|
||
// assert | ||
respond.Should().Be("hit"); | ||
|
||
await host.StopAsync(); | ||
} | ||
|
||
[Fact] | ||
public async Task Should_launch_ClusterSingleton_and_Proxy_separately() | ||
{ | ||
// arrange | ||
|
||
var singletonOptions = new ClusterSingletonOptions() { Role = "my-host" }; | ||
using var singletonHost = await CreateHost( | ||
builder => { builder.WithSingleton<MySingletonActor>("my-singleton", MySingletonActor.MyProps, singletonOptions, createProxyToo:false); }, | ||
new ClusterOptions(){ Roles = new[] { "my-host"}}); | ||
|
||
var singletonSystem = singletonHost.Services.GetRequiredService<ActorSystem>(); | ||
var address = Cluster.Get(singletonSystem).SelfAddress; | ||
|
||
using var singletonProxyHost = await CreateHost( | ||
builder => { builder.WithSingletonProxy<MySingletonActor>("my-singleton", singletonOptions); }, | ||
new ClusterOptions(){ Roles = new[] { "proxy" }, SeedNodes = new Address[]{ address } }); | ||
|
||
var registry = singletonProxyHost.Services.GetRequiredService<ActorRegistry>(); | ||
var singletonProxy = registry.Get<MySingletonActor>(); | ||
|
||
// act | ||
|
||
// verify round-trip to the singleton proxy and back | ||
var respond = await singletonProxy.Ask<string>("hit", TimeSpan.FromSeconds(3)); | ||
|
||
// assert | ||
respond.Should().Be("hit"); | ||
|
||
await Task.WhenAll(singletonHost.StopAsync(), singletonProxyHost.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