Skip to content

Commit

Permalink
Add rewind core scenarii
Browse files Browse the repository at this point in the history
  • Loading branch information
Greybird committed May 28, 2022
1 parent c938483 commit 9a54563
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
59 changes: 59 additions & 0 deletions test/DurableTask.SqlServer.AzureFunctions.Tests/CoreScenarios.cs
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,32 @@ public async Task CanInvokeSubOrchestration()
Assert.Equal("done", status.Output);
}

[Fact]
public async Task CanRewindOrchestration()
{
Functions.ThrowExceptionInCanFail = true;
DurableOrchestrationStatus status = await this.RunOrchestrationAsync(nameof(Functions.RewindOrchestration));
Assert.Equal(OrchestrationRuntimeStatus.Failed, status.RuntimeStatus);

Functions.ThrowExceptionInCanFail = false;
status = await this.RewindOrchestrationAsync(status.InstanceId);
Assert.Equal(OrchestrationRuntimeStatus.Completed, status.RuntimeStatus);
Assert.Equal("activity", status.Output);
}

[Fact]
public async Task CanRewindSubOrchestration()
{
Functions.ThrowExceptionInCanFail = true;
DurableOrchestrationStatus status = await this.RunOrchestrationAsync(nameof(Functions.RewindSubOrchestration));
Assert.Equal(OrchestrationRuntimeStatus.Failed, status.RuntimeStatus);

Functions.ThrowExceptionInCanFail = false;
status = await this.RewindOrchestrationAsync(status.InstanceId);
Assert.Equal(OrchestrationRuntimeStatus.Completed, status.RuntimeStatus);
Assert.Equal("0,1,2,activity", status.Output);
}

static class Functions
{
[FunctionName(nameof(Sequence))]
Expand Down Expand Up @@ -458,6 +484,39 @@ public static async Task<string> SubOrchestrationTest([OrchestrationTrigger] IDu
await ctx.CallSubOrchestratorAsync(nameof(NoOp), "NoOpInstanceId", null);
return "done";
}

[FunctionName(nameof(RewindOrchestration))]
public static async Task<string> RewindOrchestration([OrchestrationTrigger] IDurableOrchestrationContext ctx)
{
return await ctx.CallActivityAsync<string>(nameof(CanFail), "activity");
}

[FunctionName(nameof(RewindSubOrchestration))]
public static async Task<string> RewindSubOrchestration([OrchestrationTrigger] IDurableOrchestrationContext ctx)
{
var tasks = new List<Task<string>>();
for (int i = 0; i < 3; i++)
{
tasks.Add(ctx.CallActivityAsync<string>(nameof(CanFail), i.ToString()));
}
tasks.Add(ctx.CallSubOrchestratorAsync<string>(nameof(RewindOrchestration), "RewindOrchestrationId", "suborchestration"));

var results = await Task.WhenAll(tasks);
return string.Join(',', results);
}

public static bool ThrowExceptionInCanFail { get; set; }

[FunctionName(nameof(CanFail))]
public static string CanFail([ActivityTrigger] IDurableActivityContext context)
{
if (ThrowExceptionInCanFail)
{
throw new Exception("exception");
}

return context.GetInput<string>();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,17 @@ protected async Task<DurableOrchestrationStatus> StartOrchestrationAsync(
return status;
}

protected async Task<DurableOrchestrationStatus> RewindOrchestrationAsync(string instanceId)
{
IDurableClient client = await this.GetDurableClientAsync();
await client.RewindAsync(instanceId, "rewind");

TimeSpan timeout = Debugger.IsAttached ? TimeSpan.FromMinutes(5) : TimeSpan.FromSeconds(10);
DurableOrchestrationStatus status = await client.WaitForCompletionAsync(instanceId, timeout);
Assert.NotNull(status);
return status;
}

protected async Task<IDurableClient> GetDurableClientAsync()
{
var clientRef = new IDurableClient[1];
Expand Down

0 comments on commit 9a54563

Please sign in to comment.