Skip to content

Commit

Permalink
[AzureServiceFabric provider] Implement ContinueAsNew functionality. (#…
Browse files Browse the repository at this point in the history
…362)

* [AzureServiceFabric] Implement ContinueAsNew functionality.

* Fixes for ContinueAsNew functionality.

* Add test for RecurringOrchestration.

* Convert RetryPolicy to interface. Remove confusing assert.
  • Loading branch information
tkamesh authored Mar 18, 2020
1 parent bbbac72 commit 76912f3
Show file tree
Hide file tree
Showing 20 changed files with 327 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,51 @@ public async Task Orchestration_With_TimeoutWrapper_Test()
Console.WriteLine($"Time for Orchestration with 5 second running task wrapped in 1 second timeout : {result.CompletedTime - result.CreatedTime}");
}

[TestMethod]
public async Task GenerationBasicTest()
{
var instanceId = nameof(GenerationBasicOrchestration);
var instance = await this.taskHubClient.CreateOrchestrationInstanceAsync(typeof(GenerationBasicOrchestration), instanceId, 2);
var result = await this.taskHubClient.WaitForOrchestrationAsync(instance, TimeSpan.FromMinutes(2));

Assert.AreEqual(OrchestrationStatus.ContinuedAsNew, result.OrchestrationStatus);

var state = await this.taskHubClient.GetOrchestrationStateAsync(instanceId);
result = await this.taskHubClient.WaitForOrchestrationAsync(state.OrchestrationInstance, TimeSpan.FromMinutes(2));

Assert.AreEqual(OrchestrationStatus.Completed, result.OrchestrationStatus);
Assert.AreEqual("2", result.Output, "Orchestration Result is wrong!!!");
Assert.AreEqual(result.OrchestrationInstance.InstanceId, instance.InstanceId);
Assert.AreNotEqual(result.OrchestrationInstance.ExecutionId, instance.ExecutionId);
}

[TestMethod]
public async Task RecurringOrchestrationTest()
{
var instanceId = nameof(RecurringOrchestration);
var input = new RecurringOrchestrationInput
{
TargetOrchestrationInput = "1",
TargetOrchestrationType = typeof(RecurringTargetOrchestration).ToString(),
TargetOrchestrationInstanceId = nameof(RecurringTargetOrchestration)
};
var instance = await this.taskHubClient.CreateOrchestrationInstanceAsync(typeof(RecurringOrchestration), instanceId, input);
await Task.Delay(TimeSpan.FromSeconds(1));
var result = await this.taskHubClient.GetOrchestrationStateAsync(input.TargetOrchestrationInstanceId, false);

Assert.AreNotEqual("4", result[0].Output, "Orchestration Result is wrong!!!");

OrchestrationState state;
do
{
state = await this.taskHubClient.GetOrchestrationStateAsync(instanceId);
await Task.Delay(TimeSpan.FromSeconds(1));
} while (state.OrchestrationStatus != OrchestrationStatus.Completed);

Assert.AreEqual(OrchestrationStatus.Completed, state.OrchestrationStatus);
Assert.AreEqual("4", state.Output, "Orchestration Result is wrong!!!");
}

[TestMethod]
public async Task Orchestration_With_Same_Id_Cant_Be_Started_While_Running()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
namespace TestApplication.Common.OrchestrationTasks
{
using System.Threading.Tasks;
using DurableTask.Test.Orchestrations.Performance;

public interface ITestTasks
{
Expand All @@ -24,5 +23,17 @@ public interface ITestTasks
/// <param name="remainingAttempts">remaining number of attempts</param>
/// <returns>bool indicating whether task completed successfully or not.</returns>
Task<bool> ThrowExceptionAsync(int remainingAttempts);

/// <summary>
/// Increments Generation Count variable.
/// </summary>
/// <returns>Generation count</returns>
Task<int> IncrementGenerationCount();

/// <summary>
/// Utility method to reset counter at the beginning of test.
/// </summary>
/// <returns>Generation coutner value</returns>
Task<int> ResetGenerationCounter();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,40 @@ namespace TestApplication.Common.OrchestrationTasks

public class TestTasks : ITestTasks
{
static int generationCount = 0;

/// <summary>
/// Increments Generation Count variable.
/// </summary>
/// <returns>Generation count</returns>
public Task<int> IncrementGenerationCount()
{
return Task.FromResult(++generationCount);
}

/// <summary>
/// Utility method to reset counter at the beginning of test.
/// </summary>
/// <returns>Generation coutner value</returns>
public Task<int> ResetGenerationCounter()
{
generationCount = 0;
return Task.FromResult(generationCount);
}

/// <summary>
/// Throws exception when remainingAttempts > 0. Otherwise succeeds.
/// </summary>
/// <param name="remainingAttempts">remaining number of attempts</param>
/// <returns>bool indicating whether task completed successfully or not.</returns>
public async Task<bool> ThrowExceptionAsync(int remainingAttempts)
public Task<bool> ThrowExceptionAsync(int remainingAttempts)
{
if (remainingAttempts > 0)
{
throw new CounterException(remainingAttempts);
}

return true;
return Task.FromResult(true);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// ----------------------------------------------------------------------------------
// Copyright Microsoft Corporation
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ----------------------------------------------------------------------------------

namespace TestApplication.Common.Orchestrations
{
using System.Threading.Tasks;

using DurableTask.Core;

using TestApplication.Common.OrchestrationTasks;

public class GenerationBasicOrchestration : TaskOrchestration<int, int>
{
// HACK: This is just a hack to communicate result of orchestration back to test
public static int Result;

public override async Task<int> RunTask(OrchestrationContext context, int numberOfGenerations)
{
var testTasks = context.CreateClient<ITestTasks>();
int count = await testTasks.IncrementGenerationCount();
numberOfGenerations--;
if (numberOfGenerations > 0)
{
context.ContinueAsNew(numberOfGenerations);
}

// This is a HACK to get unit test up and running. Should never be done in actual code.
Result = count;
return count;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// ----------------------------------------------------------------------------------
// Copyright Microsoft Corporation
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ----------------------------------------------------------------------------------

namespace TestApplication.Common.Orchestrations
{
using System;
using System.Threading;
using System.Threading.Tasks;

using DurableTask.Core;

using TestApplication.Common.OrchestrationTasks;

public class RecurringOrchestration : TaskOrchestration<int, RecurringOrchestrationInput>
{
// HACK: This is just a hack to communicate result of orchestration back to test
private static int targetOchestrationInvocationsCount = 0;

public override async Task<int> RunTask(OrchestrationContext context, RecurringOrchestrationInput input)
{
var testTasks = context.CreateClient<ITestTasks>();

if (targetOchestrationInvocationsCount == 0)
{
// First time, Reset Generation Count variable.
await testTasks.ResetGenerationCounter();
}

int result = await context.CreateSubOrchestrationInstance<int>(input.TargetOrchestrationType,
GetTargetOrchestrationVersion(),
input.TargetOrchestrationInstanceId,
input.TargetOrchestrationInput);

await context.CreateTimer(GetNextExecutionTime(context), true, CancellationToken.None);
if (ShouldRepeatTargetOrchestration())
{
context.ContinueAsNew(input);
}
else
{
// Finally, Reset Generation Count variable.
await testTasks.ResetGenerationCounter();
}

return result;
}

public virtual DateTime GetNextExecutionTime(OrchestrationContext context)
{
return context.CurrentUtcDateTime.AddSeconds(Math.Pow(2, targetOchestrationInvocationsCount));
}

public virtual string GetTargetOrchestrationVersion()
{
return string.Empty;
}

public virtual bool ShouldRepeatTargetOrchestration()
{
targetOchestrationInvocationsCount++;

return targetOchestrationInvocationsCount < 4;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// ----------------------------------------------------------------------------------
// Copyright Microsoft Corporation
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ----------------------------------------------------------------------------------

namespace TestApplication.Common.Orchestrations
{
public class RecurringOrchestrationInput
{
public string TargetOrchestrationType { get; set; }

public string TargetOrchestrationInput { get; set; }

public string TargetOrchestrationInstanceId { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// ----------------------------------------------------------------------------------
// Copyright Microsoft Corporation
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ----------------------------------------------------------------------------------

namespace TestApplication.Common.Orchestrations
{
using System.Threading.Tasks;

using DurableTask.Core;

using TestApplication.Common.OrchestrationTasks;

public class RecurringTargetOrchestration : TaskOrchestration<int, string>
{
public override async Task<int> RunTask(OrchestrationContext context, string input)
{
var testTasks = context.CreateClient<ITestTasks>();
int count = await testTasks.IncrementGenerationCount();
return count;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ private IEnumerable<Type> GetOrchestrationTypes()
typeof(SimpleOrchestrationWithTasks),
typeof(SimpleOrchestrationWithTimer),
typeof(SimpleOrchestrationWithSubOrchestration),
typeof(GenerationBasicOrchestration),
typeof(RecurringOrchestration),
typeof(RecurringTargetOrchestration),
typeof(DriverOrchestration),
typeof(TestOrchestration),
typeof(ExecutionCountingOrchestration)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<TargetFrameworks>net461</TargetFrameworks>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<PackageId>Microsoft.Azure.DurableTask.AzureServiceFabric</PackageId>
<Version>2.2.0</Version>
<Version>2.3.0</Version>
<AssemblyVersion>$(Version)</AssemblyVersion>
<FileVersion>$(Version)</FileVersion>
</PropertyGroup>
Expand Down
Loading

0 comments on commit 76912f3

Please sign in to comment.