Skip to content
Open
24 changes: 14 additions & 10 deletions eng/ci/templates/public/windows-validation.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/// <inheritdoc/>
public async override Task<IList<OrchestrationState>> GetAllOrchestrationStates(CancellationToken cancellationToken)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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);
Comment thread
YunchuWang marked this conversation as resolved.
Resolved
}

private bool ConnectionNameMatchesCurrentApp(DurableClient client)
{
return this.DurabilityProvider.ConnectionNameMatches(client.DurabilityProvider);
return this.config.DefaultDurabilityProvider.ConnectionNameMatches(client.DurabilityProvider);
}

/// <inheritdoc />
Expand Down
11 changes: 11 additions & 0 deletions src/WebJobs.Extensions.DurableTask/DurabilityProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,17 @@ internal virtual bool ConnectionNameMatches(DurabilityProvider durabilityProvide
return this.ConnectionName.Equals(durabilityProvider.ConnectionName);
}

/// <summary>
/// Returns true if two task hub names identify the same task hub for this durability provider.
/// </summary>
/// <param name="taskHubName">The first task hub name.</param>
/// <param name="otherTaskHubName">The second task hub name.</param>
/// <returns>A boolean indicating whether the task hub names match.</returns>
internal virtual bool TaskHubNameMatches(string taskHubName, string otherTaskHubName)
{
return string.Equals(taskHubName, otherTaskHubName, StringComparison.Ordinal);
}

/// <summary>
/// Tries to obtain a scale monitor for autoscaling.
/// </summary>
Expand Down
19 changes: 19 additions & 0 deletions test/FunctionsV2/FunctionApp/ClientFunctions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
14 changes: 14 additions & 0 deletions test/FunctionsV2/FunctionApp/CurrentAppDetectionFunctions.cs
Original file line number Diff line number Diff line change
@@ -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<string>()}";
}
}
}
235 changes: 235 additions & 0 deletions test/FunctionsV2/Tests/E2E/CurrentAppDetectionTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,235 @@
// 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<string, string>
{
{ "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<FunctionInvocationException>(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<string, string>
{
{ "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<TestDurableClient> StartOnSecondaryStorageAsync(
ITestHost host,
string functionName,
object input,
string instanceId)
{
var clientRef = new TestDurableClient[1];
var arguments = new Dictionary<string, object>
{
{ "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<DurableOrchestrationStatus> 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.");
}
}
}
Loading
Loading