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

Fix custom connection name not working when using IDurableClientFactory -> CreateClient(). #2923

Merged
merged 6 commits into from
Oct 17, 2024
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
2 changes: 2 additions & 0 deletions release_notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

### Bug Fixes

- Fix custom connection name not working when using IDurableClientFactory.CreateClient() - contributed by [@hctan](https://github.com/hctan)

### Breaking Changes

### Dependency Updates
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ private AzureStorageDurabilityProvider GetAzureStorageStorageProvider(DurableCli
// Need to check this.defaultStorageProvider != null for external clients that call GetDurabilityProvider(attribute)
// which never initializes the defaultStorageProvider.
if (string.Equals(this.defaultSettings?.TaskHubName, settings.TaskHubName, StringComparison.OrdinalIgnoreCase) &&
string.Equals(this.defaultSettings?.StorageConnectionString, settings.StorageConnectionString, StringComparison.OrdinalIgnoreCase) &&
string.Equals(this.defaultSettings?.StorageAccountDetails?.ConnectionString, settings.StorageAccountDetails?.ConnectionString, StringComparison.OrdinalIgnoreCase) &&
this.defaultStorageProvider != null)
{
// It's important that clients use the same AzureStorageOrchestrationService instance
Expand Down
33 changes: 33 additions & 0 deletions test/Common/CustomTestStorageAccountProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.

using System;
using DurableTask.AzureStorage;
using Microsoft.WindowsAzure.Storage;

namespace Microsoft.Azure.WebJobs.Extensions.DurableTask.Tests
{
internal class CustomTestStorageAccountProvider : IStorageAccountProvider
{
private readonly string customConnectionString;
private readonly string customConnectionName;

public CustomTestStorageAccountProvider(string connectionName)
{
this.customConnectionName = connectionName;
this.customConnectionString = $"DefaultEndpointsProtocol=https;AccountName=test;AccountKey={GenerateRandomKey()};EndpointSuffix=core.windows.net";
}

public CloudStorageAccount GetCloudStorageAccount(string name) =>
CloudStorageAccount.Parse(name != this.customConnectionName ? TestHelpers.GetStorageConnectionString() : this.customConnectionString);

public StorageAccountDetails GetStorageAccountDetails(string name) =>
new StorageAccountDetails { ConnectionString = name != this.customConnectionName ? TestHelpers.GetStorageConnectionString() : this.customConnectionString };

private static string GenerateRandomKey()
{
string key = Guid.NewGuid().ToString();
return Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(key));
}
}
}
41 changes: 41 additions & 0 deletions test/FunctionsV2/AzureStorageDurabilityProviderFactoryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,5 +154,46 @@ public void EnvironmentIsVMSS_WorkerIdFromEnvironmentVariables()

Assert.Equal("waws-prod-euapbn1-003:dw0SmallDedicatedWebWorkerRole_hr0HostRole-3-VM-13", settings.WorkerId);
}

[Fact]
[Trait("Category", PlatformSpecificHelpers.TestCategory)]
public void CustomConnectionNameIsResolved()
{
var storageAccountProvider = new CustomTestStorageAccountProvider("CustomConnection");
var mockOptions = new OptionsWrapper<DurableTaskOptions>(new DurableTaskOptions());
var nameResolver = new Mock<INameResolver>().Object;

var factory = new AzureStorageDurabilityProviderFactory(
mockOptions,
storageAccountProvider,
nameResolver,
NullLoggerFactory.Instance,
TestHelpers.GetMockPlatformInformationService());

factory.GetDurabilityProvider(); // This will initialize the default connection string
var provider = factory.GetDurabilityProvider(new DurableClientAttribute() { ConnectionName = "CustomConnection", TaskHub = "TestHubName" });

Assert.Equal("CustomConnection", provider.ConnectionName);
}

[Fact]
[Trait("Category", PlatformSpecificHelpers.TestCategory)]
public void DefaultConnectionNameIsResolved()
{
var storageAccountProvider = new CustomTestStorageAccountProvider("CustomConnection");
var mockOptions = new OptionsWrapper<DurableTaskOptions>(new DurableTaskOptions());
var nameResolver = new Mock<INameResolver>().Object;

var factory = new AzureStorageDurabilityProviderFactory(
mockOptions,
storageAccountProvider,
nameResolver,
NullLoggerFactory.Instance,
TestHelpers.GetMockPlatformInformationService());

var provider = factory.GetDurabilityProvider();

Assert.Equal("Storage", provider.ConnectionName);
}
}
}
Loading