Skip to content

Commit

Permalink
Add/update Setup classes (#235)
Browse files Browse the repository at this point in the history
  • Loading branch information
Arkatufus authored Aug 26, 2022
1 parent 48046bf commit 2e7bfd2
Show file tree
Hide file tree
Showing 10 changed files with 694 additions and 90 deletions.
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

0 comments on commit 2e7bfd2

Please sign in to comment.