|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +// End-to-end regression test for https://github.com/microsoft/durabletask-dotnet/issues/668 |
| 5 | +// |
| 6 | +// This test validates that calling an unregistered entity type from an orchestration |
| 7 | +// correctly fails with an EntityOperationFailedException instead of hanging indefinitely. |
| 8 | +// |
| 9 | +// Prerequisites: |
| 10 | +// Set the DTS_CONNECTION_STRING environment variable to a valid DTS connection string. |
| 11 | +// Example: Endpoint=https://myscheduler.eastasia.durabletask.io;Authentication=DefaultAzure;TaskHub=myHub |
| 12 | +// |
| 13 | +// Usage: |
| 14 | +// dotnet run --project test/ManualE2ETests/WorkItemFilterRegression |
| 15 | + |
| 16 | +using Microsoft.DurableTask; |
| 17 | +using Microsoft.DurableTask.Client; |
| 18 | +using Microsoft.DurableTask.Client.AzureManaged; |
| 19 | +using Microsoft.DurableTask.Entities; |
| 20 | +using Microsoft.DurableTask.Worker; |
| 21 | +using Microsoft.DurableTask.Worker.AzureManaged; |
| 22 | +using Microsoft.Extensions.DependencyInjection; |
| 23 | +using Microsoft.Extensions.Hosting; |
| 24 | +using Microsoft.Extensions.Logging; |
| 25 | + |
| 26 | +string connectionString = Environment.GetEnvironmentVariable("DTS_CONNECTION_STRING") |
| 27 | + ?? throw new InvalidOperationException( |
| 28 | + "DTS_CONNECTION_STRING environment variable is not set. " |
| 29 | + + "Example: Endpoint=https://myscheduler.eastasia.durabletask.io;Authentication=DefaultAzure;TaskHub=myHub"); |
| 30 | + |
| 31 | +Console.WriteLine("=== Work Item Filter Regression Test (Issue #668) ==="); |
| 32 | +Console.WriteLine($"Connection: {MaskConnectionString(connectionString)}"); |
| 33 | +Console.WriteLine(); |
| 34 | + |
| 35 | +HostApplicationBuilder builder = Host.CreateApplicationBuilder(args); |
| 36 | + |
| 37 | +builder.Logging.SetMinimumLevel(LogLevel.Warning); |
| 38 | + |
| 39 | +// Register worker with only one entity type ("Counter"), but the orchestration |
| 40 | +// will attempt to call an entity type ("UnregisteredEntity") that is NOT registered. |
| 41 | +builder.Services.AddDurableTaskWorker(workerBuilder => |
| 42 | +{ |
| 43 | + workerBuilder.AddTasks(registry => |
| 44 | + { |
| 45 | + registry.AddOrchestrator<CallUnregisteredEntityOrchestrator>(); |
| 46 | + registry.AddOrchestrator<CallRegisteredEntityOrchestrator>(); |
| 47 | + registry.AddActivity<NoOpActivity>(); |
| 48 | + registry.AddEntity<Counter>(); |
| 49 | + }); |
| 50 | + workerBuilder.UseDurableTaskScheduler(connectionString); |
| 51 | +}); |
| 52 | + |
| 53 | +builder.Services.AddDurableTaskClient(clientBuilder => |
| 54 | +{ |
| 55 | + clientBuilder.UseDurableTaskScheduler(connectionString); |
| 56 | +}); |
| 57 | + |
| 58 | +using IHost host = builder.Build(); |
| 59 | +await host.StartAsync(); |
| 60 | + |
| 61 | +await using DurableTaskClient client = host.Services.GetRequiredService<DurableTaskClient>(); |
| 62 | + |
| 63 | +int passed = 0; |
| 64 | +int failed = 0; |
| 65 | + |
| 66 | +// Test 1: Calling an UNREGISTERED entity should fail with an error, not hang. |
| 67 | +await RunTestAsync("CallEntityAsync targeting unregistered entity fails with error", async () => |
| 68 | +{ |
| 69 | + string instanceId = await client.ScheduleNewOrchestrationInstanceAsync( |
| 70 | + nameof(CallUnregisteredEntityOrchestrator)); |
| 71 | + |
| 72 | + OrchestrationMetadata? result = await client.WaitForInstanceCompletionAsync( |
| 73 | + instanceId, getInputsAndOutputs: true, cancellation: new CancellationTokenSource(TimeSpan.FromSeconds(30)).Token); |
| 74 | + |
| 75 | + if (result == null) |
| 76 | + { |
| 77 | + throw new Exception("Orchestration result was null."); |
| 78 | + } |
| 79 | + |
| 80 | + if (result.RuntimeStatus == OrchestrationRuntimeStatus.Completed) |
| 81 | + { |
| 82 | + string? output = result.ReadOutputAs<string>(); |
| 83 | + Console.WriteLine($" Orchestration completed with output: {output}"); |
| 84 | + |
| 85 | + // The orchestration is designed to catch the exception and return a success message. |
| 86 | + if (output?.Contains("EntityOperationFailedException") == true |
| 87 | + || output?.Contains("EntityTaskNotFound") == true) |
| 88 | + { |
| 89 | + Console.WriteLine(" PASS: Orchestration correctly caught the entity error."); |
| 90 | + } |
| 91 | + else |
| 92 | + { |
| 93 | + throw new Exception($"Unexpected output: {output}"); |
| 94 | + } |
| 95 | + } |
| 96 | + else if (result.RuntimeStatus == OrchestrationRuntimeStatus.Failed) |
| 97 | + { |
| 98 | + // Also acceptable — the error propagated as a failure. |
| 99 | + Console.WriteLine($" Orchestration failed (expected). FailureDetails: {result.FailureDetails?.ErrorMessage}"); |
| 100 | + } |
| 101 | + else |
| 102 | + { |
| 103 | + throw new Exception($"Unexpected status: {result.RuntimeStatus}"); |
| 104 | + } |
| 105 | +}); |
| 106 | + |
| 107 | +// Test 2: Calling a REGISTERED entity should succeed normally. |
| 108 | +await RunTestAsync("CallEntityAsync targeting registered entity succeeds", async () => |
| 109 | +{ |
| 110 | + string instanceId = await client.ScheduleNewOrchestrationInstanceAsync( |
| 111 | + nameof(CallRegisteredEntityOrchestrator)); |
| 112 | + |
| 113 | + OrchestrationMetadata? result = await client.WaitForInstanceCompletionAsync( |
| 114 | + instanceId, getInputsAndOutputs: true, cancellation: new CancellationTokenSource(TimeSpan.FromSeconds(30)).Token); |
| 115 | + |
| 116 | + if (result == null) |
| 117 | + { |
| 118 | + throw new Exception("Orchestration result was null."); |
| 119 | + } |
| 120 | + |
| 121 | + if (result.RuntimeStatus != OrchestrationRuntimeStatus.Completed) |
| 122 | + { |
| 123 | + throw new Exception($"Expected Completed but got {result.RuntimeStatus}. FailureDetails: {result.FailureDetails?.ErrorMessage}"); |
| 124 | + } |
| 125 | + |
| 126 | + string? output = result.ReadOutputAs<string>(); |
| 127 | + Console.WriteLine($" Orchestration completed with output: {output}"); |
| 128 | +}); |
| 129 | + |
| 130 | +// Test 3: Calling an unregistered activity should fail with an error, not hang. |
| 131 | +await RunTestAsync("CallActivityAsync targeting unregistered activity fails with error", async () => |
| 132 | +{ |
| 133 | + string instanceId = await client.ScheduleNewOrchestrationInstanceAsync( |
| 134 | + "CallUnregisteredActivityOrchestrator"); |
| 135 | + |
| 136 | + OrchestrationMetadata? result = await client.WaitForInstanceCompletionAsync( |
| 137 | + instanceId, getInputsAndOutputs: true, cancellation: new CancellationTokenSource(TimeSpan.FromSeconds(30)).Token); |
| 138 | + |
| 139 | + if (result == null) |
| 140 | + { |
| 141 | + throw new Exception("Orchestration result was null."); |
| 142 | + } |
| 143 | + |
| 144 | + // This orchestration is NOT registered, so the work item should be dispatched to the worker, |
| 145 | + // which will fail with OrchestratorTaskNotFound. This is expected behavior. |
| 146 | + if (result.RuntimeStatus == OrchestrationRuntimeStatus.Failed) |
| 147 | + { |
| 148 | + Console.WriteLine($" Orchestration failed as expected: {result.FailureDetails?.ErrorType}"); |
| 149 | + } |
| 150 | + else |
| 151 | + { |
| 152 | + throw new Exception($"Expected Failed but got {result.RuntimeStatus}"); |
| 153 | + } |
| 154 | +}); |
| 155 | + |
| 156 | +Console.WriteLine(); |
| 157 | +Console.WriteLine($"=== Results: {passed} passed, {failed} failed ==="); |
| 158 | +Console.WriteLine(); |
| 159 | + |
| 160 | +await host.StopAsync(); |
| 161 | +Environment.Exit(failed > 0 ? 1 : 0); |
| 162 | + |
| 163 | +async Task RunTestAsync(string testName, Func<Task> testAction) |
| 164 | +{ |
| 165 | + Console.WriteLine($"[TEST] {testName}"); |
| 166 | + try |
| 167 | + { |
| 168 | + await testAction(); |
| 169 | + Console.WriteLine($" RESULT: PASS"); |
| 170 | + passed++; |
| 171 | + } |
| 172 | + catch (OperationCanceledException) |
| 173 | + { |
| 174 | + Console.WriteLine($" RESULT: FAIL - Timed out (orchestration hung, regression detected!)"); |
| 175 | + failed++; |
| 176 | + } |
| 177 | + catch (Exception ex) |
| 178 | + { |
| 179 | + Console.WriteLine($" RESULT: FAIL - {ex.Message}"); |
| 180 | + failed++; |
| 181 | + } |
| 182 | + |
| 183 | + Console.WriteLine(); |
| 184 | +} |
| 185 | + |
| 186 | +static string MaskConnectionString(string cs) |
| 187 | +{ |
| 188 | + // Mask the endpoint for security, show just enough for identification |
| 189 | + int endpointIdx = cs.IndexOf("Endpoint=", StringComparison.OrdinalIgnoreCase); |
| 190 | + if (endpointIdx >= 0) |
| 191 | + { |
| 192 | + int semicolonIdx = cs.IndexOf(';', endpointIdx + 9); |
| 193 | + string endpoint = semicolonIdx >= 0 ? cs.Substring(endpointIdx + 9, semicolonIdx - endpointIdx - 9) : cs.Substring(endpointIdx + 9); |
| 194 | + return $"Endpoint={endpoint};..."; |
| 195 | + } |
| 196 | + |
| 197 | + return "***"; |
| 198 | +} |
| 199 | + |
| 200 | +// ===== Orchestrators and entities ===== |
| 201 | + |
| 202 | +/// <summary> |
| 203 | +/// Orchestrator that calls an entity type that is NOT registered with the worker. |
| 204 | +/// Before the fix (issue #668), this would hang indefinitely. |
| 205 | +/// After the fix, this should fail with EntityOperationFailedException. |
| 206 | +/// </summary> |
| 207 | +sealed class CallUnregisteredEntityOrchestrator : TaskOrchestrator<object?, string> |
| 208 | +{ |
| 209 | + public override async Task<string> RunAsync(TaskOrchestrationContext context, object? input) |
| 210 | + { |
| 211 | + try |
| 212 | + { |
| 213 | + EntityInstanceId unregistered = new("UnregisteredEntity", "key1"); |
| 214 | + await context.Entities.CallEntityAsync<string>(unregistered, "get"); |
| 215 | + return "ERROR: CallEntityAsync did not throw for unregistered entity"; |
| 216 | + } |
| 217 | + catch (EntityOperationFailedException ex) |
| 218 | + { |
| 219 | + return $"OK: Got EntityOperationFailedException - {ex.FailureDetails.ErrorType}: {ex.FailureDetails.ErrorMessage}"; |
| 220 | + } |
| 221 | + catch (Exception ex) |
| 222 | + { |
| 223 | + return $"OK: Got exception - {ex.GetType().Name}: {ex.Message}"; |
| 224 | + } |
| 225 | + } |
| 226 | +} |
| 227 | + |
| 228 | +/// <summary> |
| 229 | +/// Orchestrator that calls a registered entity type. This should succeed. |
| 230 | +/// </summary> |
| 231 | +sealed class CallRegisteredEntityOrchestrator : TaskOrchestrator<object?, string> |
| 232 | +{ |
| 233 | + public override async Task<string> RunAsync(TaskOrchestrationContext context, object? input) |
| 234 | + { |
| 235 | + EntityInstanceId counter = new(nameof(Counter), Guid.NewGuid().ToString("N")); |
| 236 | + await context.Entities.CallEntityAsync(counter, "add", 5); |
| 237 | + int result = await context.Entities.CallEntityAsync<int>(counter, "get"); |
| 238 | + return $"Counter value: {result}"; |
| 239 | + } |
| 240 | +} |
| 241 | + |
| 242 | +/// <summary> |
| 243 | +/// A simple counter entity used for testing registered entity calls. |
| 244 | +/// </summary> |
| 245 | +sealed class Counter : TaskEntity<int> |
| 246 | +{ |
| 247 | + public int Add(int value) |
| 248 | + { |
| 249 | + this.State += value; |
| 250 | + return this.State; |
| 251 | + } |
| 252 | + |
| 253 | + public int Get() => this.State; |
| 254 | +} |
| 255 | + |
| 256 | +/// <summary> |
| 257 | +/// A no-op activity for testing. |
| 258 | +/// </summary> |
| 259 | +sealed class NoOpActivity : TaskActivity<string?, string> |
| 260 | +{ |
| 261 | + public override Task<string> RunAsync(TaskActivityContext context, string? input) |
| 262 | + { |
| 263 | + return Task.FromResult("done"); |
| 264 | + } |
| 265 | +} |
0 commit comments