diff --git a/eng/ci/templates/public/windows-validation.yml b/eng/ci/templates/public/windows-validation.yml index a2646385e..f84f1dfd9 100644 --- a/eng/ci/templates/public/windows-validation.yml +++ b/eng/ci/templates/public/windows-validation.yml @@ -160,6 +160,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 @@ -198,16 +199,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 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 87da49393..046d9f9d3 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; @@ -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/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/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/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/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/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 new file mode 100644 index 000000000..307838f8a --- /dev/null +++ b/test/FunctionsV2/Tests/E2E/CurrentAppDetectionTests.cs @@ -0,0 +1,336 @@ +// 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)); + } + } + + [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) + { + 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 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, + 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 async Task DeleteTaskHubAsync(string taskHubName, string connectionString) + { + var settings = new AzureStorageOrchestrationServiceSettings + { + TaskHubName = taskHubName, + StorageAccountClientProvider = new StorageAccountClientProvider(connectionString), + }; + using var service = new AzureStorageOrchestrationService(settings); + await service.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."); + } + } +} diff --git a/test/FunctionsV2/Tests/Unit/DurableClientBaseTests.cs b/test/FunctionsV2/Tests/Unit/DurableClientBaseTests.cs index 6a1a239fe..7bf0de2b9 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")] @@ -82,11 +101,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 +176,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 +194,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 +235,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 +308,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 +329,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,11 +456,28 @@ 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(); + await ((IDurableEntityClient)durableClient).SignalEntityAsync( + new EntityId("MissingEntity", "entity-key"), + "operation", + new { Value = 1 }); + + serviceClient.Verify( + x => x.SendTaskOrchestrationMessageAsync(It.IsAny()), + Times.Once()); + } + + [Fact] + [Trait("Category", PlatformSpecificHelpers.TestCategory)] + public async Task SignalEntityAsync_AzureStorageCaseVariantTaskHub_MissingEntity_ThrowsException() + { + (DurableClient durableClient, Mock serviceClient, _) = + CreateClient(taskHub: "durabletaskhub", connectionName: TestConstants.ConnectionName); + ArgumentException exception = await Assert.ThrowsAsync( () => ((IDurableEntityClient)durableClient).SignalEntityAsync( new EntityId("MissingEntity", "entity-key"), @@ -394,23 +490,177 @@ public async Task SignalEntityAsync_DifferentConnection_MissingEntity_ThrowsExce 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()); + } + [Fact] [Trait("Category", PlatformSpecificHelpers.TestCategory)] - public async Task SignalEntityAsync_CaseVariantTaskHub_MissingEntity_SendsMessage() + public async Task SignalEntityAsync_ExactCaseProviderCaseVariantTaskHub_UsesRequestedCachedClient() { - (DurableClient durableClient, Mock serviceClient, _) = - CreateClient(taskHub: "durabletaskhub", connectionName: TestConstants.ConnectionName); + 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 ((IDurableEntityClient)durableClient).SignalEntityAsync( + await currentClient.SignalEntityAsync( new EntityId("MissingEntity", "entity-key"), "operation", - new { Value = 1 }); + operationInput: null, + taskHubName: "currenthub", + connectionName: TestConstants.ConnectionName); - serviceClient.Verify( - x => x.SendTaskOrchestrationMessageAsync(It.IsAny()), + 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)] @@ -698,7 +948,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 @@ -712,7 +963,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, @@ -829,21 +1087,40 @@ private static List GetInstanceState( }; } - private static DurableTaskExtension GetDurableTaskConfig() + private static DurableTaskExtension GetDurableTaskConfig( + Microsoft.Azure.WebJobs.Extensions.DurableTask.DurabilityProvider defaultProvider = null, + string taskHubName = "DurableTaskHub", + Func providerSelector = null) { 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(); 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((DurableClientAttribute attribute) => + providerSelector?.Invoke(attribute) ?? 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 351559fb6..8b0331874 100644 --- a/test/FunctionsV2/Tests/Unit/HttpApiHandlerTests.cs +++ b/test/FunctionsV2/Tests/Unit/HttpApiHandlerTests.cs @@ -2139,11 +2139,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 @@ -2155,7 +2157,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"), @@ -2177,6 +2179,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); + } + [Fact] [Trait("Category", PlatformSpecificHelpers.TestCategory)] public async Task MakePrimary_Returns_HTTP_400_When_AppLease_Is_Disabled() 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(