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

Add/update Setup and setting classes #235

Merged
merged 2 commits into from
Aug 26, 2022
Merged
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
207 changes: 161 additions & 46 deletions src/Akka.Persistence.Azure.Tests/AzurePersistenceConfigSpec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,84 +9,199 @@
using Akka.Persistence.Azure.Journal;
using Akka.Persistence.Azure.Query;
using Akka.Persistence.Azure.Snapshot;
using Azure.Data.Tables;
using Azure.Identity;
using Azure.Storage.Blobs;
using Azure.Storage.Blobs.Models;
using FluentAssertions;
using FluentAssertions.Extensions;
using Xunit;

namespace Akka.Persistence.Azure.Tests
{
public class AzurePersistenceConfigSpec
{
private const string SnapshotStorePath = "akka.persistence.snapshot-store.azure-blob-store";
private const string JournalPath = "akka.persistence.journal.azure-table";

private static readonly AzureBlobSnapshotStoreSettings DefaultSnapshotSettings =
AzureBlobSnapshotStoreSettings.Create(AzurePersistence.DefaultConfig.GetConfig(SnapshotStorePath));

private static readonly AzureTableStorageJournalSettings DefaultJournalSettings =
AzureTableStorageJournalSettings.Create(AzurePersistence.DefaultConfig.GetConfig(JournalPath));

[Fact]
public void ShouldLoadDefaultConfig()
{
var defaultConfig = AzurePersistence.DefaultConfig;
defaultConfig.HasPath("akka.persistence.journal.azure-table").Should().BeTrue();
defaultConfig.HasPath("akka.persistence.snapshot-store.azure-blob-store").Should().BeTrue();
AzurePersistence.DefaultConfig.HasPath(SnapshotStorePath).Should().BeTrue();
AzurePersistence.DefaultConfig.HasPath(JournalPath).Should().BeTrue();
AzurePersistence.DefaultConfig.HasPath(AzureTableStorageReadJournal.Identifier).Should().BeTrue();
}

[Fact]
public void ShouldParseDefaultSnapshotConfig()
{
var blobSettings =
AzureBlobSnapshotStoreSettings.Create(
ConfigurationFactory.ParseString(@"akka.persistence.snapshot-store.azure-blob-store{
connection-string = foo
container-name = bar
}").WithFallback(AzurePersistence.DefaultConfig)
.GetConfig("akka.persistence.snapshot-store.azure-blob-store"));
var settings =
AzureBlobSnapshotStoreSettings.Create(AzurePersistence.DefaultConfig.GetConfig(SnapshotStorePath));

blobSettings.ContainerName.Should().Be("bar");
blobSettings.ConnectionString.Should().Be("foo");
blobSettings.ConnectTimeout.Should().Be(TimeSpan.FromSeconds(3));
blobSettings.RequestTimeout.Should().Be(TimeSpan.FromSeconds(3));
blobSettings.VerboseLogging.Should().BeFalse();
blobSettings.ContainerPublicAccessType.Should().Be(PublicAccessType.None);
settings.ConnectionString.Should().BeEmpty();
settings.ContainerName.Should().Be("akka-persistence-default-container");
settings.ConnectTimeout.Should().Be(3.Seconds());
settings.RequestTimeout.Should().Be(3.Seconds());
settings.VerboseLogging.Should().BeFalse();
settings.Development.Should().BeFalse();
settings.AutoInitialize.Should().BeTrue();
settings.ContainerPublicAccessType.Should().Be(PublicAccessType.None);
settings.ServiceUri.Should().BeNull();
settings.DefaultAzureCredential.Should().BeNull();
settings.BlobClientOptions.Should().BeNull();
}

[Fact]
public void ShouldProvideDefaultContainerNameValue()
[Fact(DisplayName = "AzureBlobSnapshotStoreSettings With overrides should override default values")]
public void SnapshotSettingsWithMethodsTest()
{
var blobSettings =
AzureBlobSnapshotStoreSettings.Create(
ConfigurationFactory.ParseString(@"akka.persistence.snapshot-store.azure-blob-store{
connection-string = foo
}").WithFallback(AzurePersistence.DefaultConfig)
.GetConfig("akka.persistence.snapshot-store.azure-blob-store"));
var uri = new Uri("https://whatever.com");
var credentials = new DefaultAzureCredential();
var options = new BlobClientOptions();
var settings = DefaultSnapshotSettings
.WithConnectionString("abc")
.WithContainerName("bcd")
.WithConnectTimeout(1.Seconds())
.WithRequestTimeout(2.Seconds())
.WithVerboseLogging(true)
.WithDevelopment(true)
.WithAutoInitialize(false)
.WithContainerPublicAccessType(PublicAccessType.Blob)
.WithAzureCredential(uri, credentials, options);

settings.ConnectionString.Should().Be("abc");
settings.ContainerName.Should().Be("bcd");
settings.ConnectTimeout.Should().Be(1.Seconds());
settings.RequestTimeout.Should().Be(2.Seconds());
settings.VerboseLogging.Should().BeTrue();
settings.Development.Should().BeTrue();
settings.AutoInitialize.Should().BeFalse();
settings.ContainerPublicAccessType.Should().Be(PublicAccessType.Blob);
settings.ServiceUri.Should().Be(uri);
settings.DefaultAzureCredential.Should().Be(credentials);
settings.BlobClientOptions.Should().Be(options);
}

blobSettings.ContainerName.Should().Be("akka-persistence-default-container");
[Fact(DisplayName = "AzureBlobSnapshotStoreSetup should override settings values")]
public void SnapshotSetupTest()
{
var uri = new Uri("https://whatever.com");
var credentials = new DefaultAzureCredential();
var options = new BlobClientOptions();
var setup = new AzureBlobSnapshotSetup
{
ConnectionString = "abc",
ContainerName = "bcd",
ConnectTimeout = 1.Seconds(),
RequestTimeout = 2.Seconds(),
VerboseLogging = true,
Development = true,
AutoInitialize = false,
ContainerPublicAccessType = PublicAccessType.Blob,
ServiceUri = uri,
DefaultAzureCredential = credentials,
BlobClientOptions = options
};

var settings = setup.Apply(DefaultSnapshotSettings);

settings.ConnectionString.Should().Be("abc");
settings.ContainerName.Should().Be("bcd");
settings.ConnectTimeout.Should().Be(1.Seconds());
settings.RequestTimeout.Should().Be(2.Seconds());
settings.VerboseLogging.Should().BeTrue();
settings.Development.Should().BeTrue();
settings.AutoInitialize.Should().BeFalse();
settings.ContainerPublicAccessType.Should().Be(PublicAccessType.Blob);
settings.ServiceUri.Should().Be(uri);
settings.DefaultAzureCredential.Should().Be(credentials);
settings.BlobClientOptions.Should().Be(options);
}

[Fact]
public void ShouldParseTableConfig()
{
var tableSettings =
AzureTableStorageJournalSettings.Create(
ConfigurationFactory.ParseString(@"akka.persistence.journal.azure-table{
connection-string = foo
table-name = bar
}").WithFallback(AzurePersistence.DefaultConfig)
.GetConfig("akka.persistence.journal.azure-table"));
var settings = DefaultJournalSettings;

tableSettings.TableName.Should().Be("bar");
tableSettings.ConnectionString.Should().Be("foo");
tableSettings.ConnectTimeout.Should().Be(TimeSpan.FromSeconds(3));
tableSettings.RequestTimeout.Should().Be(TimeSpan.FromSeconds(3));
tableSettings.VerboseLogging.Should().BeFalse();
settings.ConnectionString.Should().BeEmpty();
settings.TableName.Should().Be("AkkaPersistenceDefaultTable");
settings.ConnectTimeout.Should().Be(3.Seconds());
settings.RequestTimeout.Should().Be(3.Seconds());
settings.VerboseLogging.Should().BeFalse();
settings.Development.Should().BeFalse();
settings.AutoInitialize.Should().BeTrue();
settings.ServiceUri.Should().BeNull();
settings.DefaultAzureCredential.Should().BeNull();
settings.TableClientOptions.Should().BeNull();
}

[Fact]
public void ShouldProvideDefaultTableNameValue()
[Fact(DisplayName = "AzureTableStorageJournalSettings With overrides should override default values")]
public void JournalSettingsWithMethodsTest()
{
var tableSettings =
AzureTableStorageJournalSettings.Create(
ConfigurationFactory.ParseString(@"akka.persistence.journal.azure-table{
connection-string = foo
}").WithFallback(AzurePersistence.DefaultConfig)
.GetConfig("akka.persistence.journal.azure-table"));
tableSettings.TableName.Should().Be("AkkaPersistenceDefaultTable");
var uri = new Uri("https://whatever.com");
var credentials = new DefaultAzureCredential();
var options = new TableClientOptions();
var settings = DefaultJournalSettings
.WithConnectionString("abc")
.WithTableName("bcd")
.WithConnectTimeout(1.Seconds())
.WithRequestTimeout(2.Seconds())
.WithVerboseLogging(true)
.WithDevelopment(true)
.WithAutoInitialize(false)
.WithAzureCredential(uri, credentials, options);

settings.ConnectionString.Should().Be("abc");
settings.TableName.Should().Be("bcd");
settings.ConnectTimeout.Should().Be(1.Seconds());
settings.RequestTimeout.Should().Be(2.Seconds());
settings.VerboseLogging.Should().BeTrue();
settings.Development.Should().BeTrue();
settings.AutoInitialize.Should().BeFalse();
settings.ServiceUri.Should().Be(uri);
settings.DefaultAzureCredential.Should().Be(credentials);
settings.TableClientOptions.Should().Be(options);
}

[Fact(DisplayName = "AzureTableStorageJournalSetup should override settings values")]
public void JournalSetupTest()
{
var uri = new Uri("https://whatever.com");
var credentials = new DefaultAzureCredential();
var options = new TableClientOptions();
var setup = new AzureTableStorageJournalSetup
{
ConnectionString = "abc",
TableName = "bcd",
ConnectTimeout = 1.Seconds(),
RequestTimeout = 2.Seconds(),
VerboseLogging = true,
Development = true,
AutoInitialize = false,
ServiceUri = uri,
DefaultAzureCredential = credentials,
TableClientOptions = options
};

var settings = setup.Apply(DefaultJournalSettings);

settings.ConnectionString.Should().Be("abc");
settings.TableName.Should().Be("bcd");
settings.ConnectTimeout.Should().Be(1.Seconds());
settings.RequestTimeout.Should().Be(2.Seconds());
settings.VerboseLogging.Should().BeTrue();
settings.Development.Should().BeTrue();
settings.AutoInitialize.Should().BeFalse();
settings.ServiceUri.Should().Be(uri);
settings.DefaultAzureCredential.Should().Be(credentials);
settings.TableClientOptions.Should().Be(options);
}

[Theory]
[InlineData("fo", "Invalid table name length")]
[InlineData("1foo", "Invalid table name")]
Expand Down
2 changes: 1 addition & 1 deletion src/Akka.Persistence.Azure/AzurePersistence.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public AzurePersistence(ActorSystem system, AzureTableStorageJournalSettings tab
/// <summary>
/// The default HOCON configuration for <see cref="AzurePersistence" />.
/// </summary>
public static Config DefaultConfig =>
public static readonly Config DefaultConfig =
ConfigurationFactory.FromResource<AzurePersistence>("Akka.Persistence.Azure.reference.conf");

/// <summary>
Expand Down
20 changes: 19 additions & 1 deletion src/Akka.Persistence.Azure/Journal/AzureTableStorageJournal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,26 @@ public AzureTableStorageJournal(Config config = null)
AzurePersistence.Get(Context.System).TableSettings :
AzureTableStorageJournalSettings.Create(config);

var setup = Context.System.Settings.Setup.Get<AzureTableStorageJournalSetup>();
if (setup.HasValue)
_settings = setup.Value.Apply(_settings);

_serialization = new SerializationHelper(Context.System);
_tableServiceClient = new TableServiceClient(_settings.ConnectionString);

if (_settings.Development)
{
_tableServiceClient = new TableServiceClient(connectionString: "UseDevelopmentStorage=true");
}
else
{
// Use DefaultAzureCredential if both ServiceUri and DefaultAzureCredential are populated in the settings
_tableServiceClient = _settings.ServiceUri != null && _settings.DefaultAzureCredential != null
? new TableServiceClient(
endpoint: _settings.ServiceUri,
tokenCredential: _settings.DefaultAzureCredential,
options: _settings.TableClientOptions)
: new TableServiceClient(connectionString: _settings.ConnectionString);
}
}

public TableClient Table
Expand Down
Loading