diff --git a/test/RulesEngine.UnitTest/BusinessRuleEngineTest.cs b/test/RulesEngine.UnitTest/BusinessRuleEngineTest.cs index 6682d62c..22f847de 100644 --- a/test/RulesEngine.UnitTest/BusinessRuleEngineTest.cs +++ b/test/RulesEngine.UnitTest/BusinessRuleEngineTest.cs @@ -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; @@ -412,7 +413,7 @@ public async Task ExecuteRule_RuleWithMethodExpression_ReturnsSucess(string rule Func func = () => true; var re = GetRulesEngine(ruleFileName, new ReSettings { - CustomTypes = new[] { typeof(Func) } + CustomTypes = new[] { typeof(Func) } }); dynamic input1 = new ExpandoObject(); @@ -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 { @@ -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()); + Assert.NotNull(result); + var exceptionruleOne = result[0].ActionResult?.Exception; + var exceptionruleTwo = result[1].ActionResult?.Exception; + + Assert.Null(exceptionruleOne); + Assert.IsType(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(), cancellationTokenSource.Token); + + var exception = actionResult.Exception; + + Assert.IsType(exception?.InnerException); + } [Fact] public async Task ExecuteRule_RuntimeError_ShouldReturnAsErrorMessage() @@ -1177,7 +1223,16 @@ public bool CheckExists(string str) return false; } + } + public class ReturnTrueIfCancellationRequestedAction : ActionBase + { + public async override ValueTask Run(ActionContext context, RuleParameter[] ruleParameters) + { + var cancellationToken = context.GetCancellationToken(); + await Task.Delay(TimeSpan.FromMilliseconds(400), cancellationToken); + return cancellationToken.IsCancellationRequested; + } } } diff --git a/test/RulesEngine.UnitTest/RulesEngine.UnitTest.csproj b/test/RulesEngine.UnitTest/RulesEngine.UnitTest.csproj index 7dd46275..65f15c07 100644 --- a/test/RulesEngine.UnitTest/RulesEngine.UnitTest.csproj +++ b/test/RulesEngine.UnitTest/RulesEngine.UnitTest.csproj @@ -30,6 +30,9 @@ Always + + PreserveNewest + PreserveNewest diff --git a/test/RulesEngine.UnitTest/TestData/rules12.json b/test/RulesEngine.UnitTest/TestData/rules12.json new file mode 100644 index 00000000..8b1a78ee --- /dev/null +++ b/test/RulesEngine.UnitTest/TestData/rules12.json @@ -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": {} + } + } + } + + ] +} \ No newline at end of file