Skip to content

Commit

Permalink
Merge pull request #31 from RenanCarlosPereira/cancellation_token_tests
Browse files Browse the repository at this point in the history
Add Unit Tests for Cancellation Token Handling in ExecuteAllRulesAsync and ExecuteActionWorkflowAsync
  • Loading branch information
asulwer committed Jun 30, 2024
2 parents 0bafdaf + 3a5e9b4 commit 8ba9be2
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 5 deletions.
65 changes: 60 additions & 5 deletions test/RulesEngine.UnitTest/BusinessRuleEngineTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Moq;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using RulesEngine.Actions;
using RulesEngine.Exceptions;
using RulesEngine.HelperFunctions;
using RulesEngine.Interfaces;
Expand Down Expand Up @@ -412,7 +413,7 @@ public async Task ExecuteRule_RuleWithMethodExpression_ReturnsSucess(string rule
Func<bool> func = () => true;

var re = GetRulesEngine(ruleFileName, new ReSettings {
CustomTypes = new[] { typeof(Func<bool>) }
CustomTypes = new[] { typeof(Func<bool>) }
});

dynamic input1 = new ExpandoObject();
Expand Down Expand Up @@ -519,10 +520,10 @@ public async Task ExecuteRuleWithIgnoreException_CompilationException_DoesNotRet
public async Task ExecuteRuleWithJsonElement(string ruleFileName)
{
var re = GetRulesEngine(ruleFileName, new ReSettings() {
EnableExceptionAsErrorMessage = true,
CustomTypes = new Type[] { typeof(System.Text.Json.JsonElement) }
});
EnableExceptionAsErrorMessage = true,
CustomTypes = new Type[] { typeof(System.Text.Json.JsonElement) }

});

var input1 = new {
Data = System.Text.Json.JsonSerializer.SerializeToElement(new {
Expand Down Expand Up @@ -809,7 +810,52 @@ public async Task RulesEngineWithGlobalParam_RunsSuccessfully(string ruleFileNam
Assert.True(result[1].IsSuccess);
}

[Theory]
[InlineData("rules12.json")]
public async Task ExecuteAllRulesAsync_WithCancellationToken_CancelsProperly(string ruleFileName)
{
var settings = new ReSettings {
CustomActions = new()
{
{
nameof(ReturnTrueIfCancellationRequestedAction), () => new ReturnTrueIfCancellationRequestedAction()
}
}
};
var re = GetRulesEngine(ruleFileName, settings);
var cancellationTokenSource = new CancellationTokenSource(TimeSpan.FromMilliseconds(500));

var result = await re.ExecuteAllRulesAsync("inputWorkflowReference", cancellationTokenSource.Token, Array.Empty<RuleParameter>());
Assert.NotNull(result);

var exceptionruleOne = result[0].ActionResult?.Exception;
var exceptionruleTwo = result[1].ActionResult?.Exception;

Assert.Null(exceptionruleOne);
Assert.IsType<TaskCanceledException>(exceptionruleTwo?.InnerException);
}

[Theory]
[InlineData("rules12.json")]
public async Task ExecuteActionWorkflowAsync_WithCancellationToken_CancelsProperly(string ruleFileName)
{
var settings = new ReSettings {
CustomActions = new()
{
{
nameof(ReturnTrueIfCancellationRequestedAction), () => new ReturnTrueIfCancellationRequestedAction()
}
}
};
var re = GetRulesEngine(ruleFileName, settings);
var cancellationTokenSource = new CancellationTokenSource(TimeSpan.FromMilliseconds(200));

var actionResult = await re.ExecuteActionWorkflowAsync("inputWorkflowReference", "OneEqualsOneRule", Array.Empty<RuleParameter>(), cancellationTokenSource.Token);

var exception = actionResult.Exception;

Assert.IsType<TaskCanceledException>(exception?.InnerException);
}

[Fact]
public async Task ExecuteRule_RuntimeError_ShouldReturnAsErrorMessage()
Expand Down Expand Up @@ -1177,7 +1223,16 @@ public bool CheckExists(string str)

return false;
}
}

public class ReturnTrueIfCancellationRequestedAction : ActionBase
{
public async override ValueTask<object> Run(ActionContext context, RuleParameter[] ruleParameters)
{
var cancellationToken = context.GetCancellationToken();
await Task.Delay(TimeSpan.FromMilliseconds(400), cancellationToken);
return cancellationToken.IsCancellationRequested;
}
}

}
Expand Down
3 changes: 3 additions & 0 deletions test/RulesEngine.UnitTest/RulesEngine.UnitTest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
<None Update="TestData\rules11.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="TestData\rules12.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="TestData\rules4.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
Expand Down
32 changes: 32 additions & 0 deletions test/RulesEngine.UnitTest/TestData/rules12.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"WorkflowName": "inputWorkflow",
"Rules": [
{
"RuleName": "OneEqualsOneRule",
"ErrorMessage": "One or more adjust rules failed.",
"ErrorType": "Error",
"RuleExpressionType": "LambdaExpression",
"Expression": "1==1",
"Actions": {
"OnSuccess": {
"Name": "ReturnTrueIfCancellationRequestedAction",
"Context": {}
}
}
},
{
"RuleName": "TwoEqualsTwoRule",
"ErrorMessage": "One or more adjust rules failed.",
"ErrorType": "Error",
"RuleExpressionType": "LambdaExpression",
"Expression": "2==2",
"Actions": {
"OnSuccess": {
"Name": "ReturnTrueIfCancellationRequestedAction",
"Context": {}
}
}
}

]
}

0 comments on commit 8ba9be2

Please sign in to comment.