Skip to content

Commit

Permalink
Feature/forceparallelactions (#57)
Browse files Browse the repository at this point in the history
* Force ParallelActions by yielding back the task

* Fixing flaky test.
  • Loading branch information
TravisOnGit authored Jan 10, 2024
1 parent d18d5ea commit de29125
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
16 changes: 12 additions & 4 deletions Forge.TreeWalker.UnitTests/test/TreeWalkerUnitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -468,17 +468,25 @@ public void TestTreeWalkerSession_WalkTree_ActionWithDelay_CancelWalkTree()
// This gives us time to start WalkTree before calling CancelWalkTree.
this.TestInitialize(jsonSchema: ForgeSchemaHelper.ActionDelay_ContinuationOnTimeout_RetryPolicy_TimeoutInAction);

// Test - WalkTree then CancelWalkTree while WalkTree is running and expect the Status to be Cancelled.
// Test - WalkTree then CancelWalkTree while WalkTree is running and expect the Status to be either:
// 1. CancelledBeforeExecution if TaskCanceledException is thrown from WalkTree. This happens when the ForgeAction gets canceled and honored by Forge.
// 2. Cancelled if OperationCanceledException is thrown from WalkTree. This happens when some other Task wins the race to get cancelled first, such as nodeTimeoutTask.
Task<string> task = this.session.WalkTree("Root");
Thread.Sleep(25);
this.session.CancelWalkTree();
Assert.ThrowsException<OperationCanceledException>(() =>

try
{
string temp = task.GetAwaiter().GetResult();
}, "Expected WalkTree to throw exception after calling CancelWalkTree.");
Assert.Fail("Expected WalkTree to throw exception after calling CancelWalkTree.");
}
catch { }

string actualStatus = this.session.Status;
Assert.AreEqual("Cancelled", actualStatus, "Expected WalkTree to be cancelled after calling CancelWalkTree.");
if (actualStatus != "Cancelled" && actualStatus != "CancelledBeforeExecution")
{
Assert.Fail($"Expected WalkTree to be Cancelled or CancelledBeforeExecution after calling CancelWalkTree, but it was {actualStatus}.");
}
}

[TestMethod]
Expand Down
3 changes: 3 additions & 0 deletions Forge.TreeWalker/src/TreeWalkerSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -714,6 +714,9 @@ internal async Task ExecuteActionWithRetry(
ActionDefinition actionDefinition,
CancellationToken token)
{
// Ensure ForgeActions run in parallel by yielding back the task.
await Task.Yield();

// Initialize values. Default infinite timeout. Default RetryPolicyType.None.
int retryCount = 0;
Exception innerException = null;
Expand Down

0 comments on commit de29125

Please sign in to comment.