Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expand long running timer functionality to WaitForExternalEvent #1550

Merged
merged 2 commits into from
Nov 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -383,26 +383,16 @@ async Task<T> IDurableOrchestrationContext.CreateTimer<T>(DateTime fireAt, T sta
return result;
}

internal Task OutOfProcCreateTimer(DurableOrchestrationContext ctx, DateTime fireAt, CancellationToken cancelToken)
{
if (!this.ValidOutOfProcTimer(fireAt, out string errorMessage))
{
throw new ArgumentException(errorMessage, nameof(fireAt));
}

return ((IDurableOrchestrationContext)ctx).CreateTimer(fireAt, cancelToken);
}

private bool ValidOutOfProcTimer(DateTime fireAt, out string errorMessage)
// We now have built in long-timer support for C#, but in some scenarios, such as out-of-proc,
// we may still need to enforce this limitations until the solution works there as well.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: this -> these

internal void ThrowIfInvalidTimerLengthForStorageProvider(DateTime fireAt)
{
this.ThrowIfInvalidAccess();

if (!this.durabilityProvider.ValidateDelayTime(fireAt.Subtract(this.InnerContext.CurrentUtcDateTime), out errorMessage))
if (!this.durabilityProvider.ValidateDelayTime(fireAt.Subtract(this.InnerContext.CurrentUtcDateTime), out string errorMessage))
{
return false;
throw new ArgumentException(errorMessage, nameof(fireAt));
}
ConnorMcMahon marked this conversation as resolved.
Show resolved Hide resolved

return true;
}

/// <inheritdoc />
Expand Down Expand Up @@ -943,11 +933,6 @@ internal void RescheduleBufferedExternalEvents()

private Task<T> WaitForExternalEvent<T>(string name, TimeSpan timeout, Action<TaskCompletionSource<T>> timeoutAction, CancellationToken cancelToken)
{
if (!this.durabilityProvider.ValidateDelayTime(timeout, out string errorMessage))
{
throw new ArgumentException(errorMessage, nameof(timeout));
}

var tcs = new TaskCompletionSource<T>();
var cts = CancellationTokenSource.CreateLinkedTokenSource(cancelToken);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ private async Task ProcessAsyncActions(AsyncAction[][] actions)
foreach (AsyncAction[] actionSet in actions)
{
var tasks = new List<Task>(actions.Length);
DurableOrchestrationContext ctx = this.context as DurableOrchestrationContext;

// An actionSet represents all actions that were scheduled within that execution.
foreach (AsyncAction action in actionSet)
Expand All @@ -127,17 +128,13 @@ private async Task ProcessAsyncActions(AsyncAction[][] actions)
case AsyncActionType.CreateTimer:
using (var cts = new CancellationTokenSource())
{
DurableOrchestrationContext ctx = this.context as DurableOrchestrationContext;

if (ctx != null)
{
tasks.Add(ctx.OutOfProcCreateTimer(ctx, action.FireAt, cts.Token));
}
else
{
tasks.Add(this.context.CreateTimer(action.FireAt, cts.Token));
ctx.ThrowIfInvalidTimerLengthForStorageProvider(action.FireAt);
}

tasks.Add(this.context.CreateTimer(action.FireAt, cts.Token));

if (action.IsCanceled)
{
cts.Cancel();
Expand Down
54 changes: 0 additions & 54 deletions test/Common/DurableTaskEndToEndTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1554,34 +1554,6 @@ public async Task WaitForExternalEventWithTimeout(string defaultValue, bool send
}
}

/// <summary>
/// End-to-end test which validates correct exceptions for invalid timeout values.
/// </summary>
[Fact]
[Trait("Category", PlatformSpecificHelpers.TestCategory)]
public async Task WaitForExternalEventWithTooLargeTimeout()
{
var orchestratorFunctionNames = new[] { nameof(TestOrchestrations.ApprovalWithTimeout) };
var extendedSessions = false;
using (ITestHost host = TestHelpers.GetJobHost(
this.loggerProvider,
nameof(this.WaitForExternalEventWithTooLargeTimeout),
extendedSessions))
{
await host.StartAsync();

var timeout = TimeSpan.FromDays(7);
var client = await host.StartOrchestratorAsync(orchestratorFunctionNames[0], (timeout, "throw"), this.output);
await client.WaitForStartupAsync(this.output);

var status = await client.WaitForCompletionAsync(this.output);
Assert.Equal(OrchestrationRuntimeStatus.Completed, status?.RuntimeStatus);
Assert.Equal("ArgumentException", status?.Output.ToString());

await host.StopAsync();
}
}

/// <summary>
/// End-to-end test which validates a CancellationToken-providing overload of WaitForExternalEvent.
/// </summary>
Expand Down Expand Up @@ -3771,32 +3743,6 @@ public async Task AzureStorage_MaxRetryIntervalLimitHit_ThrowsException()
}
}

[Fact]
[Trait("Category", PlatformSpecificHelpers.TestCategory)]
public async Task AzureStorage_EventTimeoutLimitHit_ThrowsException()
{
string orchestrationFunctionName = nameof(TestOrchestrations.SimpleEventWithTimeoutSucceeds);

using (var host = TestHelpers.GetJobHost(
this.loggerProvider,
nameof(this.AzureStorage_EventTimeoutLimitHit_ThrowsException),
false))
{
await host.StartAsync();

var timeout = TimeSpan.FromDays(7);

var client = await host.StartOrchestratorAsync(orchestrationFunctionName, timeout, this.output);

var status = await client.WaitForCompletionAsync(this.output);

Assert.Equal(OrchestrationRuntimeStatus.Failed, status.RuntimeStatus);

string output = status.Output.ToString();
Assert.Contains("timeout", output);
}
}

/// <summary>
/// End-to-end test which validates basic use of the object dispatch feature.
/// TODO: This test is flakey in Functions V1.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@

namespace WebJobs.Extensions.DurableTask.Tests.V2
{
public class TimerTests
public class LongTimerTests
{
private readonly ITestOutputHelper output;
private readonly TestLoggerProvider loggerProvider;

public TimerTests(ITestOutputHelper output)
public LongTimerTests(ITestOutputHelper output)
{
this.output = output;
this.loggerProvider = new TestLoggerProvider(output);
Expand Down Expand Up @@ -54,7 +54,7 @@ public async Task TimerLengthLessThanMaxTime(bool extendedSessions)
{
using (ITestHost host = TestHelpers.GetJobHost(
this.loggerProvider,
nameof(this.LongRunningTimer),
nameof(this.TimerLengthLessThanMaxTime),
extendedSessions,
storageProviderType: "azure_storage",
durabilityProviderFactoryType: typeof(AzureStorageShortenedTimerDurabilityProviderFactory)))
Expand All @@ -78,7 +78,7 @@ public async Task EntitySignalWithLongDelay(bool extendedSessions)
{
using (var host = TestHelpers.GetJobHost(
this.loggerProvider,
nameof(this.LongRunningTimer),
nameof(this.EntitySignalWithLongDelay),
extendedSessions,
storageProviderType: "azure_storage",
durabilityProviderFactoryType: typeof(AzureStorageShortenedTimerDurabilityProviderFactory)))
Expand All @@ -103,5 +103,29 @@ public async Task EntitySignalWithLongDelay(bool extendedSessions)
await host.StopAsync();
}
}

[Fact]
[Trait("Category", PlatformSpecificHelpers.TestCategory)]
public async Task WaitForExternalEventAboveMaximumTimerLength()
{
using (ITestHost host = TestHelpers.GetJobHost(
this.loggerProvider,
nameof(this.WaitForExternalEventAboveMaximumTimerLength),
enableExtendedSessions: false,
storageProviderType: "azure_storage",
durabilityProviderFactoryType: typeof(AzureStorageShortenedTimerDurabilityProviderFactory)))
{
await host.StartAsync();

var fireAt = TimeSpan.FromSeconds(90);
var client = await host.StartOrchestratorAsync(nameof(TestOrchestrations.ApprovalWithTimeout), (fireAt, "throw"), this.output);
var status = await client.WaitForCompletionAsync(this.output, timeout: TimeSpan.FromMinutes(2));

Assert.Equal(OrchestrationRuntimeStatus.Completed, status?.RuntimeStatus);
Assert.Equal("TimeoutException", status?.Output);

await host.StopAsync();
}
}
}
}