Skip to content
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

Adding Hosting Extensions for Akka.Persistence.MongoDB #331

Merged
merged 16 commits into from
Jul 17, 2023
Merged
Show file tree
Hide file tree
Changes from 7 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
16 changes: 14 additions & 2 deletions Akka.Persistence.MongoDb.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.29306.81
# Visual Studio Version 17
VisualStudioVersion = 17.6.33723.286
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Akka.Persistence.MongoDb", "src\Akka.Persistence.MongoDb\Akka.Persistence.MongoDb.csproj", "{E945AABA-2779-41E8-9B43-8898FFD64F22}"
EndProject
Expand All @@ -15,6 +15,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "build", "build", "{BE1178E1
build.sh = build.sh
EndProjectSection
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Akka.Persistence.MongoDb.Hosting", "src\Akka.Persistence.MongoDb.Hosting\Akka.Persistence.MongoDb.Hosting.csproj", "{72B8C165-FE00-465F-A2E9-60B4B79F81AF}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Akka.Persistence.MongoDb.Hosting.Tests", "src\Akka.Persistence.MongoDb.Hosting.Tests\Akka.Persistence.MongoDb.Hosting.Tests.csproj", "{7DA742BE-BE29-48E3-AA6D-D06C9D7C6464}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -29,6 +33,14 @@ Global
{0F9B9BC6-9F86-40E8-BA9B-D27BF3AC7970}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0F9B9BC6-9F86-40E8-BA9B-D27BF3AC7970}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0F9B9BC6-9F86-40E8-BA9B-D27BF3AC7970}.Release|Any CPU.Build.0 = Release|Any CPU
{72B8C165-FE00-465F-A2E9-60B4B79F81AF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{72B8C165-FE00-465F-A2E9-60B4B79F81AF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{72B8C165-FE00-465F-A2E9-60B4B79F81AF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{72B8C165-FE00-465F-A2E9-60B4B79F81AF}.Release|Any CPU.Build.0 = Release|Any CPU
{7DA742BE-BE29-48E3-AA6D-D06C9D7C6464}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7DA742BE-BE29-48E3-AA6D-D06C9D7C6464}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7DA742BE-BE29-48E3-AA6D-D06C9D7C6464}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7DA742BE-BE29-48E3-AA6D-D06C9D7C6464}.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
@@ -0,0 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk">
mhbuck marked this conversation as resolved.
Show resolved Hide resolved
<Import Project="..\common.props" />

<PropertyGroup>
<TargetFrameworks>$(NetFrameworkTestVersion);$(NetTestVersion);$(NetCoreTestVersion)</TargetFrameworks>
Copy link
Contributor Author

Choose a reason for hiding this comment

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

$(NetFrameworkTestVersion) - I did not have the Netframework developer kit installed on my machine so the tests have not been run against framework. Given the changes I don't believe it will be an issue but if required I will get them installed. They were all run against NetTestVersion and NetCoreTestVersion

Copy link
Member

Choose a reason for hiding this comment

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

No worries - looks like things are passing on CI/CD so far. We can probably drop .NET Framework for testing MongoDb in the near future anyway.

</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="6.11.0" />
mhbuck marked this conversation as resolved.
Show resolved Hide resolved
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="$(TestSdkVersion)" />
<PackageReference Include="xunit.runner.visualstudio" Version="$(XunitVersion)">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="xunit" Version="$(XunitVersion)" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Akka.Persistence.MongoDb.Hosting\Akka.Persistence.MongoDb.Hosting.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
using System;
using System.IO;
using System.Text;
using Akka.Configuration;
using FluentAssertions;
using FluentAssertions.Extensions;
using Microsoft.Extensions.Configuration;
using Xunit;

namespace Akka.Persistence.MongoDb.Hosting.Tests
{
public class MongoDbJournalOptionsSpec
Copy link
Contributor Author

Choose a reason for hiding this comment

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

These tests are basically clones of the ones here https://github.com/akkadotnet/Akka.Persistence.PostgreSql/blob/dev/src/Akka.Persistence.PostgreSql.Tests/Hosting/PostgreSqlOptionsSpec.cs

I have split them up and cleaned up parts but if there are additional cases needed I am happy to add them.

{
[Fact(DisplayName = "MongoDbJournalOptions as default plugin should generate plugin setting")]
public void DefaultPluginJournalOptionsTest()
{
var options = new MongoDbJournalOptions(true);
var config = options.ToConfig();

config.GetString("akka.persistence.journal.plugin").Should().Be("akka.persistence.journal.mongodb");
config.HasPath("akka.persistence.journal.mongodb").Should().BeTrue();
}

[Fact(DisplayName = "Empty MongoDbJournalOptions should equal empty config with default fallback")]
public void DefaultJournalOptionsTest()
{
var options = new MongoDbJournalOptions(false);
var emptyRootConfig = options.ToConfig().WithFallback(options.DefaultConfig);
var baseRootConfig = Config.Empty
.WithFallback(MongoDbPersistence.DefaultConfiguration());

emptyRootConfig.GetString("akka.persistence.journal.plugin").Should().Be(baseRootConfig.GetString("akka.persistence.journal.plugin"));

var config = emptyRootConfig.GetConfig("akka.persistence.journal.mongodb");
var baseConfig = baseRootConfig.GetConfig("akka.persistence.journal.mongodb");
config.Should().NotBeNull();
baseConfig.Should().NotBeNull();

config.GetString("class").Should().Be(baseConfig.GetString("class"));
config.GetString("connection-string").Should().Be(baseConfig.GetString("connection-string"));
config.GetBoolean("use-write-transaction").Should().Be(baseConfig.GetBoolean("use-write-transaction"));
config.GetBoolean("auto-initialize").Should().Be(baseConfig.GetBoolean("auto-initialize"));
config.GetString("plugin-dispatcher").Should().Be(baseConfig.GetString("plugin-dispatcher"));
config.GetString("collection").Should().Be(baseConfig.GetString("collection"));
config.GetString("metadata-collection").Should().Be(baseConfig.GetString("metadata-collection"));
config.GetBoolean("legacy-serialization").Should().Be(baseConfig.GetBoolean("legacy-serialization"));
config.GetTimeSpan("call-timeout").Should().Be(baseConfig.GetTimeSpan("call-timeout"));
}

[Fact(DisplayName = "Empty MongoDbJournalOptions with custom identifier should equal empty config with default fallback")]
public void CustomIdJournalOptionsTest()
{
var options = new MongoDbJournalOptions(false, "custom");
var emptyRootConfig = options.ToConfig().WithFallback(options.DefaultConfig);
var baseRootConfig = Config.Empty
.WithFallback(MongoDbPersistence.DefaultConfiguration());

emptyRootConfig.GetString("akka.persistence.journal.plugin").Should().Be(baseRootConfig.GetString("akka.persistence.journal.plugin"));

var config = emptyRootConfig.GetConfig("akka.persistence.journal.custom");
var baseConfig = baseRootConfig.GetConfig("akka.persistence.journal.mongodb");
config.Should().NotBeNull();
baseConfig.Should().NotBeNull();

config.GetString("class").Should().Be(baseConfig.GetString("class"));
config.GetString("connection-string").Should().Be(baseConfig.GetString("connection-string"));
config.GetBoolean("use-write-transaction").Should().Be(baseConfig.GetBoolean("use-write-transaction"));
config.GetBoolean("auto-initialize").Should().Be(baseConfig.GetBoolean("auto-initialize"));
config.GetString("plugin-dispatcher").Should().Be(baseConfig.GetString("plugin-dispatcher"));
config.GetString("collection").Should().Be(baseConfig.GetString("collection"));
config.GetString("metadata-collection").Should().Be(baseConfig.GetString("metadata-collection"));
config.GetBoolean("legacy-serialization").Should().Be(baseConfig.GetBoolean("legacy-serialization"));
config.GetTimeSpan("call-timeout").Should().Be(baseConfig.GetTimeSpan("call-timeout"));
}

[Fact(DisplayName = "MongoDbJournalOptions should generate proper config")]
public void JournalOptionsTest()
{
var options = new MongoDbJournalOptions(true)
{
Identifier = "custom",
AutoInitialize = true,
ConnectionString = "testConnection",
Collection = "testCollection",
MetadataCollection = "metadataCollection",
UseWriteTransaction = true,
LegacySerialization = true,
CallTimeout = TimeSpan.FromHours(2)
};

var baseConfig = options.ToConfig();

baseConfig.GetString("akka.persistence.journal.plugin").Should().Be("akka.persistence.journal.custom");

var config = baseConfig.GetConfig("akka.persistence.journal.custom");
config.Should().NotBeNull();
config.GetString("connection-string").Should().Be(options.ConnectionString);
config.GetBoolean("auto-initialize").Should().Be(options.AutoInitialize);
config.GetString("collection").Should().Be(options.Collection);
config.GetString("metadata-collection").Should().Be(options.MetadataCollection);
config.GetBoolean("use-write-transaction").Should().Be(options.UseWriteTransaction);
config.GetBoolean("legacy-serialization").Should().Be(options.LegacySerialization);
config.GetTimeSpan("call-timeout").Should().Be(options.CallTimeout);

}

const string Json = @"
{
""Logging"": {
""LogLevel"": {
""Default"": ""Information"",
""Microsoft.AspNetCore"": ""Warning""
}
},
""Akka"": {
""JournalOptions"": {
""ConnectionString"": ""mongodb://localhost:27017"",
""UseWriteTransaction"": ""true"",
""Identifier"": ""custommongodb"",
""AutoInitialize"": true,
""IsDefaultPlugin"": false,

""Collection"": ""CustomEnventJournalCollection"",
""MetadataCollection"": ""CustomMetadataCollection"",
""LegacySerialization"" : ""true"",
""CallTimeout"": ""00:10:00"",
""Serializer"": ""hyperion"",
}
}
}";

[Fact(DisplayName = "MongoDbJournalOptions should be bindable to IConfiguration")]
public void JournalOptionsIConfigurationBindingTest()
{
using var stream = new MemoryStream(Encoding.UTF8.GetBytes(Json));
var jsonConfig = new ConfigurationBuilder().AddJsonStream(stream).Build();

var options = jsonConfig.GetSection("Akka:JournalOptions").Get<MongoDbJournalOptions>();
options.ConnectionString.Should().Be("mongodb://localhost:27017");
options.UseWriteTransaction.Should().BeTrue();
options.Identifier.Should().Be("custommongodb");
options.AutoInitialize.Should().BeTrue();
options.IsDefaultPlugin.Should().BeFalse();
options.Collection.Should().Be("CustomEnventJournalCollection");
options.MetadataCollection.Should().Be("CustomMetadataCollection");
options.LegacySerialization.Should().BeTrue();
options.CallTimeout.Should().Be(10.Minutes());
options.Serializer.Should().Be("hyperion");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
using System;
using System.IO;
using System.Text;
using Akka.Configuration;
using FluentAssertions;
using FluentAssertions.Extensions;
using Microsoft.Extensions.Configuration;
using Xunit;

namespace Akka.Persistence.MongoDb.Hosting.Tests
{
public class MongoDbSnapshotOptionsSpec
{
[Fact(DisplayName = "MongoDbSnapshotOptions as default plugin should generate plugin setting")]
public void DefaultPluginSnapshotOptionsTest()
{
var options = new MongoDbSnapshotOptions(true);
var config = options.ToConfig();

config.GetString("akka.persistence.snapshot-store.plugin").Should().Be("akka.persistence.snapshot-store.mongodb");
config.HasPath("akka.persistence.snapshot-store.mongodb").Should().BeTrue();
}

[Fact(DisplayName = "Empty MongoDbSnapshotOptions with default fallback should return default config")]
public void DefaultSnapshotOptionsTest()
{
var options = new MongoDbSnapshotOptions(false);
var emptyRootConfig = options.ToConfig().WithFallback(options.DefaultConfig);
var baseRootConfig = Config.Empty
.WithFallback(MongoDbPersistence.DefaultConfiguration());

emptyRootConfig.GetString("akka.persistence.snapshot-store.plugin").Should().Be(baseRootConfig.GetString("akka.persistence.snapshot-store.plugin"));

var config = emptyRootConfig.GetConfig("akka.persistence.snapshot-store.mongodb");
var baseConfig = baseRootConfig.GetConfig("akka.persistence.snapshot-store.mongodb");
config.Should().NotBeNull();
baseConfig.Should().NotBeNull();

config.GetString("class").Should().Be(baseConfig.GetString("class"));
config.GetString("connection-string").Should().Be(baseConfig.GetString("connection-string"));
config.GetBoolean("use-write-transaction").Should().Be(baseConfig.GetBoolean("use-write-transaction"));
config.GetBoolean("auto-initialize").Should().Be(baseConfig.GetBoolean("auto-initialize"));
config.GetString("plugin-dispatcher").Should().Be(baseConfig.GetString("plugin-dispatcher"));
config.GetString("collection").Should().Be(baseConfig.GetString("collection"));
config.GetBoolean("legacy-serialization").Should().Be(baseConfig.GetBoolean("legacy-serialization"));
config.GetTimeSpan("call-timeout").Should().Be(baseConfig.GetTimeSpan("call-timeout"));
}

[Fact(DisplayName = "Empty MongoDbSnapshotOptions with custom identifier should equal empty config with default fallback")]
public void CustomIdSnapshotOptionsTest()
{
var options = new MongoDbSnapshotOptions(false, "custom");
var emptyRootConfig = options.ToConfig().WithFallback(options.DefaultConfig);
var baseRootConfig = Config.Empty
.WithFallback(MongoDbPersistence.DefaultConfiguration());

emptyRootConfig.GetString("akka.persistence.snapshot-store.plugin").Should().Be(baseRootConfig.GetString("akka.persistence.snapshot-store.plugin"));

var config = emptyRootConfig.GetConfig("akka.persistence.snapshot-store.custom");
var baseConfig = baseRootConfig.GetConfig("akka.persistence.snapshot-store.mongodb");
config.Should().NotBeNull();
baseConfig.Should().NotBeNull();

config.GetString("class").Should().Be(baseConfig.GetString("class"));
config.GetString("connection-string").Should().Be(baseConfig.GetString("connection-string"));
config.GetBoolean("use-write-transaction").Should().Be(baseConfig.GetBoolean("use-write-transaction"));
config.GetBoolean("auto-initialize").Should().Be(baseConfig.GetBoolean("auto-initialize"));
config.GetString("plugin-dispatcher").Should().Be(baseConfig.GetString("plugin-dispatcher"));
config.GetString("collection").Should().Be(baseConfig.GetString("collection"));
config.GetBoolean("legacy-serialization").Should().Be(baseConfig.GetBoolean("legacy-serialization"));
config.GetTimeSpan("call-timeout").Should().Be(baseConfig.GetTimeSpan("call-timeout"));
}

[Fact(DisplayName = "MongoDbSnapshotOptions should generate proper config")]
public void SnapshotOptionsTest()
{
var options = new MongoDbSnapshotOptions(true)
{
Identifier = "custom",
AutoInitialize = true,
ConnectionString = "testConnection",
Collection = "testCollection",
UseWriteTransaction = true,
LegacySerialization = true,
CallTimeout = TimeSpan.FromHours(2)
};

var baseConfig = options.ToConfig()
.WithFallback(MongoDbPersistence.DefaultConfiguration());

baseConfig.GetString("akka.persistence.snapshot-store.plugin").Should().Be("akka.persistence.snapshot-store.custom");

var config = baseConfig.GetConfig("akka.persistence.snapshot-store.custom");
config.Should().NotBeNull();
config.GetString("connection-string").Should().Be(options.ConnectionString);
config.GetBoolean("auto-initialize").Should().Be(options.AutoInitialize);
config.GetString("collection").Should().Be(options.Collection);
config.GetBoolean("use-write-transaction").Should().Be(options.UseWriteTransaction);
config.GetBoolean("legacy-serialization").Should().Be(options.LegacySerialization);
config.GetTimeSpan("call-timeout").Should().Be(options.CallTimeout);
}

[Fact(DisplayName = "MongoDbSnapshotOptions should be bindable to IConfiguration")]
public void SnapshotOptionsIConfigurationBindingTest()
{
const string json = @"
{
""Logging"": {
""LogLevel"": {
""Default"": ""Information"",
""Microsoft.AspNetCore"": ""Warning""
}
},
""Akka"": {
""SnapshotOptions"": {
""ConnectionString"": ""mongodb://localhost:27017"",
""UseWriteTransaction"": ""true"",
""Identifier"": ""custommongodb"",
""AutoInitialize"": true,
""IsDefaultPlugin"": false,

""Collection"": ""CustomEnventJournalCollection"",
""LegacySerialization"" : ""true"",
""CallTimeout"": ""00:10:00"",
""Serializer"": ""hyperion"",
}
}
}";

using var stream = new MemoryStream(Encoding.UTF8.GetBytes(json));
var jsonConfig = new ConfigurationBuilder().AddJsonStream(stream).Build();

var options = jsonConfig.GetSection("Akka:SnapshotOptions").Get<MongoDbSnapshotOptions>();
options.ConnectionString.Should().Be("mongodb://localhost:27017");
options.UseWriteTransaction.Should().BeTrue();
options.Identifier.Should().Be("custommongodb");
options.AutoInitialize.Should().BeTrue();
options.IsDefaultPlugin.Should().BeFalse();
options.Collection.Should().Be("CustomEnventJournalCollection");
options.LegacySerialization.Should().BeTrue();
options.CallTimeout.Should().Be(10.Minutes());
options.Serializer.Should().Be("hyperion");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="..\common.props" />

<PropertyGroup>
<TargetFrameworks>$(NetStandardLibVersion)</TargetFrameworks>
<LangVersion>latest</LangVersion>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Akka.Persistence.Hosting" Version="$(AkkaVersion)" />
Copy link
Contributor Author

Choose a reason for hiding this comment

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

From the other projects it looked like targeting $(AkkaVersion) was the correct approach.

</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Akka.Persistence.MongoDb\Akka.Persistence.MongoDb.csproj" />
</ItemGroup>
</Project>
Loading