|
| 1 | +// Copyright (c) .NET Foundation. All rights reserved. |
| 2 | +// Licensed under the MIT License. See License.txt in the project root for license information. |
| 3 | + |
| 4 | +using System.Net; |
| 5 | +using Xunit; |
| 6 | +using Xunit.Abstractions; |
| 7 | + |
| 8 | +namespace Microsoft.Azure.Durable.Tests.DotnetIsolatedE2E; |
| 9 | + |
| 10 | +[Collection(Constants.FunctionAppCollectionName)] |
| 11 | +public class ScheduledOrchestrationTests |
| 12 | +{ |
| 13 | + private readonly FunctionAppFixture fixture; |
| 14 | + private readonly ITestOutputHelper output; |
| 15 | + |
| 16 | + public ScheduledOrchestrationTests(FunctionAppFixture fixture, ITestOutputHelper testOutputHelper) |
| 17 | + { |
| 18 | + this.fixture = fixture; |
| 19 | + this.fixture.TestLogs.UseTestLogger(testOutputHelper); |
| 20 | + this.output = testOutputHelper; |
| 21 | + } |
| 22 | + |
| 23 | + // Due to some kind of asynchronous race condition in XUnit, when running these tests in pipelines, |
| 24 | + // the output may be disposed before the message is written. Just ignore these types of errors for now. |
| 25 | + private void WriteOutput(string message) |
| 26 | + { |
| 27 | + try |
| 28 | + { |
| 29 | + this.output.WriteLine(message); |
| 30 | + } |
| 31 | + catch |
| 32 | + { |
| 33 | + // Ignore |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + [Theory] |
| 38 | + [InlineData("HelloCities_HttpStart_Scheduled", 10, HttpStatusCode.Accepted)] |
| 39 | + [InlineData("HelloCities_HttpStart_Scheduled", -5, HttpStatusCode.Accepted)] |
| 40 | + [Trait("PowerShell", "Skip")] // Scheduled orchestrations not implemented in PowerShell |
| 41 | + public async Task ScheduledStartTests(string functionName, int startDelaySeconds, HttpStatusCode expectedStatusCode) |
| 42 | + { |
| 43 | + var testStartTime = DateTime.UtcNow; |
| 44 | + var scheduledStartTime = testStartTime + TimeSpan.FromSeconds(startDelaySeconds); |
| 45 | + string urlQueryString = $"?ScheduledStartTime={scheduledStartTime.ToString("o")}"; |
| 46 | + |
| 47 | + using HttpResponseMessage response = await HttpHelpers.InvokeHttpTrigger(functionName, urlQueryString); |
| 48 | + |
| 49 | + string statusQueryGetUri = await DurableHelpers.ParseStatusQueryGetUriAsync(response); |
| 50 | + |
| 51 | + Assert.Equal(expectedStatusCode, response.StatusCode); |
| 52 | + |
| 53 | + if (scheduledStartTime > DateTime.UtcNow + TimeSpan.FromSeconds(1)) |
| 54 | + { |
| 55 | + if (this.fixture.functionLanguageLocalizer.GetLanguageType() == LanguageType.DotnetIsolated || |
| 56 | + this.fixture.functionLanguageLocalizer.GetLanguageType() == LanguageType.Java) |
| 57 | + { |
| 58 | + // This line will throw if the orchestration goes to a terminal state before reaching "Pending", |
| 59 | + // ensuring that any scheduled orchestrations that run immediately fail the test. |
| 60 | + await DurableHelpers.WaitForOrchestrationStateAsync(statusQueryGetUri, "Pending", 30); |
| 61 | + } |
| 62 | + else |
| 63 | + { |
| 64 | + // Scheduled orchestrations are not properly implemented in the other languages - however, |
| 65 | + // this test has been implemented using timers in the orchestration instead. |
| 66 | + await DurableHelpers.WaitForOrchestrationStateAsync(statusQueryGetUri, "Running", 30); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + await DurableHelpers.WaitForOrchestrationStateAsync(statusQueryGetUri, "Completed", Math.Max(startDelaySeconds, 0) + 30); |
| 71 | + |
| 72 | + // This +2s should not be necessary - however, experimentally the orchestration may run up to ~1 second before the scheduled time. |
| 73 | + // It is unclear currently whether this is a bug where orchestrations run early, or a clock difference/error, |
| 74 | + // but leaving this logic in for now until further investigation. |
| 75 | + Assert.True(DateTime.UtcNow + TimeSpan.FromSeconds(2) >= scheduledStartTime); |
| 76 | + |
| 77 | + var finalOrchestrationDetails = await DurableHelpers.GetRunningOrchestrationDetailsAsync(statusQueryGetUri); |
| 78 | + WriteOutput($"Last updated at {finalOrchestrationDetails.LastUpdatedTime}, scheduled to complete at {scheduledStartTime}"); |
| 79 | + Assert.True(finalOrchestrationDetails.LastUpdatedTime + TimeSpan.FromSeconds(2) >= scheduledStartTime); |
| 80 | + } |
| 81 | + |
| 82 | + [Theory] |
| 83 | + [InlineData("EntityCreatesScheduledOrchestrationOrchestrator_HttpStart", 10, HttpStatusCode.Accepted)] |
| 84 | + [InlineData("EntityCreatesScheduledOrchestrationOrchestrator_HttpStart", -5, HttpStatusCode.Accepted)] |
| 85 | + [Trait("PowerShell", "Skip")] // Durable Entities not yet implemented in PowerShell |
| 86 | + [Trait("Java", "Skip")] // Durable Entities not yet implemented in Java |
| 87 | + [Trait("Python", "Skip")] // Durable Entities do not support the "schedule new orchestration" action in Python |
| 88 | + [Trait("Node", "Skip")] // Durable Entities do not support the "schedule new orchestration" action in Node |
| 89 | + [Trait("MSSQL", "Skip")] // Durable Entities are not supported in MSSQL for out-of-proc (see https://github.com/microsoft/durabletask-mssql/issues/205) |
| 90 | + public async Task ScheduledStartFromEntitiesTests(string functionName, int startDelaySeconds, HttpStatusCode expectedStatusCode) |
| 91 | + { |
| 92 | + var testStartTime = DateTime.UtcNow; |
| 93 | + var scheduledStartTime = testStartTime + TimeSpan.FromSeconds(startDelaySeconds); |
| 94 | + string urlQueryString = $"?scheduledStartDelaySeconds={startDelaySeconds}"; |
| 95 | + |
| 96 | + using HttpResponseMessage response = await HttpHelpers.InvokeHttpTrigger(functionName, urlQueryString); |
| 97 | + |
| 98 | + string statusQueryGetUri = await DurableHelpers.ParseStatusQueryGetUriAsync(response); |
| 99 | + |
| 100 | + Assert.Equal(expectedStatusCode, response.StatusCode); |
| 101 | + await DurableHelpers.WaitForOrchestrationStateAsync(statusQueryGetUri, "Completed", Math.Max(startDelaySeconds, 0) + 30); |
| 102 | + var schedulerOrchestrationDetails = await DurableHelpers.GetRunningOrchestrationDetailsAsync(statusQueryGetUri); |
| 103 | + string subOrchestratorInstanceId = schedulerOrchestrationDetails.Output; |
| 104 | + |
| 105 | + string subOrchestratorStatusQueryGetUri = statusQueryGetUri.ToLower().Replace(schedulerOrchestrationDetails.InstanceId.ToLower(), subOrchestratorInstanceId); |
| 106 | + |
| 107 | + // Azure Storage backend has a quirk where creating an orchestration from an entity creates the OrchestrationStarted event in the History table |
| 108 | + // but doesn't initialize the orchestration state in the Instances table until the orchestration starts running. Since the implementation for |
| 109 | + // GetOrchestrationStateAsync in AzureStorage only queries the Instances table, this will 404 until the orchestration state becomes "running", |
| 110 | + // so we can't check for "Pending". |
| 111 | + // The checks below should still suffice to prove that the orchestration did not run until the scheduled time. |
| 112 | + if (scheduledStartTime > DateTime.UtcNow + TimeSpan.FromSeconds(1) && this.fixture.GetDurabilityProvider() != FunctionAppFixture.ConfiguredDurabilityProviderType.AzureStorage) |
| 113 | + { |
| 114 | + // This line will throw if the orchestration goes to a terminal state before reaching "Pending", |
| 115 | + // ensuring that any scheduled orchestrations that run immediately fail the test. |
| 116 | + await DurableHelpers.WaitForOrchestrationStateAsync(subOrchestratorStatusQueryGetUri, "Pending", 30); |
| 117 | + } |
| 118 | + |
| 119 | + await DurableHelpers.WaitForOrchestrationStateAsync(subOrchestratorStatusQueryGetUri, "Completed", Math.Max(startDelaySeconds, 0) + 30); |
| 120 | + |
| 121 | + // This +2s should not be necessary - however, experimentally the orchestration may run up to ~1 second before the scheduled time. |
| 122 | + // It is unclear currently whether this is a bug where orchestrations run early, or a clock difference/error, |
| 123 | + // but leaving this logic in for now until further investigation. |
| 124 | + Assert.True(DateTime.UtcNow + TimeSpan.FromSeconds(2) >= scheduledStartTime); |
| 125 | + |
| 126 | + var subOrchestrationDetails = await DurableHelpers.GetRunningOrchestrationDetailsAsync(subOrchestratorStatusQueryGetUri); |
| 127 | + Assert.True(subOrchestrationDetails.LastUpdatedTime + TimeSpan.FromSeconds(2) >= scheduledStartTime); |
| 128 | + Assert.Equal("Success", subOrchestrationDetails.Output); |
| 129 | + } |
| 130 | +} |
0 commit comments