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

Added RetryCurrentTreeNodeActions flag support to re-execute node upo… #46

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 @@ -54,6 +54,7 @@
<Compile Include="test\ActionsAndCallbacks\BaseCommonAction.cs" />
<Compile Include="test\ActionsAndCallbacks\CollectDiagnosticsAction.cs" />
<Compile Include="test\ActionsAndCallbacks\ForgeUserContext.cs" />
<Compile Include="test\ActionsAndCallbacks\GetCurrentTimeStampAction.cs" />
<Compile Include="test\ActionsAndCallbacks\ReturnSessionIdAction.cs" />
<Compile Include="test\ActionsAndCallbacks\RevisitAction.cs" />
<Compile Include="test\ActionsAndCallbacks\TardigradeAction.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//-----------------------------------------------------------------------
// <copyright file="ReturnSessionIdAction.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
// <summary>
// The ReturnSessionIdAction class implements the BaseCommonAction abstract class.
// </summary>
//-----------------------------------------------------------------------

namespace Microsoft.Forge.TreeWalker.UnitTests
{
using System;
using System.Threading.Tasks;

using Microsoft.Forge.Attributes;
using Microsoft.Forge.TreeWalker;

[ForgeAction]
public class GetCurrentTimeStamp : BaseCommonAction
{
public override Task<ActionResponse> RunAction()
{
return Task.FromResult(new ActionResponse() { Status = DateTime.UtcNow.ToString(), StatusCode = 1 });
}
}
}
21 changes: 21 additions & 0 deletions Forge.TreeWalker.UnitTests/test/ForgeSchemaHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,27 @@ public static class ForgeSchemaHelper
}
";

public const string ReExecuteNodeSchema = @"
{
""Tree"": {
""Root"": {
""Type"": ""Action"",
""Actions"": {
""Root_GetCurrentTimeStampAction"": {
""Action"": ""GetCurrentTimeStamp""
}
},
""ChildSelector"": [
{
""ShouldSelect"": ""C#|Session.GetLastActionResponse().StatusCode == 0"",
""Child"": ""Root""
}
]
}
}
}
";

public const string Cycle_SubroutineActionUsesDifferentSessionId = @"
{
""RootTree"": {
Expand Down
61 changes: 60 additions & 1 deletion Forge.TreeWalker.UnitTests/test/TreeWalkerUnitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -500,12 +500,71 @@ public void TestTreeWalkerSession_WalkTree_Failed_MissingKey()
public void TestTreeWalkerSession_WalkTree_NoChildMatched()
{
this.TestInitialize(jsonSchema: ForgeSchemaHelper.NoChildMatch);

// Test - WalkTree and expect the Status to be NoChildMatched.
string actualStatus = this.session.WalkTree("Root").GetAwaiter().GetResult();
kijujjav marked this conversation as resolved.
Show resolved Hide resolved
Assert.AreEqual("RanToCompletion_NoChildMatched", actualStatus, "Expected WalkTree to end with NoChildMatched status.");
}

[TestMethod]
public void TestReexecutingNode_WithRetryCurrentTreeNodeActionsFlag_Success()
kijujjav marked this conversation as resolved.
Show resolved Hide resolved
{
this.TestInitialize(jsonSchema: ForgeSchemaHelper.ReExecuteNodeSchema);

this.session.WalkTree("Root").GetAwaiter().GetResult();
kijujjav marked this conversation as resolved.
Show resolved Hide resolved
string time1 = session.GetLastActionResponse().Status;

this.session = new TreeWalkerSession(new TreeWalkerParameters(
this.sessionId,
parameters.ForgeTree,
parameters.ForgeState,
this.callbacksV2,
this.token)
{
UserContext = new ForgeUserContext(),
ForgeActionsAssembly = typeof(CollectDiagnosticsAction).Assembly,
InitializeSubroutineTree = this.InitializeSubroutineTree,
Dependencies = new List<Type>() { typeof(FooEnum) },
ScriptCache = this.scriptCache
});
kijujjav marked this conversation as resolved.
Show resolved Hide resolved

this.session.WalkTree("Root").GetAwaiter().GetResult();
string time2 = session.GetLastActionResponse().Status;

Assert.IsTrue(time1 == time2);

}
kijujjav marked this conversation as resolved.
Show resolved Hide resolved

[TestMethod]
public void TestReexecutingNode_WithoutRetryCurrentTreeNodeActionsFlag_Success()
{
this.TestInitialize(jsonSchema: ForgeSchemaHelper.ReExecuteNodeSchema);
this.session.Parameters.RetryCurrentTreeNodeActions = true;

this.session.WalkTree("Root").GetAwaiter().GetResult();
string time1 = session.GetLastActionResponse().Status;

this.session = new TreeWalkerSession(new TreeWalkerParameters(
this.sessionId,
parameters.ForgeTree,
parameters.ForgeState,
this.callbacksV2,
this.token)
{
UserContext = new ForgeUserContext(),
ForgeActionsAssembly = typeof(CollectDiagnosticsAction).Assembly,
InitializeSubroutineTree = this.InitializeSubroutineTree,
Dependencies = new List<Type>() { typeof(FooEnum) },
ScriptCache = this.scriptCache,
RetryCurrentTreeNodeActions = true
});
kijujjav marked this conversation as resolved.
Show resolved Hide resolved

this.session.WalkTree("Root").GetAwaiter().GetResult();
string time2 = session.GetLastActionResponse().Status;

Assert.IsFalse(time1 == time2);
kijujjav marked this conversation as resolved.
Show resolved Hide resolved

}

#endregion WalkTree

[TestMethod]
Expand Down
5 changes: 5 additions & 0 deletions Forge.TreeWalker/src/TreeWalkerParameters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,11 @@ public class TreeWalkerParameters
/// </summary>
public object TreeInput { get; set; }

/// <summary>
/// Flag that will allow forge to re-execute the Node upon re-hydration.
kijujjav marked this conversation as resolved.
Show resolved Hide resolved
/// </summary>
public bool RetryCurrentTreeNodeActions { get; set; }

#endregion

#region Constructor with ITreeWalkerCallbacks, [DEPRECATED]
Expand Down
2 changes: 1 addition & 1 deletion Forge.TreeWalker/src/TreeWalkerSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1123,7 +1123,7 @@ private async Task CommitCurrentTreeNode(string treeNodeKey, TreeNode treeNode)
List<KeyValuePair<string, object>> itemsToPersist = new List<KeyValuePair<string, object>>();

// Handle revisit/cycle case if this node has been previously completed and we aren't rehydrating.
kijujjav marked this conversation as resolved.
Show resolved Hide resolved
if (treeNode.Actions != null && this.hasSessionRehydrated)
if (treeNode.Actions != null && (this.hasSessionRehydrated || this.Parameters.RetryCurrentTreeNodeActions))
{
// Check if all actions already have an action response.
foreach (KeyValuePair<string, TreeAction> kvp in treeNode.Actions)
Expand Down