From 917a5e62aa4ec275a91e6fe731ce78707f86c173 Mon Sep 17 00:00:00 2001 From: wangbill Date: Thu, 20 Aug 2026 11:33:15 -0400 Subject: [PATCH 1/7] Fix durable client current-app detection Compare durable client targets with the host default provider and let providers define TaskHub identity casing. Cover cross-connection routing and shared HTTP, gRPC, orchestrator, restart, and entity validation behavior. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --- .../AzureStorageDurabilityProvider.cs | 5 + .../ContextImplementations/DurableClient.cs | 7 +- .../DurabilityProvider.cs | 11 ++ .../Tests/Unit/DurableClientBaseTests.cs | 164 +++++++++++++----- .../Tests/Unit/HttpApiHandlerTests.cs | 48 ++++- .../Tests/Unit/LocalGrpcListenerTests.cs | 60 ++++++- 6 files changed, 248 insertions(+), 47 deletions(-) diff --git a/src/WebJobs.Extensions.DurableTask/AzureStorageDurabilityProvider.cs b/src/WebJobs.Extensions.DurableTask/AzureStorageDurabilityProvider.cs index 030d80947..314e6be2e 100644 --- a/src/WebJobs.Extensions.DurableTask/AzureStorageDurabilityProvider.cs +++ b/src/WebJobs.Extensions.DurableTask/AzureStorageDurabilityProvider.cs @@ -73,6 +73,11 @@ public AzureStorageDurabilityProvider( public override string EventSourceName { get; set; } = "DurableTask-AzureStorage"; + internal override bool TaskHubNameMatches(string taskHubName, string otherTaskHubName) + { + return string.Equals(taskHubName, otherTaskHubName, StringComparison.OrdinalIgnoreCase); + } + /// public async override Task> GetAllOrchestrationStates(CancellationToken cancellationToken) { diff --git a/src/WebJobs.Extensions.DurableTask/ContextImplementations/DurableClient.cs b/src/WebJobs.Extensions.DurableTask/ContextImplementations/DurableClient.cs index 4ff2b491c..1a0b84239 100644 --- a/src/WebJobs.Extensions.DurableTask/ContextImplementations/DurableClient.cs +++ b/src/WebJobs.Extensions.DurableTask/ContextImplementations/DurableClient.cs @@ -358,7 +358,8 @@ private async Task SignalEntityAsyncInternal(DurableClient durableClient, string private bool ClientReferencesCurrentApp(DurableClient client) { - return !client.attribute.ExternalClient && + return this.config != null && + !client.attribute.ExternalClient && this.TaskHubMatchesCurrentApp(client) && this.ConnectionNameMatchesCurrentApp(client); } @@ -374,12 +375,12 @@ internal void ThrowIfOrchestratorFunctionIsDisabled(string name) private bool TaskHubMatchesCurrentApp(DurableClient client) { var taskHubName = this.durableTaskOptions.HubName; - return client.TaskHubName.Equals(taskHubName); + return this.config.DefaultDurabilityProvider.TaskHubNameMatches(client.TaskHubName, taskHubName); } private bool ConnectionNameMatchesCurrentApp(DurableClient client) { - return this.DurabilityProvider.ConnectionNameMatches(client.DurabilityProvider); + return this.config.DefaultDurabilityProvider.ConnectionNameMatches(client.DurabilityProvider); } /// diff --git a/src/WebJobs.Extensions.DurableTask/DurabilityProvider.cs b/src/WebJobs.Extensions.DurableTask/DurabilityProvider.cs index 5d27d59d8..6e130fd36 100644 --- a/src/WebJobs.Extensions.DurableTask/DurabilityProvider.cs +++ b/src/WebJobs.Extensions.DurableTask/DurabilityProvider.cs @@ -616,6 +616,17 @@ internal virtual bool ConnectionNameMatches(DurabilityProvider durabilityProvide return this.ConnectionName.Equals(durabilityProvider.ConnectionName); } + /// + /// Returns true if two task hub names identify the same task hub for this durability provider. + /// + /// The first task hub name. + /// The second task hub name. + /// A boolean indicating whether the task hub names match. + internal virtual bool TaskHubNameMatches(string taskHubName, string otherTaskHubName) + { + return string.Equals(taskHubName, otherTaskHubName, StringComparison.Ordinal); + } + /// /// Tries to obtain a scale monitor for autoscaling. /// diff --git a/test/FunctionsV2/Tests/Unit/DurableClientBaseTests.cs b/test/FunctionsV2/Tests/Unit/DurableClientBaseTests.cs index 1ae8300b2..26c4d798e 100644 --- a/test/FunctionsV2/Tests/Unit/DurableClientBaseTests.cs +++ b/test/FunctionsV2/Tests/Unit/DurableClientBaseTests.cs @@ -82,11 +82,70 @@ public async Task StartNewAsync_MissingOrchestrator_ThrowsException() [Fact] [Trait("Category", PlatformSpecificHelpers.TestCategory)] - public async Task StartNewAsync_DifferentConnection_MissingOrchestrator_ThrowsException() + public async Task StartNewAsync_DifferentConnection_MissingOrchestrator_SchedulesInstance() { (DurableClient durableClient, Mock serviceClient, _) = CreateClient(); + await ((IDurableOrchestrationClient)durableClient).StartNewAsync("MissingOrchestrator"); + + serviceClient.Verify( + x => x.CreateTaskOrchestrationAsync(It.IsAny(), It.IsAny()), + Times.Once()); + } + + [Fact] + [Trait("Category", PlatformSpecificHelpers.TestCategory)] + public async Task StartNewAsync_UsesHostProviderConnectionMatching() + { + var serviceClient = new Mock(); + serviceClient + .Setup(x => x.CreateTaskOrchestrationAsync(It.IsAny(), It.IsAny())) + .Returns(Task.CompletedTask); + var targetProvider = new DurabilityProvider( + "targetProvider", + new Mock().Object, + serviceClient.Object, + TestConstants.ConnectionName); + var defaultProvider = new Mock( + "defaultProvider", + new Mock().Object, + new Mock().Object, + TestConstants.ConnectionName) + { + CallBase = true, + }; + defaultProvider + .Setup(provider => provider.ConnectionNameMatches(targetProvider)) + .Returns(false); + DurableTaskExtension extension = GetDurableTaskConfig(defaultProvider.Object); + var client = (IDurableOrchestrationClient)new DurableClient( + targetProvider, + extension, + extension.HttpApiHandler, + new DurableClientAttribute + { + TaskHub = "DurableTaskHub", + ConnectionName = TestConstants.ConnectionName, + }); + + await client.StartNewAsync("MissingOrchestrator"); + + defaultProvider.Verify( + provider => provider.ConnectionNameMatches(targetProvider), + Times.Exactly(2)); + serviceClient.Verify( + x => x.CreateTaskOrchestrationAsync(It.IsAny(), It.IsAny()), + Times.Once()); + } + + [Fact] + [Trait("Category", PlatformSpecificHelpers.TestCategory)] + public async Task StartNewAsync_AzureStorageCaseVariantTaskHub_MissingOrchestrator_ThrowsException() + { + (DurableClient durableClient, Mock serviceClient, _) = + CreateClient(taskHub: "durabletaskhub", connectionName: TestConstants.ConnectionName); + ArgumentException exception = await Assert.ThrowsAsync( () => ((IDurableOrchestrationClient)durableClient).StartNewAsync("MissingOrchestrator")); @@ -98,10 +157,13 @@ public async Task StartNewAsync_DifferentConnection_MissingOrchestrator_ThrowsEx [Fact] [Trait("Category", PlatformSpecificHelpers.TestCategory)] - public async Task StartNewAsync_CaseVariantTaskHub_MissingOrchestrator_SchedulesInstance() + public async Task StartNewAsync_ExactCaseProviderCaseVariantTaskHub_MissingOrchestrator_SchedulesInstance() { (DurableClient durableClient, Mock serviceClient, _) = - CreateClient(taskHub: "durabletaskhub", connectionName: TestConstants.ConnectionName); + CreateClient( + taskHub: "durabletaskhub", + connectionName: TestConstants.ConnectionName, + useExactCaseDefaultProvider: true); await ((IDurableOrchestrationClient)durableClient).StartNewAsync("MissingOrchestrator"); @@ -113,9 +175,9 @@ public async Task StartNewAsync_CaseVariantTaskHub_MissingOrchestrator_Schedules [Theory] [InlineData(null, true)] [InlineData("DurableTaskHub", true)] - [InlineData("durabletaskhub", false)] + [InlineData("durabletaskhub", true)] [Trait("Category", PlatformSpecificHelpers.TestCategory)] - public async Task StartNewAsync_DisabledOrchestrator_UsesLegacyTaskHubMatching( + public async Task StartNewAsync_DisabledOrchestrator_UsesAzureStorageTaskHubMatching( string taskHub, bool rejectStart) { @@ -154,27 +216,26 @@ public async Task StartNewAsync_DisabledOrchestrator_UsesLegacyTaskHubMatching( [Fact] [Trait("Category", PlatformSpecificHelpers.TestCategory)] - public async Task StartNewAsync_DifferentConnection_DisabledOrchestrator_ThrowsException() + public async Task StartNewAsync_DifferentConnection_DisabledOrchestrator_SchedulesInstance() { const string FunctionName = "DisabledOrchestrator"; (DurableClient durableClient, Mock serviceClient, DurableTaskExtension extension) = CreateClient(); extension.RegisterOrchestrator(new FunctionName(FunctionName), orchestratorInfo: null); - await Assert.ThrowsAsync( - () => ((IDurableOrchestrationClient)durableClient).StartNewAsync(FunctionName)); + await ((IDurableOrchestrationClient)durableClient).StartNewAsync(FunctionName); serviceClient.Verify( x => x.CreateTaskOrchestrationAsync(It.IsAny(), It.IsAny()), - Times.Never()); + Times.Once()); } [Theory] [InlineData(null, true)] [InlineData("DurableTaskHub", true)] - [InlineData("durabletaskhub", false)] + [InlineData("durabletaskhub", true)] [Trait("Category", PlatformSpecificHelpers.TestCategory)] - public async Task RestartAsync_DisabledOrchestrator_UsesLegacyTaskHubMatching( + public async Task RestartAsync_DisabledOrchestrator_UsesAzureStorageTaskHubMatching( string taskHub, bool rejectRestart) { @@ -228,7 +289,7 @@ public async Task RestartAsync_DisabledOrchestrator_UsesLegacyTaskHubMatching( [Fact] [Trait("Category", PlatformSpecificHelpers.TestCategory)] - public async Task RestartAsync_DifferentConnection_DisabledOrchestrator_ThrowsException() + public async Task RestartAsync_DifferentConnection_DisabledOrchestrator_SchedulesInstance() { const string InstanceId = "completed-instance"; const string FunctionName = "DisabledOrchestrator"; @@ -249,14 +310,13 @@ public async Task RestartAsync_DifferentConnection_DisabledOrchestrator_ThrowsEx }); extension.RegisterOrchestrator(new FunctionName(FunctionName), orchestratorInfo: null); - await Assert.ThrowsAsync( - () => ((IDurableOrchestrationClient)durableClient).RestartAsync( - InstanceId, - restartWithNewInstanceId: false)); + await ((IDurableOrchestrationClient)durableClient).RestartAsync( + InstanceId, + restartWithNewInstanceId: false); serviceClient.Verify( x => x.CreateTaskOrchestrationAsync(It.IsAny(), It.IsAny()), - Times.Never()); + Times.Once()); } [Theory] @@ -377,38 +437,38 @@ public async Task SignalEntity_InvalidEntityKey_ThrowsException(string entityKey [Fact] [Trait("Category", PlatformSpecificHelpers.TestCategory)] - public async Task SignalEntityAsync_DifferentConnection_MissingEntity_ThrowsException() + public async Task SignalEntityAsync_DifferentConnection_MissingEntity_SendsMessage() { (DurableClient durableClient, Mock serviceClient, _) = CreateClient(); - ArgumentException exception = await Assert.ThrowsAsync( - () => ((IDurableEntityClient)durableClient).SignalEntityAsync( - new EntityId("MissingEntity", "entity-key"), - "operation", - new { Value = 1 })); + await ((IDurableEntityClient)durableClient).SignalEntityAsync( + new EntityId("MissingEntity", "entity-key"), + "operation", + new { Value = 1 }); - Assert.Contains("doesn't exist, is disabled, or is not an entity function", exception.Message); serviceClient.Verify( x => x.SendTaskOrchestrationMessageAsync(It.IsAny()), - Times.Never()); + Times.Once()); } [Fact] [Trait("Category", PlatformSpecificHelpers.TestCategory)] - public async Task SignalEntityAsync_CaseVariantTaskHub_MissingEntity_SendsMessage() + public async Task SignalEntityAsync_AzureStorageCaseVariantTaskHub_MissingEntity_ThrowsException() { (DurableClient durableClient, Mock serviceClient, _) = CreateClient(taskHub: "durabletaskhub", connectionName: TestConstants.ConnectionName); - await ((IDurableEntityClient)durableClient).SignalEntityAsync( - new EntityId("MissingEntity", "entity-key"), - "operation", - new { Value = 1 }); + ArgumentException exception = await Assert.ThrowsAsync( + () => ((IDurableEntityClient)durableClient).SignalEntityAsync( + new EntityId("MissingEntity", "entity-key"), + "operation", + new { Value = 1 })); + Assert.Contains("doesn't exist, is disabled, or is not an entity function", exception.Message); serviceClient.Verify( x => x.SendTaskOrchestrationMessageAsync(It.IsAny()), - Times.Once()); + Times.Never()); } [Theory] @@ -681,7 +741,8 @@ private static ( DurableTaskExtension Extension) CreateClient( string taskHub = "DurableTaskHub", string connectionName = "TargetStorage", - bool externalClient = false) + bool externalClient = false, + bool useExactCaseDefaultProvider = false) { var serviceClient = new Mock(); serviceClient @@ -695,7 +756,14 @@ private static ( new Mock().Object, serviceClient.Object, connectionName); - var durableExtension = GetDurableTaskConfig(); + DurableTaskExtension durableExtension = useExactCaseDefaultProvider + ? GetDurableTaskConfig( + new DurabilityProvider( + "azureManaged", + new Mock().Object, + new Mock().Object, + TestConstants.ConnectionName)) + : GetDurableTaskConfig(); var attribute = new DurableClientAttribute { TaskHub = taskHub, @@ -808,7 +876,8 @@ private static List GetInstanceState(OrchestrationStatus sta }; } - private static DurableTaskExtension GetDurableTaskConfig() + private static DurableTaskExtension GetDurableTaskConfig( + Microsoft.Azure.WebJobs.Extensions.DurableTask.DurabilityProvider defaultProvider = null) { var options = new DurableTaskOptions(); options.HubName = "DurableTaskHub"; @@ -817,12 +886,27 @@ private static DurableTaskExtension GetDurableTaskConfig() var nameResolver = TestHelpers.GetTestNameResolver(); var clientProviderFactory = new TestStorageServiceClientProviderFactory(); var platformInformationService = TestHelpers.GetMockPlatformInformationService(); - var serviceFactory = new AzureStorageDurabilityProviderFactory( - wrappedOptions, - clientProviderFactory, - nameResolver, - NullLoggerFactory.Instance, - platformInformationService); + IDurabilityProviderFactory serviceFactory; + if (defaultProvider == null) + { + serviceFactory = new AzureStorageDurabilityProviderFactory( + wrappedOptions, + clientProviderFactory, + nameResolver, + NullLoggerFactory.Instance, + platformInformationService); + } + else + { + var serviceFactoryMock = new Mock(); + serviceFactoryMock.SetupGet(factory => factory.Name).Returns(AzureStorageDurabilityProviderFactory.ProviderName); + serviceFactoryMock.Setup(factory => factory.GetDurabilityProvider()).Returns(defaultProvider); + serviceFactoryMock + .Setup(factory => factory.GetDurabilityProvider(It.IsAny())) + .Returns(defaultProvider); + serviceFactory = serviceFactoryMock.Object; + } + return new DurableTaskExtension( wrappedOptions, new LoggerFactory(), diff --git a/test/FunctionsV2/Tests/Unit/HttpApiHandlerTests.cs b/test/FunctionsV2/Tests/Unit/HttpApiHandlerTests.cs index e4a31e767..1b0785727 100644 --- a/test/FunctionsV2/Tests/Unit/HttpApiHandlerTests.cs +++ b/test/FunctionsV2/Tests/Unit/HttpApiHandlerTests.cs @@ -2124,11 +2124,13 @@ public async Task StartNewInstance_Returns_HTTP_400_On_Disabled_Function() [Fact] [Trait("Category", PlatformSpecificHelpers.TestCategory)] - public async Task StartNewInstance_CaseVariantTaskHub_DoesNotRejectLocallyDisabledFunction() + public async Task StartNewInstance_DifferentConnection_DoesNotRejectLocallyDisabledFunction() { const string FunctionName = "DisabledOrchestrator"; + const string TargetConnectionName = "TargetStorage"; var requestUri = new Uri( - $"http://localhost/runtime/webhooks/durabletask/orchestrators/{FunctionName}?taskHub=testhubname"); + $"http://localhost/runtime/webhooks/durabletask/orchestrators/{FunctionName}" + + $"?taskHub={TestConstants.TaskHub}&connection={TargetConnectionName}"); var orchestrationServiceMock = new Mock(MockBehavior.Strict); var orchestrationServiceClientMock = new Mock(); orchestrationServiceClientMock @@ -2140,7 +2142,7 @@ public async Task StartNewInstance_CaseVariantTaskHub_DoesNotRejectLocallyDisabl "storageProviderName", orchestrationServiceMock.Object, orchestrationServiceClientMock.Object, - TestConstants.ConnectionName); + TargetConnectionName); var options = new DurableTaskOptions { WebhookUriProviderOverride = () => new Uri("http://localhost/runtime/webhooks/durabletask"), @@ -2162,6 +2164,46 @@ public async Task StartNewInstance_CaseVariantTaskHub_DoesNotRejectLocallyDisabl Times.Once); } + [Fact] + [Trait("Category", PlatformSpecificHelpers.TestCategory)] + public async Task StartNewInstance_AzureStorageCaseVariantTaskHub_RejectsLocallyDisabledFunction() + { + const string FunctionName = "DisabledOrchestrator"; + var requestUri = new Uri( + $"http://localhost/runtime/webhooks/durabletask/orchestrators/{FunctionName}?taskHub=samplehubvs"); + var orchestrationServiceMock = new Mock(MockBehavior.Strict); + var orchestrationServiceClientMock = new Mock(); + orchestrationServiceClientMock + .Setup(p => p.CreateTaskOrchestrationAsync( + It.IsAny(), + It.IsAny())) + .Returns(Task.CompletedTask); + var durabilityProvider = new DurabilityProvider( + "storageProviderName", + orchestrationServiceMock.Object, + orchestrationServiceClientMock.Object, + TestConstants.ConnectionName); + var options = new DurableTaskOptions + { + WebhookUriProviderOverride = () => new Uri("http://localhost/runtime/webhooks/durabletask"), + HubName = TestConstants.TaskHub, + }; + var extension = TestDurableTaskExtension.CreateWithProvider(options, durabilityProvider); + extension.RegisterOrchestrator(new FunctionName(FunctionName), orchestratorInfo: null); + var handler = new HttpApiHandler(extension, NullLogger.Instance); + + HttpResponseMessage response = await handler.HandleRequestAsync( + new HttpRequestMessage(HttpMethod.Post, requestUri), + CancellationToken.None); + + Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode); + orchestrationServiceClientMock.Verify( + p => p.CreateTaskOrchestrationAsync( + It.IsAny(), + It.IsAny()), + Times.Never); + } + private static DurableTaskExtension GetTestExtension() { var options = new DurableTaskOptions(); diff --git a/test/FunctionsV2/Tests/Unit/LocalGrpcListenerTests.cs b/test/FunctionsV2/Tests/Unit/LocalGrpcListenerTests.cs index 4d4bf8ce9..78edf4927 100644 --- a/test/FunctionsV2/Tests/Unit/LocalGrpcListenerTests.cs +++ b/test/FunctionsV2/Tests/Unit/LocalGrpcListenerTests.cs @@ -50,6 +50,7 @@ public enum TestGrpcListenerMode public class LocalGrpcListenerTests { + private const string ConnectionNameMetadataKey = "Durable-ConnectionName"; private const string TaskHubMetadataKey = "Durable-TaskHub"; // Host's configured hub for the task hub attribution test. Must be a constant so it can be @@ -315,7 +316,64 @@ public async Task TestGrpcListener_RejectsDisabledOrchestrator() [Fact] [Trait("Category", PlatformSpecificHelpers.TestCategory)] - public async Task TestGrpcListener_CaseVariantTaskHub_DoesNotRejectLocallyDisabledFunction() + public async Task TestGrpcListener_DifferentConnection_DoesNotRejectLocallyDisabledFunction() + { + const string FunctionName = "DisabledOrchestrator"; + const string HubName = "CurrentHub"; + const string TargetConnectionName = "TargetConnection"; + Mock targetProvider = CreateDurabilityProviderMock( + new Mock().Object, + new Mock().Object, + TargetConnectionName); + targetProvider + .Setup(provider => provider.CreateTaskOrchestrationAsync( + It.IsAny(), + It.IsAny(), + It.IsAny())) + .Returns(Task.CompletedTask); + DurabilityProvider defaultProvider = CreateDurabilityProvider( + new Mock().Object, + new Mock().Object); + using DurableTaskExtension extension = this.CreateExtension( + HubName, + targetProvider.Object, + defaultProvider); + extension.RegisterOrchestrator(new FunctionName(FunctionName), orchestratorInfo: null); + ILocalGrpcListener listener = LocalGrpcListener.Create( + extension, + LocalGrpcListenerMode.AspNetCore); + + try + { + await listener.StartAsync(default); + using GrpcChannel channel = GrpcChannel.ForAddress(listener.ListenAddress); + var client = new P.TaskHubSidecarService.TaskHubSidecarServiceClient(channel); + var headers = new Metadata + { + { TaskHubMetadataKey, HubName }, + { ConnectionNameMetadataKey, TargetConnectionName }, + }; + + await client.StartInstanceAsync( + new P.CreateInstanceRequest { Name = FunctionName }, + headers).ResponseAsync; + } + finally + { + await listener.StopAsync(default); + } + + targetProvider.Verify( + provider => provider.CreateTaskOrchestrationAsync( + It.IsAny(), + It.IsAny(), + It.IsAny()), + Times.Once); + } + + [Fact] + [Trait("Category", PlatformSpecificHelpers.TestCategory)] + public async Task TestGrpcListener_ExactCaseProviderCaseVariantTaskHub_DoesNotRejectLocallyDisabledFunction() { const string FunctionName = "RemoteOrchestrator"; Mock durabilityProvider = CreateDurabilityProviderMock( From f6bc0cf9fcf24e6f02189fdc75d76cab492e66c7 Mon Sep 17 00:00:00 2001 From: wangbill Date: Thu, 20 Aug 2026 19:35:52 -0400 Subject: [PATCH 2/7] Add Azure Storage current-app E2E coverage Exercise provider-aware TaskHub casing and cross-account routing with real Functions hosts against two isolated Azurite backends. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: 8dd495b5-0788-4ec5-bfdc-5dd9728bb509 --- .github/workflows/validate-build-e2e.yml | 26 +- .../FunctionApp/ClientFunctions.cs | 19 ++ .../CurrentAppDetectionFunctions.cs | 14 ++ .../Tests/E2E/CurrentAppDetectionTests.cs | 234 ++++++++++++++++++ 4 files changed, 282 insertions(+), 11 deletions(-) create mode 100644 test/FunctionsV2/FunctionApp/CurrentAppDetectionFunctions.cs create mode 100644 test/FunctionsV2/Tests/E2E/CurrentAppDetectionTests.cs diff --git a/.github/workflows/validate-build-e2e.yml b/.github/workflows/validate-build-e2e.yml index 82d4c5fe8..ce4536ccb 100644 --- a/.github/workflows/validate-build-e2e.yml +++ b/.github/workflows/validate-build-e2e.yml @@ -17,6 +17,7 @@ on: env: config: Release AzureWebJobsStorage: UseDevelopmentStorage=true + SecondaryStorage: 'DefaultEndpointsProtocol=http;AccountName=devstoreaccount1;AccountKey=Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==;BlobEndpoint=http://127.0.0.1:11000/devstoreaccount1;QueueEndpoint=http://127.0.0.1:11001/devstoreaccount1;TableEndpoint=http://127.0.0.1:11002/devstoreaccount1;' NPM_CONFIG_REGISTRY: https://registry.npmjs.org/ jobs: @@ -77,18 +78,21 @@ jobs: - name: Start Azurite shell: pwsh run: | - Start-Process (Resolve-Path '.azurite\node_modules\.bin\azurite.cmd') -ArgumentList '--silent','--skipApiVersionCheck','--blobPort','10000','--queuePort','10001','--tablePort','10002' + Start-Process (Resolve-Path '.azurite\node_modules\.bin\azurite.cmd') -ArgumentList '--silent','--skipApiVersionCheck','--location','.azurite-primary','--blobPort','10000','--queuePort','10001','--tablePort','10002' + Start-Process (Resolve-Path '.azurite\node_modules\.bin\azurite.cmd') -ArgumentList '--silent','--skipApiVersionCheck','--location','.azurite-secondary','--blobPort','11000','--queuePort','11001','--tablePort','11002' $timeout = 30 - for ($i = 1; $i -le $timeout; $i++) { - try { - $tcp = [System.Net.Sockets.TcpClient]::new('127.0.0.1', 10000) - $tcp.Dispose() - Write-Host "Azurite is ready" - break - } catch { - if ($i -eq $timeout) { throw "Azurite did not start within $timeout seconds" } - Write-Host "Waiting for Azurite ($i/$timeout)" - Start-Sleep -Seconds 1 + foreach ($port in 10000, 10001, 10002, 11000, 11001, 11002) { + for ($i = 1; $i -le $timeout; $i++) { + try { + $tcp = [System.Net.Sockets.TcpClient]::new('127.0.0.1', $port) + $tcp.Dispose() + Write-Host "Azurite on port $port is ready" + break + } catch { + if ($i -eq $timeout) { throw "Azurite on port $port did not start within $timeout seconds" } + Write-Host "Waiting for Azurite on port $port ($i/$timeout)" + Start-Sleep -Seconds 1 + } } } diff --git a/test/FunctionsV2/FunctionApp/ClientFunctions.cs b/test/FunctionsV2/FunctionApp/ClientFunctions.cs index db221d133..94e6518a4 100644 --- a/test/FunctionsV2/FunctionApp/ClientFunctions.cs +++ b/test/FunctionsV2/FunctionApp/ClientFunctions.cs @@ -45,6 +45,25 @@ public static async Task StartFunctionWithTaskHub( instanceCreationTime); } + // NOTE: The TestTaskHub and SecondaryStorage app setting names must exist for indexing. + [NoAutomaticTrigger] + public static async Task StartFunctionWithConnection( + [DurableClient(TaskHub = "%TestTaskHub%", ConnectionName = "SecondaryStorage")] IDurableClient client, + string functionName, + string instanceId, + object input, + TestDurableClient[] clientRef) + { + DateTime instanceCreationTime = DateTime.UtcNow; + + instanceId = await client.StartNewAsync(functionName, instanceId, input); + clientRef[0] = new TestDurableClient( + client, + functionName, + instanceId, + instanceCreationTime); + } + [NoAutomaticTrigger] public static void GetEntityClient( [DurableClient] IDurableClient client, diff --git a/test/FunctionsV2/FunctionApp/CurrentAppDetectionFunctions.cs b/test/FunctionsV2/FunctionApp/CurrentAppDetectionFunctions.cs new file mode 100644 index 000000000..06f543da8 --- /dev/null +++ b/test/FunctionsV2/FunctionApp/CurrentAppDetectionFunctions.cs @@ -0,0 +1,14 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the MIT License. See LICENSE in the project root for license information. + +namespace Microsoft.Azure.WebJobs.Extensions.DurableTask.Tests +{ + internal static class CurrentAppDetectionFunctions + { + public static string TargetOnlyOrchestrator( + [OrchestrationTrigger] IDurableOrchestrationContext context) + { + return $"target:{context.GetInput()}"; + } + } +} diff --git a/test/FunctionsV2/Tests/E2E/CurrentAppDetectionTests.cs b/test/FunctionsV2/Tests/E2E/CurrentAppDetectionTests.cs new file mode 100644 index 000000000..14b8a166a --- /dev/null +++ b/test/FunctionsV2/Tests/E2E/CurrentAppDetectionTests.cs @@ -0,0 +1,234 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the MIT License. See LICENSE in the project root for license information. + +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Threading.Tasks; +using DurableTask.AzureStorage; +using Microsoft.Azure.WebJobs.Host; +using Microsoft.Azure.WebJobs.Host.TestCommon; +using Xunit; +using Xunit.Abstractions; + +namespace Microsoft.Azure.WebJobs.Extensions.DurableTask.Tests +{ + [Trait("TestType", "E2E")] + public class CurrentAppDetectionTests + { + private const string SecondaryStorageSettingName = "SecondaryStorage"; + + private readonly ITestOutputHelper output; + private readonly TestLoggerProvider loggerProvider; + + public CurrentAppDetectionTests(ITestOutputHelper output) + { + this.output = output; + this.loggerProvider = new TestLoggerProvider(output); + } + + [Fact] + [Trait("Category", PlatformSpecificHelpers.TestCategory)] + public async Task AzureStorage_CaseVariantTaskHub_DisabledOrchestratorIsRejected() + { + string taskHubName = TestHelpers.GetTaskHubNameFromTestName( + nameof(this.AzureStorage_CaseVariantTaskHub_DisabledOrchestratorIsRejected), + enableExtendedSessions: false); + string caseVariantTaskHub = taskHubName.ToUpperInvariant(); + string instanceId = $"case-disabled-{Guid.NewGuid():N}"; + var nameResolver = new SimpleNameResolver( + new Dictionary + { + { "TestTaskHub", caseVariantTaskHub }, + }); + + try + { + using ITestHost host = TestHelpers.GetJobHost( + this.loggerProvider, + nameof(this.AzureStorage_CaseVariantTaskHub_DisabledOrchestratorIsRejected), + enableExtendedSessions: false, + nameResolver: nameResolver, + storageProviderType: TestHelpers.AzureStorageProviderType, + exactTaskHubName: taskHubName); + await host.StartAsync(); + + Exception exception = await Record.ExceptionAsync( + () => host.StartOrchestratorAsync( + nameof(TestOrchestrations.DisabledOrchestrator), + input: null, + this.output, + instanceId, + useTaskHubFromAppSettings: true)); + + IDurableOrchestrationClient defaultClient = + await host.GetOrchestrationClientBindingTest(this.output); + if (exception == null) + { + DurableOrchestrationStatus persistedStatus = + await WaitForInstanceAsync(defaultClient, instanceId); + Assert.Fail( + $"The case-variant client accepted and persisted the disabled orchestration " + + $"with status {persistedStatus.RuntimeStatus}."); + } + + FunctionInvocationException invocationException = + Assert.IsType(exception); + Assert.Contains( + "doesn't exist, is disabled, or is not an orchestrator function", + invocationException.InnerException?.ToString()); + await AssertInstanceRemainsAbsentAsync(defaultClient, instanceId); + + await host.StopAsync(); + } + finally + { + await DeleteTaskHubAsync(taskHubName, TestHelpers.GetStorageConnectionString()); + } + } + + [Fact] + [Trait("Category", PlatformSpecificHelpers.TestCategory)] + public async Task AzureStorage_DifferentConnection_SameTaskHub_ReachesTargetBackend() + { + string taskHubName = TestHelpers.GetTaskHubNameFromTestName( + nameof(this.AzureStorage_DifferentConnection_SameTaskHub_ReachesTargetBackend), + enableExtendedSessions: false); + string instanceId = $"cross-account-{Guid.NewGuid():N}"; + string secondaryConnection = GetSecondaryStorageConnectionString(); + var nameResolver = new SimpleNameResolver( + new Dictionary + { + { "TestTaskHub", taskHubName }, + { SecondaryStorageSettingName, secondaryConnection }, + }); + var targetOptions = new DurableTaskOptions + { + HubName = taskHubName, + }; + targetOptions.StorageProvider["ConnectionName"] = SecondaryStorageSettingName; + + try + { + using ITestHost sourceHost = TestHelpers.GetJobHost( + this.loggerProvider, + nameof(this.AzureStorage_DifferentConnection_SameTaskHub_ReachesTargetBackend), + enableExtendedSessions: false, + nameResolver: nameResolver, + storageProviderType: TestHelpers.AzureStorageProviderType, + exactTaskHubName: taskHubName, + types: new[] { typeof(ClientFunctions) }); + using ITestHost targetHost = TestHelpers.GetJobHostWithOptions( + this.loggerProvider, + targetOptions, + nameResolver: nameResolver, + types: new[] { typeof(CurrentAppDetectionFunctions) }); + + await targetHost.StartAsync(); + await sourceHost.StartAsync(); + + TestDurableClient targetClient = await this.StartOnSecondaryStorageAsync( + sourceHost, + nameof(CurrentAppDetectionFunctions.TargetOnlyOrchestrator), + input: "completed", + instanceId); + DurableOrchestrationStatus targetStatus = + await targetClient.WaitForCompletionAsync(this.output); + + Assert.Equal(OrchestrationRuntimeStatus.Completed, targetStatus.RuntimeStatus); + Assert.Equal("target:completed", targetStatus.Output); + + IDurableOrchestrationClient sourceClient = + await sourceHost.GetOrchestrationClientBindingTest(this.output); + await AssertInstanceRemainsAbsentAsync(sourceClient, instanceId); + + await sourceHost.StopAsync(); + await targetHost.StopAsync(); + } + finally + { + await Task.WhenAll( + DeleteTaskHubAsync(taskHubName, TestHelpers.GetStorageConnectionString()), + DeleteTaskHubAsync(taskHubName, secondaryConnection)); + } + } + + private static async Task AssertInstanceRemainsAbsentAsync( + IDurableOrchestrationClient client, + string instanceId) + { + int observations = 0; + Stopwatch stopwatch = Stopwatch.StartNew(); + do + { + Assert.Null(await client.GetStatusAsync(instanceId)); + observations++; + await Task.Delay(TimeSpan.FromMilliseconds(200)); + } + while (stopwatch.Elapsed < TimeSpan.FromSeconds(2)); + + Assert.True(observations >= 3); + } + + private async Task StartOnSecondaryStorageAsync( + ITestHost host, + string functionName, + object input, + string instanceId) + { + var clientRef = new TestDurableClient[1]; + var arguments = new Dictionary + { + { "functionName", functionName }, + { "instanceId", instanceId }, + { "input", input }, + { "clientRef", clientRef }, + }; + + await host.CallAsync( + typeof(ClientFunctions).GetMethod(nameof(ClientFunctions.StartFunctionWithConnection)), + arguments); + this.output.WriteLine($"Started {functionName}, Instance ID = {clientRef[0].InstanceId}"); + return clientRef[0]; + } + + private static async Task WaitForInstanceAsync( + IDurableOrchestrationClient client, + string instanceId) + { + Stopwatch stopwatch = Stopwatch.StartNew(); + do + { + DurableOrchestrationStatus status = await client.GetStatusAsync(instanceId); + if (status != null) + { + return status; + } + + await Task.Delay(TimeSpan.FromMilliseconds(200)); + } + while (stopwatch.Elapsed < TimeSpan.FromSeconds(30)); + + throw new TimeoutException($"Instance '{instanceId}' was accepted but did not persist."); + } + + private static Task DeleteTaskHubAsync(string taskHubName, string connectionString) + { + var settings = new AzureStorageOrchestrationServiceSettings + { + TaskHubName = taskHubName, + StorageAccountClientProvider = new StorageAccountClientProvider(connectionString), + }; + return new AzureStorageOrchestrationService(settings).DeleteAsync(); + } + + private static string GetSecondaryStorageConnectionString() + { + string connectionString = Environment.GetEnvironmentVariable(SecondaryStorageSettingName); + return !string.IsNullOrWhiteSpace(connectionString) + ? connectionString + : throw new InvalidOperationException( + $"The {SecondaryStorageSettingName} environment variable must point to the secondary Azurite instance."); + } + } +} From 7ca55a13fdb220fb85370db370a7e9faf153b11e Mon Sep 17 00:00:00 2001 From: wangbill Date: Thu, 20 Aug 2026 21:11:09 -0400 Subject: [PATCH 3/7] Configure secondary Azurite in public E2E CI Mirror the GitHub FunctionsV2 E2E setup in the merged Azure Pipelines job while leaving analyzer CI unchanged. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: 8dd495b5-0788-4ec5-bfdc-5dd9728bb509 --- .../templates/public/windows-validation.yml | 24 +++++++++++-------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/eng/ci/templates/public/windows-validation.yml b/eng/ci/templates/public/windows-validation.yml index 3ce52f2d6..2e5322e6a 100644 --- a/eng/ci/templates/public/windows-validation.yml +++ b/eng/ci/templates/public/windows-validation.yml @@ -158,6 +158,7 @@ jobs: testTargetFramework: net10.0 variables: AzureWebJobsStorage: UseDevelopmentStorage=true + SecondaryStorage: 'DefaultEndpointsProtocol=http;AccountName=devstoreaccount1;AccountKey=Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==;BlobEndpoint=http://127.0.0.1:11000/devstoreaccount1;QueueEndpoint=http://127.0.0.1:11001/devstoreaccount1;TableEndpoint=http://127.0.0.1:11002/devstoreaccount1;' steps: - checkout: self clean: true @@ -195,16 +196,19 @@ jobs: if ($LASTEXITCODE) { exit $LASTEXITCODE } $azurite = Resolve-Path 'node_modules/.bin/azurite.cmd' - Start-Process $azurite -ArgumentList '--silent','--skipApiVersionCheck','--blobPort','10000','--queuePort','10001','--tablePort','10002' - for ($i = 1; $i -le 30; $i++) { - try { - $tcp = [System.Net.Sockets.TcpClient]::new('127.0.0.1', 10000) - $tcp.Dispose() - Write-Host 'Azurite is ready' - break - } catch { - if ($i -eq 30) { throw 'Azurite did not start within 30 seconds' } - Start-Sleep -Seconds 1 + Start-Process $azurite -ArgumentList '--silent','--skipApiVersionCheck','--location','.azurite-primary','--blobPort','10000','--queuePort','10001','--tablePort','10002' + Start-Process $azurite -ArgumentList '--silent','--skipApiVersionCheck','--location','.azurite-secondary','--blobPort','11000','--queuePort','11001','--tablePort','11002' + foreach ($port in 10000, 10001, 10002, 11000, 11001, 11002) { + for ($i = 1; $i -le 30; $i++) { + try { + $tcp = [System.Net.Sockets.TcpClient]::new('127.0.0.1', $port) + $tcp.Dispose() + Write-Host "Azurite on port $port is ready" + break + } catch { + if ($i -eq 30) { throw "Azurite on port $port did not start within 30 seconds" } + Start-Sleep -Seconds 1 + } } } displayName: Start Azurite From c6d5767142efee9b6ded25ba242f3fc04ef724e9 Mon Sep 17 00:00:00 2001 From: wangbill Date: Fri, 21 Aug 2026 15:08:37 -0400 Subject: [PATCH 4/7] Dispose Azure Storage service in E2E cleanup Await task hub deletion before deterministically disposing the locally-created orchestration service. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: 8dd495b5-0788-4ec5-bfdc-5dd9728bb509 --- test/FunctionsV2/Tests/E2E/CurrentAppDetectionTests.cs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/test/FunctionsV2/Tests/E2E/CurrentAppDetectionTests.cs b/test/FunctionsV2/Tests/E2E/CurrentAppDetectionTests.cs index 14b8a166a..c76fd5cf1 100644 --- a/test/FunctionsV2/Tests/E2E/CurrentAppDetectionTests.cs +++ b/test/FunctionsV2/Tests/E2E/CurrentAppDetectionTests.cs @@ -212,14 +212,15 @@ private static async Task WaitForInstanceAsync( throw new TimeoutException($"Instance '{instanceId}' was accepted but did not persist."); } - private static Task DeleteTaskHubAsync(string taskHubName, string connectionString) + private static async Task DeleteTaskHubAsync(string taskHubName, string connectionString) { var settings = new AzureStorageOrchestrationServiceSettings { TaskHubName = taskHubName, StorageAccountClientProvider = new StorageAccountClientProvider(connectionString), }; - return new AzureStorageOrchestrationService(settings).DeleteAsync(); + using var service = new AzureStorageOrchestrationService(settings); + await service.DeleteAsync(); } private static string GetSecondaryStorageConnectionString() From 6af50cb2bc4008d8e80431a7636a16d773544470 Mon Sep 17 00:00:00 2001 From: wangbill Date: Fri, 21 Aug 2026 16:15:15 -0400 Subject: [PATCH 5/7] Use provider hub semantics for client selection Preserve exact-case task hub identity when explicit entity operations select a target client. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: 8dd495b5-0788-4ec5-bfdc-5dd9728bb509 --- .../ContextImplementations/DurableClient.cs | 2 +- .../Tests/Unit/DurableClientBaseTests.cs | 70 ++++++++++++++++++- 2 files changed, 69 insertions(+), 3 deletions(-) diff --git a/src/WebJobs.Extensions.DurableTask/ContextImplementations/DurableClient.cs b/src/WebJobs.Extensions.DurableTask/ContextImplementations/DurableClient.cs index 1a0b84239..22343e43d 100644 --- a/src/WebJobs.Extensions.DurableTask/ContextImplementations/DurableClient.cs +++ b/src/WebJobs.Extensions.DurableTask/ContextImplementations/DurableClient.cs @@ -104,7 +104,7 @@ private bool CheckStatusBeforeRaiseEvent private IDurableClient GetDurableClient(string taskHubName, string connectionName) { - if (string.Equals(this.TaskHubName, taskHubName, StringComparison.OrdinalIgnoreCase) + if (this.DurabilityProvider.TaskHubNameMatches(this.TaskHubName, taskHubName) && string.Equals(this.attribute.ConnectionName, connectionName, StringComparison.OrdinalIgnoreCase)) { return this; diff --git a/test/FunctionsV2/Tests/Unit/DurableClientBaseTests.cs b/test/FunctionsV2/Tests/Unit/DurableClientBaseTests.cs index 26c4d798e..7e89ba5d0 100644 --- a/test/FunctionsV2/Tests/Unit/DurableClientBaseTests.cs +++ b/test/FunctionsV2/Tests/Unit/DurableClientBaseTests.cs @@ -471,6 +471,71 @@ public async Task SignalEntityAsync_AzureStorageCaseVariantTaskHub_MissingEntity Times.Never()); } + [Theory] + [InlineData(false)] + [InlineData(true)] + [Trait("Category", PlatformSpecificHelpers.TestCategory)] + public async Task SignalEntityAsync_ExactCaseProviderCaseVariantExplicitTaskHub_UsesRequestedClient( + bool scheduled) + { + var currentServiceClient = new Mock(); + currentServiceClient + .Setup(client => client.SendTaskOrchestrationMessageAsync(It.IsAny())) + .Returns(Task.CompletedTask); + var targetServiceClient = new Mock(); + targetServiceClient + .Setup(client => client.SendTaskOrchestrationMessageAsync(It.IsAny())) + .Returns(Task.CompletedTask); + var currentProvider = new DurabilityProvider( + "exactCaseProvider", + new Mock().Object, + currentServiceClient.Object, + TestConstants.ConnectionName); + var targetProvider = new DurabilityProvider( + "exactCaseProvider", + new Mock().Object, + targetServiceClient.Object, + TestConstants.ConnectionName); + DurableTaskExtension extension = GetDurableTaskConfig(targetProvider, "CurrentHub"); + var client = (IDurableEntityClient)new DurableClient( + currentProvider, + extension, + extension.HttpApiHandler, + new DurableClientAttribute + { + TaskHub = "CurrentHub", + ConnectionName = TestConstants.ConnectionName, + }); + var entityId = new EntityId("MissingEntity", "entity-key"); + + if (scheduled) + { + await client.SignalEntityAsync( + entityId, + DateTime.UtcNow.AddMinutes(1), + "operation", + operationInput: null, + taskHubName: "currenthub", + connectionName: TestConstants.ConnectionName); + } + else + { + await client.SignalEntityAsync( + entityId, + "operation", + operationInput: null, + taskHubName: "currenthub", + connectionName: TestConstants.ConnectionName); + } + + currentServiceClient.Verify( + serviceClient => serviceClient.SendTaskOrchestrationMessageAsync(It.IsAny()), + Times.Never()); + targetServiceClient.Verify( + serviceClient => serviceClient.SendTaskOrchestrationMessageAsync(It.IsAny()), + Times.Once()); + } + [Theory] [Trait("Category", PlatformSpecificHelpers.TestCategory)] [InlineData(false)] @@ -877,10 +942,11 @@ private static List GetInstanceState(OrchestrationStatus sta } private static DurableTaskExtension GetDurableTaskConfig( - Microsoft.Azure.WebJobs.Extensions.DurableTask.DurabilityProvider defaultProvider = null) + Microsoft.Azure.WebJobs.Extensions.DurableTask.DurabilityProvider defaultProvider = null, + string taskHubName = "DurableTaskHub") { var options = new DurableTaskOptions(); - options.HubName = "DurableTaskHub"; + options.HubName = taskHubName; options.WebhookUriProviderOverride = () => new Uri("https://sampleurl.net"); var wrappedOptions = new OptionsWrapper(options); var nameResolver = TestHelpers.GetTestNameResolver(); From 8b7b18b443fbd1fe1e87af693e88528f7268dbe7 Mon Sep 17 00:00:00 2001 From: wangbill Date: Fri, 21 Aug 2026 16:47:50 -0400 Subject: [PATCH 6/7] Preserve provider identity in durable client caches Use an internal ordinal cache key for provider-relevant configuration while restoring the public attribute equality/hash contract. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: 8dd495b5-0788-4ec5-bfdc-5dd9728bb509 --- .../DurableClientFactory.cs | 16 +-- .../DurableClientAttribute.cs | 11 +- .../DurableClientCacheKey.cs | 53 +++++++ .../DurableTaskExtension.cs | 14 +- .../Tests/Unit/DurableClientBaseTests.cs | 131 +++++++++++++++++- 5 files changed, 205 insertions(+), 20 deletions(-) create mode 100644 src/WebJobs.Extensions.DurableTask/DurableClientCacheKey.cs diff --git a/src/WebJobs.Extensions.DurableTask/ContextImplementations/DurableClientFactory.cs b/src/WebJobs.Extensions.DurableTask/ContextImplementations/DurableClientFactory.cs index 9fa3c901f..8d046981f 100644 --- a/src/WebJobs.Extensions.DurableTask/ContextImplementations/DurableClientFactory.cs +++ b/src/WebJobs.Extensions.DurableTask/ContextImplementations/DurableClientFactory.cs @@ -15,13 +15,12 @@ namespace Microsoft.Azure.WebJobs.Extensions.DurableTask.ContextImplementations /// public class DurableClientFactory : IDurableClientFactory, IDisposable { - // Creating client objects is expensive, so we cache them when the attributes match. - // Note that DurableClientAttribute defines a custom equality comparer. - private readonly ConcurrentDictionary cachedClients = - new ConcurrentDictionary(); + // Creating client objects is expensive, so we cache them when their semantic configuration matches. + private readonly ConcurrentDictionary cachedClients = + new ConcurrentDictionary(); - private readonly ConcurrentDictionary cachedHttpListeners = - new ConcurrentDictionary(); + private readonly ConcurrentDictionary cachedHttpListeners = + new ConcurrentDictionary(); private readonly DurableClientOptions defaultDurableClientOptions; private readonly DurableTaskOptions durableTaskOptions; @@ -76,9 +75,10 @@ public IDurableClient CreateClient(DurableClientOptions durableClientOptions) DurableClientAttribute attribute = new DurableClientAttribute(durableClientOptions); + var cacheKey = new DurableClientCacheKey(attribute); DurableClient client = this.cachedClients.GetOrAdd( - attribute, - attr => + cacheKey, + _ => { DurabilityProvider innerClient = this.durabilityProviderFactory.GetDurabilityProvider(attribute); return new DurableClient(innerClient, null, attribute, this.MessageDataConverter, this.TraceHelper, this.durableTaskOptions, this); diff --git a/src/WebJobs.Extensions.DurableTask/DurableClientAttribute.cs b/src/WebJobs.Extensions.DurableTask/DurableClientAttribute.cs index 9b59b40d2..44c200d4d 100644 --- a/src/WebJobs.Extensions.DurableTask/DurableClientAttribute.cs +++ b/src/WebJobs.Extensions.DurableTask/DurableClientAttribute.cs @@ -82,9 +82,14 @@ public override int GetHashCode() { unchecked { - return - this.TaskHub?.GetHashCode() ?? 0 + - this.ConnectionName?.GetHashCode() ?? 0; + int hashCode = 17; + hashCode = (hashCode * 31) + (this.TaskHub == null + ? 0 + : StringComparer.OrdinalIgnoreCase.GetHashCode(this.TaskHub)); + hashCode = (hashCode * 31) + (this.ConnectionName == null + ? 0 + : StringComparer.OrdinalIgnoreCase.GetHashCode(this.ConnectionName)); + return hashCode; } } diff --git a/src/WebJobs.Extensions.DurableTask/DurableClientCacheKey.cs b/src/WebJobs.Extensions.DurableTask/DurableClientCacheKey.cs new file mode 100644 index 000000000..851177c67 --- /dev/null +++ b/src/WebJobs.Extensions.DurableTask/DurableClientCacheKey.cs @@ -0,0 +1,53 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the MIT License. See LICENSE in the project root for license information. + +using System; + +namespace Microsoft.Azure.WebJobs.Extensions.DurableTask +{ + internal readonly struct DurableClientCacheKey : IEquatable + { + private readonly string taskHub; + private readonly string connectionName; + private readonly bool externalClient; + private readonly bool durableRequiresGrpc; + + public DurableClientCacheKey(DurableClientAttribute attribute) + { + this.taskHub = attribute.TaskHub; + this.connectionName = attribute.ConnectionName; + this.externalClient = attribute.ExternalClient; + this.durableRequiresGrpc = attribute.DurableRequiresGrpc; + } + + public bool Equals(DurableClientCacheKey other) + { + return string.Equals(this.taskHub, other.taskHub, StringComparison.Ordinal) + && string.Equals(this.connectionName, other.connectionName, StringComparison.Ordinal) + && this.externalClient == other.externalClient + && this.durableRequiresGrpc == other.durableRequiresGrpc; + } + + public override bool Equals(object obj) + { + return obj is DurableClientCacheKey other && this.Equals(other); + } + + public override int GetHashCode() + { + unchecked + { + int hashCode = 17; + hashCode = (hashCode * 31) + (this.taskHub == null + ? 0 + : StringComparer.Ordinal.GetHashCode(this.taskHub)); + hashCode = (hashCode * 31) + (this.connectionName == null + ? 0 + : StringComparer.Ordinal.GetHashCode(this.connectionName)); + hashCode = (hashCode * 31) + this.externalClient.GetHashCode(); + hashCode = (hashCode * 31) + this.durableRequiresGrpc.GetHashCode(); + return hashCode; + } + } + } +} diff --git a/src/WebJobs.Extensions.DurableTask/DurableTaskExtension.cs b/src/WebJobs.Extensions.DurableTask/DurableTaskExtension.cs index 6974b80ef..16b802174 100644 --- a/src/WebJobs.Extensions.DurableTask/DurableTaskExtension.cs +++ b/src/WebJobs.Extensions.DurableTask/DurableTaskExtension.cs @@ -47,10 +47,9 @@ public class DurableTaskExtension : internal static readonly string LoggerCategoryName = LogCategories.CreateTriggerCategory("DurableTask"); - // Creating client objects is expensive, so we cache them when the attributes match. - // Note that DurableClientAttribute defines a custom equality comparer. - private readonly ConcurrentDictionary cachedClients = - new ConcurrentDictionary(); + // Creating client objects is expensive, so we cache them when their semantic configuration matches. + private readonly ConcurrentDictionary cachedClients = + new ConcurrentDictionary(); private readonly ConcurrentDictionary knownOrchestrators = new ConcurrentDictionary(); @@ -1379,12 +1378,13 @@ protected internal virtual IDurableClient GetClient(DurableClientAttribute attri // We must ensure the TaskHubWorker exists so that we know we have started the appropriate server. _ = this.EnsureTaskHubWorker(); + var cacheKey = new DurableClientCacheKey(attribute); DurableClient client = this.cachedClients.GetOrAdd( - attribute, - attr => + cacheKey, + _ => { DurabilityProvider innerClient = this.durabilityProviderFactory.GetDurabilityProvider(attribute); - return new DurableClient(innerClient, this, this.HttpApiHandler, attr); + return new DurableClient(innerClient, this, this.HttpApiHandler, attribute); }); return client; diff --git a/test/FunctionsV2/Tests/Unit/DurableClientBaseTests.cs b/test/FunctionsV2/Tests/Unit/DurableClientBaseTests.cs index 7e89ba5d0..711ebd6a7 100644 --- a/test/FunctionsV2/Tests/Unit/DurableClientBaseTests.cs +++ b/test/FunctionsV2/Tests/Unit/DurableClientBaseTests.cs @@ -31,6 +31,25 @@ public DurableClientBaseTests(ITestOutputHelper output) this.output = output; } + [Fact] + [Trait("Category", PlatformSpecificHelpers.TestCategory)] + public void DurableClientAttribute_CaseVariantEqualValues_HaveEqualHashCodes() + { + var current = new DurableClientAttribute + { + TaskHub = "CurrentHub", + ConnectionName = "Storage", + }; + var caseVariant = new DurableClientAttribute + { + TaskHub = "currenthub", + ConnectionName = "storage", + }; + + Assert.Equal(current, caseVariant); + Assert.Equal(current.GetHashCode(), caseVariant.GetHashCode()); + } + [Theory] [Trait("Category", PlatformSpecificHelpers.TestCategory)] [InlineData("@invalid")] @@ -536,6 +555,112 @@ await client.SignalEntityAsync( Times.Once()); } + [Fact] + [Trait("Category", PlatformSpecificHelpers.TestCategory)] + public async Task SignalEntityAsync_ExactCaseProviderCaseVariantTaskHub_UsesRequestedCachedClient() + { + var currentServiceClient = new Mock(); + currentServiceClient + .Setup(client => client.SendTaskOrchestrationMessageAsync(It.IsAny())) + .Returns(Task.CompletedTask); + var targetServiceClient = new Mock(); + targetServiceClient + .Setup(client => client.SendTaskOrchestrationMessageAsync(It.IsAny())) + .Returns(Task.CompletedTask); + var currentProvider = new DurabilityProvider( + "exactCaseProvider", + new Mock().Object, + currentServiceClient.Object, + TestConstants.ConnectionName); + var targetProvider = new DurabilityProvider( + "exactCaseProvider", + new Mock().Object, + targetServiceClient.Object, + TestConstants.ConnectionName); + DurableTaskExtension extension = GetDurableTaskConfig( + currentProvider, + "CurrentHub", + attribute => string.Equals(attribute.TaskHub, "CurrentHub", StringComparison.Ordinal) + ? currentProvider + : targetProvider); + var currentClient = (IDurableEntityClient)extension.GetClient( + new DurableClientAttribute + { + TaskHub = "CurrentHub", + ConnectionName = TestConstants.ConnectionName, + }); + + await currentClient.SignalEntityAsync( + new EntityId("MissingEntity", "entity-key"), + "operation", + operationInput: null, + taskHubName: "currenthub", + connectionName: TestConstants.ConnectionName); + + currentServiceClient.Verify( + serviceClient => serviceClient.SendTaskOrchestrationMessageAsync(It.IsAny()), + Times.Never()); + targetServiceClient.Verify( + serviceClient => serviceClient.SendTaskOrchestrationMessageAsync(It.IsAny()), + Times.Once()); + } + + [Theory] + [InlineData("currenthub", false)] + [InlineData("CurrentHub", true)] + [Trait("Category", PlatformSpecificHelpers.TestCategory)] + public void DurableClientFactory_DistinctSemanticAttributes_ReturnDistinctCachedClients( + string secondTaskHub, + bool secondExternalClient) + { + var currentProvider = new DurabilityProvider( + "exactCaseProvider", + new Mock().Object, + new Mock().Object, + TestConstants.ConnectionName); + var targetProvider = new DurabilityProvider( + "exactCaseProvider", + new Mock().Object, + new Mock().Object, + TestConstants.ConnectionName); + var providerFactory = new Mock(); + providerFactory + .Setup(factory => factory.GetDurabilityProvider(It.IsAny())) + .Returns((DurableClientAttribute attribute) => + string.Equals(attribute.TaskHub, "CurrentHub", StringComparison.Ordinal) + && !attribute.ExternalClient + ? currentProvider + : targetProvider); + using var factory = new ContextImplementations.DurableClientFactory( + Microsoft.Extensions.Options.Options.Create( + new DurableClientOptions + { + TaskHub = "CurrentHub", + ConnectionName = TestConstants.ConnectionName, + }), + Microsoft.Extensions.Options.Options.Create(new DurableTaskOptions { HubName = "CurrentHub" }), + providerFactory.Object, + NullLoggerFactory.Instance); + + IDurableClient currentClient = factory.CreateClient( + new DurableClientOptions + { + TaskHub = "CurrentHub", + ConnectionName = TestConstants.ConnectionName, + IsExternalClient = false, + }); + IDurableClient targetClient = factory.CreateClient( + new DurableClientOptions + { + TaskHub = secondTaskHub, + ConnectionName = TestConstants.ConnectionName, + IsExternalClient = secondExternalClient, + }); + + Assert.NotSame(currentClient, targetClient); + Assert.Same(targetProvider, ((DurableClient)targetClient).DurabilityProvider); + } + [Theory] [Trait("Category", PlatformSpecificHelpers.TestCategory)] [InlineData(false)] @@ -943,7 +1068,8 @@ private static List GetInstanceState(OrchestrationStatus sta private static DurableTaskExtension GetDurableTaskConfig( Microsoft.Azure.WebJobs.Extensions.DurableTask.DurabilityProvider defaultProvider = null, - string taskHubName = "DurableTaskHub") + string taskHubName = "DurableTaskHub", + Func providerSelector = null) { var options = new DurableTaskOptions(); options.HubName = taskHubName; @@ -969,7 +1095,8 @@ private static DurableTaskExtension GetDurableTaskConfig( serviceFactoryMock.Setup(factory => factory.GetDurabilityProvider()).Returns(defaultProvider); serviceFactoryMock .Setup(factory => factory.GetDurabilityProvider(It.IsAny())) - .Returns(defaultProvider); + .Returns((DurableClientAttribute attribute) => + providerSelector?.Invoke(attribute) ?? defaultProvider); serviceFactory = serviceFactoryMock.Object; } From b31f039f44aba2f809c68ca75578457cbe45bb55 Mon Sep 17 00:00:00 2001 From: wangbill Date: Fri, 11 Sep 2026 12:02:16 -0700 Subject: [PATCH 7/7] test: cover hosted durable client cache modes Add hosted regression coverage for both internal/external client creation orders (#1926). Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: 4f73040c-3459-4728-8c53-8e481e672736 --- .../FunctionApp/HostedClientCacheFunctions.cs | 26 +++++ .../Tests/E2E/CurrentAppDetectionTests.cs | 101 ++++++++++++++++++ 2 files changed, 127 insertions(+) create mode 100644 test/FunctionsV2/FunctionApp/HostedClientCacheFunctions.cs diff --git a/test/FunctionsV2/FunctionApp/HostedClientCacheFunctions.cs b/test/FunctionsV2/FunctionApp/HostedClientCacheFunctions.cs new file mode 100644 index 000000000..52fcfd308 --- /dev/null +++ b/test/FunctionsV2/FunctionApp/HostedClientCacheFunctions.cs @@ -0,0 +1,26 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the MIT License. See LICENSE in the project root for license information. + +namespace Microsoft.Azure.WebJobs.Extensions.DurableTask.Tests +{ + internal static class HostedClientCacheFunctions + { + [NoAutomaticTrigger] + public static void CaptureInternalClient( + string hub, + IDurableClient[] clients, + [DurableClient(TaskHub = "{hub}")] IDurableClient client) + { + clients[0] = client; + } + + [NoAutomaticTrigger] + public static void CaptureExternalClient( + string hub, + IDurableClient[] clients, + [DurableClient(TaskHub = "{hub}", ExternalClient = true)] IDurableClient client) + { + clients[0] = client; + } + } +} diff --git a/test/FunctionsV2/Tests/E2E/CurrentAppDetectionTests.cs b/test/FunctionsV2/Tests/E2E/CurrentAppDetectionTests.cs index c76fd5cf1..307838f8a 100644 --- a/test/FunctionsV2/Tests/E2E/CurrentAppDetectionTests.cs +++ b/test/FunctionsV2/Tests/E2E/CurrentAppDetectionTests.cs @@ -153,6 +153,91 @@ await Task.WhenAll( } } + [Theory] + [InlineData(false)] + [InlineData(true)] + [Trait("Category", PlatformSpecificHelpers.TestCategory)] + public async Task AzureStorage_HostedClientCache_PreservesExternalClientMode( + bool externalFirst) + { + string taskHubName = TestHelpers.GetTaskHubNameFromTestName( + nameof(this.AzureStorage_HostedClientCache_PreservesExternalClientMode), + enableExtendedSessions: false); + string externalInstanceId = $"external-{Guid.NewGuid():N}"; + string internalInstanceId = $"internal-{Guid.NewGuid():N}"; + var nameResolver = new SimpleNameResolver( + new Dictionary + { + { "TestTaskHub", taskHubName }, + }); + var options = new DurableTaskOptions + { + HubName = taskHubName, + }; + + try + { + using ITestHost targetHost = TestHelpers.GetJobHostWithOptions( + this.loggerProvider, + options, + nameResolver: nameResolver, + types: new[] { typeof(CurrentAppDetectionFunctions) }); + using ITestHost clientHost = TestHelpers.GetJobHostWithOptions( + this.loggerProvider, + options, + nameResolver: nameResolver, + types: new[] { typeof(HostedClientCacheFunctions) }); + await targetHost.StartAsync(); + await clientHost.StartAsync(); + + IDurableClient firstClient = await CaptureHostedClientAsync( + clientHost, + externalFirst + ? nameof(HostedClientCacheFunctions.CaptureExternalClient) + : nameof(HostedClientCacheFunctions.CaptureInternalClient), + taskHubName); + IDurableClient secondClient = await CaptureHostedClientAsync( + clientHost, + externalFirst + ? nameof(HostedClientCacheFunctions.CaptureInternalClient) + : nameof(HostedClientCacheFunctions.CaptureExternalClient), + taskHubName); + IDurableClient externalClient = externalFirst ? firstClient : secondClient; + IDurableClient internalClient = externalFirst ? secondClient : firstClient; + + DateTime externalCreationTime = DateTime.UtcNow; + await externalClient.StartNewAsync( + nameof(CurrentAppDetectionFunctions.TargetOnlyOrchestrator), + externalInstanceId, + "external"); + var externalTestClient = new TestDurableClient( + externalClient, + nameof(CurrentAppDetectionFunctions.TargetOnlyOrchestrator), + externalInstanceId, + externalCreationTime); + DurableOrchestrationStatus externalStatus = + await externalTestClient.WaitForCompletionAsync(this.output); + Assert.Equal("target:external", externalStatus.Output); + + ArgumentException internalException = await Assert.ThrowsAsync( + () => internalClient.StartNewAsync( + nameof(CurrentAppDetectionFunctions.TargetOnlyOrchestrator), + internalInstanceId, + "internal")); + Assert.Contains( + "No orchestrator functions are currently registered", + internalException.Message); + await AssertInstanceRemainsAbsentAsync(externalClient, internalInstanceId); + + await clientHost.StopAsync(); + await targetHost.StopAsync(); + } + finally + { + await DeleteTaskHubAsync(taskHubName, TestHelpers.GetStorageConnectionString()); + } + } + private static async Task AssertInstanceRemainsAbsentAsync( IDurableOrchestrationClient client, string instanceId) @@ -170,6 +255,22 @@ private static async Task AssertInstanceRemainsAbsentAsync( Assert.True(observations >= 3); } + private static async Task CaptureHostedClientAsync( + ITestHost host, + string methodName, + string taskHubName) + { + var clients = new IDurableClient[1]; + await host.CallAsync( + typeof(HostedClientCacheFunctions).GetMethod(methodName), + new Dictionary + { + { "hub", taskHubName }, + { "clients", clients }, + }); + return clients[0]; + } + private async Task StartOnSecondaryStorageAsync( ITestHost host, string functionName,