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 all commits
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
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();
kijujjav marked this conversation as resolved.
Show resolved Hide resolved
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);
}
kijujjav marked this conversation as resolved.
Show resolved Hide resolved

[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