Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
4 changes: 2 additions & 2 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ A basic lambda function can handle it - the only reason this function is as comp
<XunitVersion>2.8.1</XunitVersion>
<TestSdkVersion>17.11.1</TestSdkVersion>
<CoverletVersion>6.0.3</CoverletVersion>
<XunitRunneVisualstudio>3.1.5</XunitRunneVisualstudio>
<AkkaVersion>1.5.50</AkkaVersion>
<XunitRunneVisualstudio>3.1.5</XunitRunneVisualstudio>
<AkkaVersion>1.5.51</AkkaVersion>

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Upgraded to Akka.NET v1.5.51 in order to get access to akkadotnet/akka.net#7842

<MicrosoftExtensionsVersion>[6.0.0,)</MicrosoftExtensionsVersion>
<SystemTextJsonVersion>[6.0.10,)</SystemTextJsonVersion>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,15 @@ namespace Akka.Persistence.Hosting
public static Akka.Hosting.AkkaConfigurationBuilder WithInMemoryJournal(this Akka.Hosting.AkkaConfigurationBuilder builder, System.Action<Akka.Persistence.Hosting.AkkaPersistenceJournalBuilder> journalBuilder, string journalId = "inmem", bool isDefaultPlugin = true) { }
public static Akka.Hosting.AkkaConfigurationBuilder WithInMemorySnapshotStore(this Akka.Hosting.AkkaConfigurationBuilder builder, string snapshotStoreId = "inmem", bool isDefaultPlugin = true) { }
public static Akka.Hosting.AkkaConfigurationBuilder WithJournal(this Akka.Hosting.AkkaConfigurationBuilder builder, Akka.Persistence.Hosting.JournalOptions journalOptions) { }
public static Akka.Hosting.AkkaConfigurationBuilder WithJournal(this Akka.Hosting.AkkaConfigurationBuilder builder, Akka.Persistence.Hosting.JournalOptions journalOptions, System.Action<Akka.Persistence.Hosting.AkkaPersistenceJournalBuilder>? configureBuilder) { }
[System.Obsolete("Use WithJournal(journalOptions, configureBuilder) instead to combine options conf" +
"iguration with event adapters and health checks. This method will be removed in " +
"v2.0.")]
public static Akka.Hosting.AkkaConfigurationBuilder WithJournal(this Akka.Hosting.AkkaConfigurationBuilder builder, string journalId, System.Action<Akka.Persistence.Hosting.AkkaPersistenceJournalBuilder> journalBuilder) { }
public static Akka.Hosting.AkkaConfigurationBuilder WithJournalAndSnapshot(this Akka.Hosting.AkkaConfigurationBuilder builder, Akka.Persistence.Hosting.JournalOptions journalOptions, Akka.Persistence.Hosting.SnapshotOptions snapshotOptions) { }
public static Akka.Hosting.AkkaConfigurationBuilder WithJournalAndSnapshot(this Akka.Hosting.AkkaConfigurationBuilder builder, Akka.Persistence.Hosting.JournalOptions journalOptions, Akka.Persistence.Hosting.SnapshotOptions snapshotOptions, System.Action<Akka.Persistence.Hosting.AkkaPersistenceJournalBuilder>? configureJournal, System.Action<Akka.Persistence.Hosting.AkkaPersistenceSnapshotBuilder>? configureSnapshot) { }
public static Akka.Hosting.AkkaConfigurationBuilder WithSnapshot(this Akka.Hosting.AkkaConfigurationBuilder builder, Akka.Persistence.Hosting.SnapshotOptions snapshotOptions) { }
public static Akka.Hosting.AkkaConfigurationBuilder WithSnapshot(this Akka.Hosting.AkkaConfigurationBuilder builder, Akka.Persistence.Hosting.SnapshotOptions snapshotOptions, System.Action<Akka.Persistence.Hosting.AkkaPersistenceSnapshotBuilder>? configureBuilder) { }
}
public sealed class AkkaPersistenceJournalBuilder
{
Expand All @@ -22,6 +28,12 @@ namespace Akka.Persistence.Hosting
where TAdapter : Akka.Persistence.Journal.IReadEventAdapter { }
public Akka.Persistence.Hosting.AkkaPersistenceJournalBuilder AddWriteEventAdapter<TAdapter>(string eventAdapterName, System.Collections.Generic.IEnumerable<System.Type> boundTypes)
where TAdapter : Akka.Persistence.Journal.IWriteEventAdapter { }
public Akka.Persistence.Hosting.AkkaPersistenceJournalBuilder WithHealthCheck(Microsoft.Extensions.Diagnostics.HealthChecks.HealthStatus unHealthyStatus = 1, string? name = null) { }
}
public sealed class AkkaPersistenceSnapshotBuilder
{
public AkkaPersistenceSnapshotBuilder(string snapshotStoreId, Akka.Hosting.AkkaConfigurationBuilder builder) { }
public Akka.Persistence.Hosting.AkkaPersistenceSnapshotBuilder WithHealthCheck(Microsoft.Extensions.Diagnostics.HealthChecks.HealthStatus unHealthyStatus = 1, string? name = null) { }
}
public static class Extensions
{
Expand Down
27 changes: 24 additions & 3 deletions src/Akka.Persistence.Hosting.Tests/EventAdapterSpecs.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Threading.Tasks;
using Akka.Actor;
using Akka.Configuration;
using Akka.Hosting;
using Akka.Persistence.Journal;
using Akka.Util;
Expand All @@ -17,11 +18,29 @@ public static async Task<IHost> StartHost(Action<IServiceCollection> testSetup)
{
var host = new HostBuilder()
.ConfigureServices(testSetup).Build();

await host.StartAsync();
return host;
}


// Mock SQL Server journal options for testing event adapters
private sealed class MockSqlServerJournalOptions : JournalOptions
{
public MockSqlServerJournalOptions() : base(isDefault: false)
{
Identifier = "sql-server";
}

public override string Identifier { get; set; }

protected override Config InternalDefaultConfig =>
ConfigurationFactory.ParseString(@"
akka.persistence.journal.sql-server {
class = ""Akka.Persistence.Journal.MemoryJournal, Akka.Persistence""
plugin-dispatcher = ""akka.actor.default-dispatcher""
}");
}

public sealed class Event1{ }
public sealed class Event2{ }

Expand Down Expand Up @@ -81,7 +100,9 @@ public IEventSequence FromJournal(object evt, string manifest)

protected override void ConfigureAkka(AkkaConfigurationBuilder builder, IServiceProvider provider)
{
builder.WithJournal("sql-server", journalBuilder =>
// Using the new unified API: WithJournal(options, builder)
var journalOptions = new MockSqlServerJournalOptions();
builder.WithJournal(journalOptions, journalBuilder =>
{
journalBuilder.AddWriteEventAdapter<EventMapper1>("mapper1", new Type[] { typeof(Event1) });
journalBuilder.AddReadEventAdapter<ReadAdapter>("reader1", new Type[] { typeof(Event1) });
Expand Down
288 changes: 288 additions & 0 deletions src/Akka.Persistence.Hosting.Tests/UnifiedApiSpecs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,288 @@
using System;
using Akka.Configuration;
using Akka.Hosting;
using Akka.Persistence.Journal;
using Akka.Util;
using FluentAssertions;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using Microsoft.Extensions.Hosting;
using Xunit;
using Xunit.Abstractions;

namespace Akka.Persistence.Hosting.Tests;

/// <summary>
/// Shared test resources for unified API specs
/// </summary>
public static class UnifiedApiTestResources

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Tests the "unified" Akka.Persistence.Hosting APIs and tries to get test coverage over the PersistenceHostingExtensions

{
// Mock journal options for testing
public sealed class TestJournalOptions : JournalOptions
{
public TestJournalOptions(bool isDefault = true) : base(isDefault)
{
Identifier = "test-journal";
}

public override string Identifier { get; set; }

protected override Config InternalDefaultConfig =>
ConfigurationFactory.ParseString(@"
akka.persistence.journal.test-journal {
class = ""Akka.Persistence.Journal.MemoryJournal, Akka.Persistence""
plugin-dispatcher = ""akka.actor.default-dispatcher""
}");
}

// Mock snapshot options for testing
public sealed class TestSnapshotOptions : SnapshotOptions
{
public TestSnapshotOptions(bool isDefault = true) : base(isDefault)
{
Identifier = "test-snapshot";
}

public override string Identifier { get; set; }

protected override Config InternalDefaultConfig =>
ConfigurationFactory.ParseString(@"
akka.persistence.snapshot-store.test-snapshot {
class = ""Akka.Persistence.Snapshot.MemorySnapshotStore, Akka.Persistence""
plugin-dispatcher = ""akka.actor.default-dispatcher""
}");
}

// Test event adapters
public sealed class TestEvent { }

public sealed class TestWriteAdapter : IWriteEventAdapter
{
public string Manifest(object evt) => string.Empty;
public object ToJournal(object evt) => evt;
}

public sealed class TestReadAdapter : IReadEventAdapter
{
public IEventSequence FromJournal(object evt, string manifest)
=> new SingleEventSequence(evt);
}
}

/// <summary>
/// Test WithJournal(JournalOptions) without builder
/// </summary>
public sealed class JournalOptionsOnlySpec : Akka.Hosting.TestKit.TestKit
{
public JournalOptionsOnlySpec(ITestOutputHelper output) : base(output: output) { }

protected override void ConfigureServices(HostBuilderContext context, IServiceCollection services)
{
base.ConfigureServices(context, services);
services.AddHealthChecks();
}

protected override void ConfigureAkka(AkkaConfigurationBuilder builder, IServiceProvider provider)
{
var options = new UnifiedApiTestResources.TestJournalOptions { IsDefaultPlugin = true };
builder.WithJournal(options);
}

[Fact]
public void WithJournal_should_configure_journal_options_only()
{
var config = Sys.Settings.Config;
config.GetString("akka.persistence.journal.plugin")
.Should().Be("akka.persistence.journal.test-journal");
// The plugin should be configured even if class property isn't set in the minimal config
config.HasPath("akka.persistence.journal.test-journal")
.Should().BeTrue();
}
}

/// <summary>
/// Test WithJournal(JournalOptions, builder) with event adapters
/// </summary>
public sealed class JournalWithAdaptersSpec : Akka.Hosting.TestKit.TestKit
{
public JournalWithAdaptersSpec(ITestOutputHelper output) : base(output: output) { }

protected override void ConfigureServices(HostBuilderContext context, IServiceCollection services)
{
base.ConfigureServices(context, services);
services.AddHealthChecks();
}

protected override void ConfigureAkka(AkkaConfigurationBuilder builder, IServiceProvider provider)
{
var options = new UnifiedApiTestResources.TestJournalOptions { IsDefaultPlugin = true };
builder.WithJournal(options, journal => journal
.AddWriteEventAdapter<UnifiedApiTestResources.TestWriteAdapter>("test-adapter",
new[] { typeof(UnifiedApiTestResources.TestEvent) })
.AddReadEventAdapter<UnifiedApiTestResources.TestReadAdapter>("test-reader",
new[] { typeof(UnifiedApiTestResources.TestEvent) }));
}

[Fact]
public void WithJournal_with_builder_should_configure_options_and_adapters()
{
var config = Sys.Settings.Config;

// assert - journal is configured (checking that at least the path exists)
config.HasPath("akka.persistence.journal.test-journal")
.Should().BeTrue();

// assert - adapters are configured
var journalConfig = config.GetConfig("akka.persistence.journal.test-journal");
journalConfig.GetString("event-adapters.test-adapter")
.Should().Be(typeof(UnifiedApiTestResources.TestWriteAdapter).TypeQualifiedName());
journalConfig.GetString("event-adapters.test-reader")
.Should().Be(typeof(UnifiedApiTestResources.TestReadAdapter).TypeQualifiedName());

var bindings = journalConfig.GetStringList(
$"event-adapter-bindings.\"{typeof(UnifiedApiTestResources.TestEvent).TypeQualifiedName()}\"");
bindings.Should().BeEquivalentTo("test-adapter", "test-reader");
}
}

/// <summary>
/// Test WithSnapshot(SnapshotOptions) without builder
/// </summary>
public sealed class SnapshotOptionsOnlySpec : Akka.Hosting.TestKit.TestKit
{
public SnapshotOptionsOnlySpec(ITestOutputHelper output) : base(output: output) { }

protected override void ConfigureServices(HostBuilderContext context, IServiceCollection services)
{
base.ConfigureServices(context, services);
services.AddHealthChecks();
}

protected override void ConfigureAkka(AkkaConfigurationBuilder builder, IServiceProvider provider)
{
var options = new UnifiedApiTestResources.TestSnapshotOptions { IsDefaultPlugin = true };
builder.WithSnapshot(options);
}

[Fact]
public void WithSnapshot_should_configure_snapshot_options_only()
{
var config = Sys.Settings.Config;
config.GetString("akka.persistence.snapshot-store.plugin")
.Should().Be("akka.persistence.snapshot-store.test-snapshot");
config.HasPath("akka.persistence.snapshot-store.test-snapshot")
.Should().BeTrue();
}
}

/// <summary>
/// Test WithJournalAndSnapshot without builders
/// </summary>
public sealed class JournalAndSnapshotWithoutBuildersSpec : Akka.Hosting.TestKit.TestKit
{
public JournalAndSnapshotWithoutBuildersSpec(ITestOutputHelper output) : base(output: output) { }

protected override void ConfigureServices(HostBuilderContext context, IServiceCollection services)
{
base.ConfigureServices(context, services);
services.AddHealthChecks();
}

protected override void ConfigureAkka(AkkaConfigurationBuilder builder, IServiceProvider provider)
{
var journalOptions = new UnifiedApiTestResources.TestJournalOptions { IsDefaultPlugin = true };
var snapshotOptions = new UnifiedApiTestResources.TestSnapshotOptions { IsDefaultPlugin = true };
builder.WithJournalAndSnapshot(journalOptions, snapshotOptions);
}

[Fact]
public void WithJournalAndSnapshot_should_configure_both_without_builders()
{
var config = Sys.Settings.Config;
config.GetString("akka.persistence.journal.plugin")
.Should().Be("akka.persistence.journal.test-journal");
config.GetString("akka.persistence.snapshot-store.plugin")
.Should().Be("akka.persistence.snapshot-store.test-snapshot");
}
}

/// <summary>
/// Test WithJournalAndSnapshot with builder actions
/// </summary>
public sealed class JournalAndSnapshotWithBuildersSpec : Akka.Hosting.TestKit.TestKit

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

End to end integration test - also asserts that the health checks on each plugin work.

{
public JournalAndSnapshotWithBuildersSpec(ITestOutputHelper output) : base(output: output) { }

protected override void ConfigureServices(HostBuilderContext context, IServiceCollection services)
{
base.ConfigureServices(context, services);
services.AddHealthChecks();
}

protected override void ConfigureAkka(AkkaConfigurationBuilder builder, IServiceProvider provider)
{
var journalOptions = new UnifiedApiTestResources.TestJournalOptions { IsDefaultPlugin = true };
var snapshotOptions = new UnifiedApiTestResources.TestSnapshotOptions { IsDefaultPlugin = true };

builder.WithJournalAndSnapshot(
journalOptions,
snapshotOptions,
journal => journal
.AddWriteEventAdapter<UnifiedApiTestResources.TestWriteAdapter>("adapter",
[typeof(UnifiedApiTestResources.TestEvent)])
.WithHealthCheck(),
snapshot => snapshot
.WithHealthCheck());
}

[Fact]
public void WithJournalAndSnapshot_with_builders_should_configure_everything()
{
var config = Sys.Settings.Config;

// assert - both plugins configured
config.HasPath("akka.persistence.journal.test-journal")
.Should().BeTrue();
config.HasPath("akka.persistence.snapshot-store.test-snapshot")
.Should().BeTrue();

// assert - adapters configured
var journalConfig = config.GetConfig("akka.persistence.journal.test-journal");
journalConfig.GetString("event-adapters.adapter")
.Should().Be(typeof(UnifiedApiTestResources.TestWriteAdapter).TypeQualifiedName());
}
}

/// <summary>
/// Test null builder actions work correctly
/// </summary>
public sealed class NullBuilderActionsSpec : Akka.Hosting.TestKit.TestKit
{
public NullBuilderActionsSpec(ITestOutputHelper output) : base(output: output) { }

protected override void ConfigureServices(HostBuilderContext context, IServiceCollection services)
{
base.ConfigureServices(context, services);
services.AddHealthChecks();
}

protected override void ConfigureAkka(AkkaConfigurationBuilder builder, IServiceProvider provider)
{
var journalOptions = new UnifiedApiTestResources.TestJournalOptions { IsDefaultPlugin = true };
var snapshotOptions = new UnifiedApiTestResources.TestSnapshotOptions { IsDefaultPlugin = false };

// Test that passing null for builder actions works
builder.WithJournal(journalOptions, configureBuilder: null);
builder.WithSnapshot(snapshotOptions, configureBuilder: null);
}

[Fact]
public void Null_builder_actions_should_work()
{
var config = Sys.Settings.Config;
config.GetString("akka.persistence.journal.plugin")
.Should().Be("akka.persistence.journal.test-journal");
config.HasPath("akka.persistence.snapshot-store.test-snapshot")
.Should().BeTrue();
}
}
Loading
Loading