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 1 commit
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,14 @@ 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)
internal void ValidateOutOfProcTimer(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 +931,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.ValidateOutOfProcTimer(action.FireAt);
ConnorMcMahon marked this conversation as resolved.
Show resolved Hide resolved
}

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

if (action.IsCanceled)
{
cts.Cancel();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT License. See LICENSE in the project root for license information.

using System;
using System.Security.Cryptography;
ConnorMcMahon marked this conversation as resolved.
Show resolved Hide resolved
using System.Threading.Tasks;
using Microsoft.Azure.WebJobs.Extensions.DurableTask;
using Microsoft.Azure.WebJobs.Extensions.DurableTask.Tests;
Expand All @@ -11,12 +12,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 @@ -103,5 +104,29 @@ public async Task EntitySignalWithLongDelay(bool extendedSessions)
await host.StopAsync();
}
}

[Fact]
[Trait("Category", PlatformSpecificHelpers.TestCategory)]
public async Task WaitForExternalEventAboveMaxTime()
{
using (ITestHost host = TestHelpers.GetJobHost(
this.loggerProvider,
nameof(this.WaitForExternalEventAboveMaxTime),
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();
}
}
ConnorMcMahon marked this conversation as resolved.
Show resolved Hide resolved
}
}