-
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.
Sharding add actor registry overloads (#70)
* Added `ActorRegistry` overloads for Akka.Cluster.Sharding * separated `TestHelper` from Akka.Cluster.Hosting.Tests * successfully tested new ActorRegistry-enabled methods
- Loading branch information
1 parent
67776ce
commit 639b905
Showing
4 changed files
with
227 additions
and
74 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,113 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
using Akka.Actor; | ||
using Akka.Cluster.Sharding; | ||
using Akka.Hosting; | ||
using FluentAssertions; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Xunit; | ||
using Xunit.Abstractions; | ||
|
||
namespace Akka.Cluster.Hosting.Tests; | ||
|
||
public class ClusterShardingSpecs | ||
{ | ||
public sealed class MyTopLevelActor : ReceiveActor | ||
{ | ||
} | ||
|
||
public sealed class MyEntityActor : ReceiveActor | ||
{ | ||
public MyEntityActor(string entityId, IActorRef sourceRef) | ||
{ | ||
EntityId = entityId; | ||
SourceRef = sourceRef; | ||
|
||
Receive<GetId>(g => { Sender.Tell(EntityId); }); | ||
Receive<GetSourceRef>(g => Sender.Tell(SourceRef)); | ||
} | ||
|
||
public string EntityId { get; } | ||
|
||
public IActorRef SourceRef { get; } | ||
|
||
public sealed class GetId : IWithId | ||
{ | ||
public GetId(string id) | ||
{ | ||
Id = id; | ||
} | ||
|
||
public string Id { get; } | ||
} | ||
|
||
public sealed class GetSourceRef : IWithId | ||
{ | ||
public GetSourceRef(string id) | ||
{ | ||
Id = id; | ||
} | ||
|
||
public string Id { get; } | ||
} | ||
} | ||
|
||
public interface IWithId | ||
{ | ||
string Id { get; } | ||
} | ||
|
||
public sealed class Extractor : HashCodeMessageExtractor | ||
{ | ||
public Extractor() : base(30) | ||
{ | ||
} | ||
|
||
public override string EntityId(object message) | ||
{ | ||
if (message is IWithId withId) | ||
return withId.Id; | ||
return string.Empty; | ||
} | ||
} | ||
|
||
public ClusterShardingSpecs(ITestOutputHelper output) | ||
{ | ||
Output = output; | ||
} | ||
|
||
public ITestOutputHelper Output { get; } | ||
|
||
[Fact] | ||
public async Task Should_use_ActorRegistry_with_ShardRegion() | ||
{ | ||
// arrange | ||
using var host = await TestHelper.CreateHost(builder => | ||
{ | ||
builder.WithActors((system, registry) => | ||
{ | ||
var tLevel = system.ActorOf(Props.Create(() => new MyTopLevelActor()), "toplevel"); | ||
registry.Register<MyTopLevelActor>(tLevel); | ||
}) | ||
.WithShardRegion<MyEntityActor>("entities", (system, registry) => | ||
{ | ||
var tLevel = registry.Get<MyTopLevelActor>(); | ||
return s => Props.Create(() => new MyEntityActor(s, tLevel)); | ||
}, new Extractor(), new ShardOptions() { Role = "my-host", StateStoreMode = StateStoreMode.DData }); | ||
}, new ClusterOptions() { Roles = new[] { "my-host" } }, Output); | ||
|
||
var actorSystem = host.Services.GetRequiredService<ActorSystem>(); | ||
var actorRegistry = ActorRegistry.For(actorSystem); | ||
var shardRegion = actorRegistry.Get<MyEntityActor>(); | ||
|
||
// act | ||
var id = await shardRegion.Ask<string>(new MyEntityActor.GetId("foo"), TimeSpan.FromSeconds(3)); | ||
var sourceRef = | ||
await shardRegion.Ask<IActorRef>(new MyEntityActor.GetSourceRef("foo"), TimeSpan.FromSeconds(3)); | ||
|
||
// assert | ||
id.Should().Be("foo"); | ||
sourceRef.Should().Be(actorRegistry.Get<MyTopLevelActor>()); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
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 Microsoft.Extensions.Hosting; | ||
using Xunit.Abstractions; | ||
|
||
namespace Akka.Cluster.Hosting.Tests; | ||
|
||
public static class TestHelper | ||
{ | ||
|
||
public static async Task<IHost> CreateHost(Action<AkkaConfigurationBuilder> specBuilder, ClusterOptions options, ITestOutputHelper output) | ||
{ | ||
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; | ||
} | ||
} |
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