Skip to content

Commit

Permalink
Fix custom connection name not working when using IDurableClientFacto…
Browse files Browse the repository at this point in the history
…ry -> CreateClient(). (#2923)

Co-authored-by: Tan Han Chong <[email protected]>
  • Loading branch information
hctan and HanChong77 authored Oct 17, 2024
1 parent 48da8ad commit 820e9dd
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 1 deletion.
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);
}
}
}

0 comments on commit 820e9dd

Please sign in to comment.