Skip to content

Commit

Permalink
Merge pull request #46 from kijujjav/feature/kijujjav/RetryCurrentTre…
Browse files Browse the repository at this point in the history
…eNodeActions

Added RetryCurrentTreeNodeActions flag support to re-execute node upon rehydration
  • Loading branch information
kijujjav committed Sep 10, 2021
2 parents d12ed9c + 2c88d20 commit 804b0a7
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 2 deletions.
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_RevisitAction"": {
""Action"": ""RevisitAction""
}
},
""ChildSelector"": [
{
""ShouldSelect"": ""C#|(int)Session.GetLastActionResponse().Output > 2"",
""Child"": ""Root""
}
]
}
}
}
";

public const string Cycle_SubroutineActionUsesDifferentSessionId = @"
{
""RootTree"": {
Expand Down
38 changes: 38 additions & 0 deletions Forge.TreeWalker.UnitTests/test/TreeWalkerUnitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,44 @@ public void TestTreeWalkerSession_WalkTree_NoChildMatched()
Assert.AreEqual("RanToCompletion_NoChildMatched", actualStatus, "Expected WalkTree to end with NoChildMatched status.");
}

[TestMethod]
public void TestReexecutingNode_WithoutRetryCurrentTreeNodeActionsFlag_Success()
{
this.TestInitialize(jsonSchema: ForgeSchemaHelper.ReExecuteNodeSchema);

// Test - WalkTree twice without the RetryCurrentTreeNodeActions flag set.
// Expect the Action to not get re-executed since the ActionResponse already exists and we are rehydrating.
this.session.WalkTree("Root").GetAwaiter().GetResult();
int count1 = (int)session.GetLastActionResponse().Output;

this.session = new TreeWalkerSession(this.parameters);

this.session.WalkTree("Root").GetAwaiter().GetResult();
int count2 = (int)session.GetLastActionResponse().Output;

Assert.IsTrue(count1 == count2);
}

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

// Test - WalkTree twice with the RetryCurrentTreeNodeActions flag set.
// Expect the Action to get re-executed even when the ActionResponse already exists and we are rehydrating.
this.session.WalkTree("Root").GetAwaiter().GetResult();
int count1 = (int)session.GetLastActionResponse().Output;

this.parameters.RetryCurrentTreeNodeActions = true;
this.session = new TreeWalkerSession(this.parameters);

this.session.WalkTree("Root").GetAwaiter().GetResult();
int count2 = (int)session.GetLastActionResponse().Output;

Assert.IsFalse(count1 == count2);
}

#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 allows Forge to re-execute the current TreeNode's Actions upon rehydration.
/// </summary>
public bool RetryCurrentTreeNodeActions { get; set; }

#endregion

#region Constructor with ITreeWalkerCallbacks, [DEPRECATED]
Expand Down
4 changes: 2 additions & 2 deletions Forge.TreeWalker/src/TreeWalkerSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1122,8 +1122,8 @@ 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.
if (treeNode.Actions != null && this.hasSessionRehydrated)
// Handle revisit/cycle case if this node has been previously completed and we are rehydrated or the override flag is set.
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

0 comments on commit 804b0a7

Please sign in to comment.