|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +using Microsoft.DurableTask; |
| 5 | +using Microsoft.DurableTask.Client; |
| 6 | +using Microsoft.DurableTask.Testing; |
| 7 | +using Microsoft.DurableTask.Worker; |
| 8 | +using Xunit; |
| 9 | + |
| 10 | +namespace InProcessTestHost.Tests; |
| 11 | + |
| 12 | +/// <summary> |
| 13 | +/// Tests to verify that multiple orchestrations with identical timer FireAt timestamps |
| 14 | +/// all complete correctly without any being dropped. |
| 15 | +/// </summary> |
| 16 | +public class ConcurrentTimerTests |
| 17 | +{ |
| 18 | + [Fact] |
| 19 | + // Test that multiple orchestrations with the same timer that fire at the same time |
| 20 | + // can all complete correctly. |
| 21 | + public async Task MultipleOrchestrations_WithSameTimerFireAt_AllComplete() |
| 22 | + { |
| 23 | + const int orchestrationCount = 10; |
| 24 | + const string orchestratorName = "TimerOrchestrator"; |
| 25 | + |
| 26 | + await using DurableTaskTestHost host = await DurableTaskTestHost.StartAsync(tasks => |
| 27 | + { |
| 28 | + tasks.AddOrchestratorFunc<DateTime, string>(orchestratorName, async (ctx, fireAt) => |
| 29 | + { |
| 30 | + await ctx.CreateTimer(fireAt, CancellationToken.None); |
| 31 | + return $"done:{ctx.InstanceId}"; |
| 32 | + }); |
| 33 | + }); |
| 34 | + |
| 35 | + DateTime sharedFireAt = DateTime.UtcNow.AddSeconds(5); |
| 36 | + |
| 37 | + string[] instanceIds = new string[orchestrationCount]; |
| 38 | + for (int i = 0; i < orchestrationCount; i++) |
| 39 | + { |
| 40 | + instanceIds[i] = await host.Client.ScheduleNewOrchestrationInstanceAsync( |
| 41 | + orchestratorName, sharedFireAt); |
| 42 | + } |
| 43 | + |
| 44 | + using CancellationTokenSource cts = new(TimeSpan.FromSeconds(30)); |
| 45 | + |
| 46 | + Task<OrchestrationMetadata>[] waitTasks = instanceIds |
| 47 | + .Select(id => host.Client.WaitForInstanceCompletionAsync( |
| 48 | + id, getInputsAndOutputs: true, cts.Token)) |
| 49 | + .ToArray(); |
| 50 | + |
| 51 | + OrchestrationMetadata[] results = await Task.WhenAll(waitTasks); |
| 52 | + |
| 53 | + for (int i = 0; i < orchestrationCount; i++) |
| 54 | + { |
| 55 | + Assert.NotNull(results[i]); |
| 56 | + Assert.Equal(OrchestrationRuntimeStatus.Completed, results[i].RuntimeStatus); |
| 57 | + string output = results[i].ReadOutputAs<string>()!; |
| 58 | + Assert.Equal($"done:{instanceIds[i]}", output); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + [Fact] |
| 63 | + // Test that fan-out sub-orchestrations with timers that all fire at the same time |
| 64 | + // can all complete correctly. |
| 65 | + public async Task SubOrchestrations_WithIdenticalTimers_AllComplete() |
| 66 | + { |
| 67 | + const int subOrchestrationCount = 10; |
| 68 | + const string parentName = "ParentOrchestrator"; |
| 69 | + const string childName = "ChildTimerOrchestrator"; |
| 70 | + |
| 71 | + await using DurableTaskTestHost host = await DurableTaskTestHost.StartAsync(tasks => |
| 72 | + { |
| 73 | + tasks.AddOrchestratorFunc<int>(parentName, async ctx => |
| 74 | + { |
| 75 | + DateTime sharedFireAt = ctx.CurrentUtcDateTime.AddSeconds(2); |
| 76 | + |
| 77 | + // A parent orchestration will schedule 10 sub-orchestrations which has a timer |
| 78 | + // fires at the same time. |
| 79 | + Task<string>[] childTasks = Enumerable.Range(0, subOrchestrationCount) |
| 80 | + .Select(i => ctx.CallSubOrchestratorAsync<string>(childName, sharedFireAt)) |
| 81 | + .ToArray(); |
| 82 | + |
| 83 | + string[] results = await Task.WhenAll(childTasks); |
| 84 | + return results.Length; |
| 85 | + }); |
| 86 | + |
| 87 | + tasks.AddOrchestratorFunc<DateTime, string>(childName, async (ctx, fireAt) => |
| 88 | + { |
| 89 | + await ctx.CreateTimer(fireAt, CancellationToken.None); |
| 90 | + return $"child-done:{ctx.InstanceId}"; |
| 91 | + }); |
| 92 | + }); |
| 93 | + |
| 94 | + string instanceId = await host.Client.ScheduleNewOrchestrationInstanceAsync(parentName); |
| 95 | + |
| 96 | + using CancellationTokenSource cts = new(TimeSpan.FromSeconds(60)); |
| 97 | + OrchestrationMetadata metadata = await host.Client.WaitForInstanceCompletionAsync( |
| 98 | + instanceId, getInputsAndOutputs: true, cts.Token); |
| 99 | + |
| 100 | + Assert.NotNull(metadata); |
| 101 | + Assert.Equal(OrchestrationRuntimeStatus.Completed, metadata.RuntimeStatus); |
| 102 | + Assert.Equal(subOrchestrationCount, metadata.ReadOutputAs<int>()); |
| 103 | + } |
| 104 | +} |
0 commit comments