|
| 1 | +// Copyright (c) .NET Foundation. All rights reserved. |
| 2 | +// Licensed under the MIT License. See LICENSE in the project root for license information. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.Reflection; |
| 7 | +using System.Threading; |
| 8 | +using System.Threading.Tasks; |
| 9 | +using DurableTask.Core; |
| 10 | +using DurableTask.Core.Entities; |
| 11 | +using DurableTask.Core.Entities.OperationFormat; |
| 12 | +using DurableTask.Core.Exceptions; |
| 13 | +using DurableTask.Core.History; |
| 14 | +using DurableTask.Core.Middleware; |
| 15 | +using Microsoft.Azure.WebJobs.Host.Executors; |
| 16 | +using Microsoft.Extensions.Logging; |
| 17 | +using Microsoft.Extensions.Logging.Abstractions; |
| 18 | +using Microsoft.Extensions.Options; |
| 19 | +using Moq; |
| 20 | +using Xunit; |
| 21 | + |
| 22 | +namespace Microsoft.Azure.WebJobs.Extensions.DurableTask.Tests |
| 23 | +{ |
| 24 | + public class OutOfProcMiddlewareTests |
| 25 | + { |
| 26 | + [Fact] |
| 27 | + [Trait("Category", PlatformSpecificHelpers.TestCategory)] |
| 28 | + public async Task CallOrchestratorAsync_DifferentInvalidOperationException_DoesNotThrowSessionAbortedException() |
| 29 | + { |
| 30 | + // Arrange: a different InvalidOperationException message should NOT trigger the retry path |
| 31 | + var innerException = new InvalidOperationException("The internal function invoker returned a task that does not support return values!"); |
| 32 | + var outerException = new Exception("Function invocation failed.", innerException); |
| 33 | + |
| 34 | + var (middleware, dispatchContext) = this.SetupOrchestratorTest(outerException); |
| 35 | + |
| 36 | + // Act: should NOT throw SessionAbortedException — instead the orchestration should be marked as failed |
| 37 | + await middleware.CallOrchestratorAsync(dispatchContext, () => Task.CompletedTask); |
| 38 | + |
| 39 | + // Assert: the middleware should have set a failure result on the dispatch context |
| 40 | + var result = dispatchContext.GetProperty<OrchestratorExecutionResult>(); |
| 41 | + Assert.NotNull(result); |
| 42 | + } |
| 43 | + |
| 44 | + [Theory] |
| 45 | + [Trait("Category", PlatformSpecificHelpers.TestCategory)] |
| 46 | + [MemberData(nameof(PlatformLevelExceptions))] |
| 47 | + public async Task CallOrchestratorAsync_PlatformLevelException_ThrowsSessionAbortedException(Exception exception) |
| 48 | + { |
| 49 | + var (middleware, dispatchContext) = this.SetupOrchestratorTest(exception); |
| 50 | + |
| 51 | + await Assert.ThrowsAsync<SessionAbortedException>( |
| 52 | + () => middleware.CallOrchestratorAsync(dispatchContext, () => Task.CompletedTask)); |
| 53 | + } |
| 54 | + |
| 55 | + public static IEnumerable<object[]> PlatformLevelExceptions() |
| 56 | + { |
| 57 | + // FunctionTimeoutException (top-level) |
| 58 | + yield return new object[] { new Host.FunctionTimeoutException("Function timed out.") }; |
| 59 | + |
| 60 | + // SessionAbortedException as InnerException (e.g. out-of-memory handling) |
| 61 | + yield return new object[] { new Exception("Function invocation failed.", new SessionAbortedException("Out of memory")) }; |
| 62 | + |
| 63 | + // WorkerProcessExitException as InnerException (matched by type name) |
| 64 | + yield return new object[] { new Exception("Function invocation failed.", new WorkerProcessExitExceptionStub("Worker process exited.")) }; |
| 65 | + |
| 66 | + // InvalidOperationException with "No process is associated" as InnerException |
| 67 | + yield return new object[] { new Exception("Function invocation failed.", new InvalidOperationException("No process is associated with this object.")) }; |
| 68 | + } |
| 69 | + |
| 70 | + private (OutOfProcMiddleware middleware, DispatchMiddlewareContext context) SetupOrchestratorTest(Exception executorException) |
| 71 | + { |
| 72 | + var (middleware, dispatchContext) = this.CreateMiddleware(executorException, "TestOrchestrator", FunctionType.Orchestrator); |
| 73 | + |
| 74 | + var orchestrationState = new OrchestrationRuntimeState( |
| 75 | + new List<HistoryEvent> |
| 76 | + { |
| 77 | + new ExecutionStartedEvent(-1, null) { Name = "TestOrchestrator" }, |
| 78 | + }); |
| 79 | + |
| 80 | + dispatchContext.SetProperty(orchestrationState); |
| 81 | + dispatchContext.SetProperty(new OrchestrationInstance { InstanceId = "test-instance-id" }); |
| 82 | + |
| 83 | + return (middleware, dispatchContext); |
| 84 | + } |
| 85 | + |
| 86 | + private (OutOfProcMiddleware middleware, DispatchMiddlewareContext context) CreateMiddleware( |
| 87 | + Exception executorException, string functionName, FunctionType functionType) |
| 88 | + { |
| 89 | + var extension = CreateDurableTaskExtension(); |
| 90 | + |
| 91 | + var mockExecutor = new Mock<ITriggeredFunctionExecutor>(); |
| 92 | + mockExecutor |
| 93 | + .Setup(e => e.TryExecuteAsync(It.IsAny<TriggeredFunctionData>(), It.IsAny<CancellationToken>())) |
| 94 | + .ReturnsAsync(new FunctionResult(false, executorException)); |
| 95 | + |
| 96 | + var name = new FunctionName(functionName); |
| 97 | + |
| 98 | + switch (functionType) |
| 99 | + { |
| 100 | + case FunctionType.Activity: |
| 101 | + extension.RegisterActivity(name, mockExecutor.Object); |
| 102 | + break; |
| 103 | + case FunctionType.Entity: |
| 104 | + extension.RegisterEntity(name, new RegisteredFunctionInfo(mockExecutor.Object, isOutOfProc: true)); |
| 105 | + break; |
| 106 | + default: |
| 107 | + extension.RegisterOrchestrator(name, new RegisteredFunctionInfo(mockExecutor.Object, isOutOfProc: true)); |
| 108 | + break; |
| 109 | + } |
| 110 | + |
| 111 | + var dispatchContext = new DispatchMiddlewareContext(); |
| 112 | + |
| 113 | + // Orchestrators and entities require WorkItemMetadata; activities do not. |
| 114 | + if (functionType != FunctionType.Activity) |
| 115 | + { |
| 116 | + dispatchContext.SetProperty(CreateWorkItemMetadata(isExtendedSession: false, includeState: false)); |
| 117 | + } |
| 118 | + |
| 119 | + return (new OutOfProcMiddleware(extension), dispatchContext); |
| 120 | + } |
| 121 | + |
| 122 | + private static DurableTaskExtension CreateDurableTaskExtension() |
| 123 | + { |
| 124 | + var options = new DurableTaskOptions |
| 125 | + { |
| 126 | + HubName = "TestHub", |
| 127 | + WebhookUriProviderOverride = () => new Uri("https://localhost"), |
| 128 | + }; |
| 129 | + |
| 130 | + return new DurableTaskExtension( |
| 131 | + new OptionsWrapper<DurableTaskOptions>(options), |
| 132 | + NullLoggerFactory.Instance, |
| 133 | + TestHelpers.GetTestNameResolver(), |
| 134 | + new[] |
| 135 | + { |
| 136 | + new AzureStorageDurabilityProviderFactory( |
| 137 | + new OptionsWrapper<DurableTaskOptions>(options), |
| 138 | + new TestStorageServiceClientProviderFactory(), |
| 139 | + TestHelpers.GetTestNameResolver(), |
| 140 | + NullLoggerFactory.Instance, |
| 141 | + TestHelpers.GetMockPlatformInformationService()), |
| 142 | + }, |
| 143 | + new TestHostShutdownNotificationService(), |
| 144 | + new DurableHttpMessageHandlerFactory(), |
| 145 | + platformInformationService: TestHelpers.GetMockPlatformInformationService()); |
| 146 | + } |
| 147 | + |
| 148 | + private static WorkItemMetadata CreateWorkItemMetadata(bool isExtendedSession, bool includeState) |
| 149 | + { |
| 150 | + // WorkItemMetadata has an internal constructor, so we use reflection to create it. |
| 151 | + var ctor = typeof(WorkItemMetadata).GetConstructor( |
| 152 | + BindingFlags.Instance | BindingFlags.NonPublic, |
| 153 | + binder: null, |
| 154 | + new[] { typeof(bool), typeof(bool) }, |
| 155 | + modifiers: null); |
| 156 | + Assert.NotNull(ctor); |
| 157 | + return (WorkItemMetadata)ctor.Invoke(new object[] { isExtendedSession, includeState }); |
| 158 | + } |
| 159 | + |
| 160 | + /// <summary> |
| 161 | + /// Stub exception whose type name contains "WorkerProcessExitException" to match the |
| 162 | + /// string-based check in <see cref="OutOfProcMiddleware"/>. The real |
| 163 | + /// <c>WorkerProcessExitException</c> lives in <c>Microsoft.Azure.WebJobs.Script</c> |
| 164 | + /// (the Functions host runtime), which is too heavy to reference as a test dependency. |
| 165 | + /// </summary> |
| 166 | + private class WorkerProcessExitExceptionStub : Exception |
| 167 | + { |
| 168 | + public WorkerProcessExitExceptionStub(string message) |
| 169 | + : base(message) |
| 170 | + { |
| 171 | + } |
| 172 | + } |
| 173 | + } |
| 174 | +} |
0 commit comments