Skip to content

Commit de29125

Browse files
authored
Feature/forceparallelactions (#57)
* Force ParallelActions by yielding back the task * Fixing flaky test.
1 parent d18d5ea commit de29125

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

Forge.TreeWalker.UnitTests/test/TreeWalkerUnitTests.cs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -468,17 +468,25 @@ public void TestTreeWalkerSession_WalkTree_ActionWithDelay_CancelWalkTree()
468468
// This gives us time to start WalkTree before calling CancelWalkTree.
469469
this.TestInitialize(jsonSchema: ForgeSchemaHelper.ActionDelay_ContinuationOnTimeout_RetryPolicy_TimeoutInAction);
470470

471-
// Test - WalkTree then CancelWalkTree while WalkTree is running and expect the Status to be Cancelled.
471+
// Test - WalkTree then CancelWalkTree while WalkTree is running and expect the Status to be either:
472+
// 1. CancelledBeforeExecution if TaskCanceledException is thrown from WalkTree. This happens when the ForgeAction gets canceled and honored by Forge.
473+
// 2. Cancelled if OperationCanceledException is thrown from WalkTree. This happens when some other Task wins the race to get cancelled first, such as nodeTimeoutTask.
472474
Task<string> task = this.session.WalkTree("Root");
473475
Thread.Sleep(25);
474476
this.session.CancelWalkTree();
475-
Assert.ThrowsException<OperationCanceledException>(() =>
477+
478+
try
476479
{
477480
string temp = task.GetAwaiter().GetResult();
478-
}, "Expected WalkTree to throw exception after calling CancelWalkTree.");
481+
Assert.Fail("Expected WalkTree to throw exception after calling CancelWalkTree.");
482+
}
483+
catch { }
479484

480485
string actualStatus = this.session.Status;
481-
Assert.AreEqual("Cancelled", actualStatus, "Expected WalkTree to be cancelled after calling CancelWalkTree.");
486+
if (actualStatus != "Cancelled" && actualStatus != "CancelledBeforeExecution")
487+
{
488+
Assert.Fail($"Expected WalkTree to be Cancelled or CancelledBeforeExecution after calling CancelWalkTree, but it was {actualStatus}.");
489+
}
482490
}
483491

484492
[TestMethod]

Forge.TreeWalker/src/TreeWalkerSession.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -714,6 +714,9 @@ internal async Task ExecuteActionWithRetry(
714714
ActionDefinition actionDefinition,
715715
CancellationToken token)
716716
{
717+
// Ensure ForgeActions run in parallel by yielding back the task.
718+
await Task.Yield();
719+
717720
// Initialize values. Default infinite timeout. Default RetryPolicyType.None.
718721
int retryCount = 0;
719722
Exception innerException = null;

0 commit comments

Comments
 (0)