|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +using RulesEngine.Models; |
| 5 | +using System.Diagnostics.CodeAnalysis; |
| 6 | +using System.Linq; |
| 7 | +using System.Threading.Tasks; |
| 8 | +using Xunit; |
| 9 | + |
| 10 | +namespace RulesEngine.UnitTest |
| 11 | +{ |
| 12 | + [ExcludeFromCodeCoverage] |
| 13 | + public class ParallelRuleCompilationTest |
| 14 | + { |
| 15 | + // Larger than the internal MinRulesForParallelCompilation threshold so parallel mode actually engages. |
| 16 | + private const int RuleCount = 64; |
| 17 | + |
| 18 | + private static Workflow BuildLargeWorkflow() => new Workflow |
| 19 | + { |
| 20 | + WorkflowName = "wf", |
| 21 | + Rules = Enumerable.Range(0, RuleCount) |
| 22 | + .Select(i => new Rule { RuleName = $"R{i}", Expression = $"input1 >= {i}" }) |
| 23 | + .ToArray() |
| 24 | + }; |
| 25 | + |
| 26 | + [Fact] |
| 27 | + public async Task ParallelCompilation_ProducesIdenticalResultsAsSerial() |
| 28 | + { |
| 29 | + var workflow = BuildLargeWorkflow(); |
| 30 | + |
| 31 | + var serialEngine = new RulesEngine(new[] { workflow }, |
| 32 | + new ReSettings { EnableParallelRuleCompilation = false }); |
| 33 | + var parallelEngine = new RulesEngine(new[] { workflow }, |
| 34 | + new ReSettings { EnableParallelRuleCompilation = true }); |
| 35 | + |
| 36 | + var serial = await serialEngine.ExecuteAllRulesAsync( |
| 37 | + "wf", new[] { RuleParameter.Create("input1", 32) }); |
| 38 | + var parallel = await parallelEngine.ExecuteAllRulesAsync( |
| 39 | + "wf", new[] { RuleParameter.Create("input1", 32) }); |
| 40 | + |
| 41 | + Assert.Equal(serial.Count, parallel.Count); |
| 42 | + for (var i = 0; i < serial.Count; i++) |
| 43 | + { |
| 44 | + Assert.Equal(serial[i].Rule.RuleName, parallel[i].Rule.RuleName); |
| 45 | + Assert.Equal(serial[i].IsSuccess, parallel[i].IsSuccess); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + [Fact] |
| 50 | + public async Task ParallelCompilation_PreservesExceptionMessage_WhenRuleExpressionThrows() |
| 51 | + { |
| 52 | + // Inject a deliberately broken rule into a large-enough workflow that the parallel |
| 53 | + // path engages, then assert the per-rule ExceptionMessage explains the underlying |
| 54 | + // failure rather than leaking an AggregateException. |
| 55 | + var rules = Enumerable.Range(0, RuleCount) |
| 56 | + .Select(i => new Rule { RuleName = $"R{i}", Expression = "input1 >= 0" }) |
| 57 | + .ToList(); |
| 58 | + rules[5].Expression = "input1.NoSuchMember.Foo()"; |
| 59 | + |
| 60 | + var workflow = new Workflow { WorkflowName = "wf", Rules = rules }; |
| 61 | + var engine = new RulesEngine(new[] { workflow }, |
| 62 | + new ReSettings { EnableParallelRuleCompilation = true }); |
| 63 | + |
| 64 | + var results = await engine.ExecuteAllRulesAsync( |
| 65 | + "wf", new[] { RuleParameter.Create("input1", 1) }); |
| 66 | + |
| 67 | + var broken = results.Single(r => r.Rule.RuleName == "R5"); |
| 68 | + Assert.False(broken.IsSuccess); |
| 69 | + Assert.False(string.IsNullOrEmpty(broken.ExceptionMessage)); |
| 70 | + Assert.DoesNotContain("AggregateException", broken.ExceptionMessage); |
| 71 | + } |
| 72 | + |
| 73 | + [Fact] |
| 74 | + public async Task ParallelCompilation_FallsBackToSerial_WhenFastExpressionCompilerEnabled() |
| 75 | + { |
| 76 | + // The flag combination is permitted at construction time (back-compat), but the |
| 77 | + // engine declines to parallelize and silently uses the serial path. We can only |
| 78 | + // assert correctness here (results match serial). The fallback itself is |
| 79 | + // observable in benchmarks, not in functional tests. |
| 80 | + var engine = new RulesEngine(new[] { BuildLargeWorkflow() }, |
| 81 | + new ReSettings |
| 82 | + { |
| 83 | + EnableParallelRuleCompilation = true, |
| 84 | + UseFastExpressionCompiler = true |
| 85 | + }); |
| 86 | + |
| 87 | + var results = await engine.ExecuteAllRulesAsync( |
| 88 | + "wf", new[] { RuleParameter.Create("input1", 100) }); |
| 89 | + |
| 90 | + Assert.Equal(RuleCount, results.Count); |
| 91 | + Assert.All(results, r => Assert.True(r.IsSuccess)); |
| 92 | + } |
| 93 | + |
| 94 | + [Fact] |
| 95 | + public async Task ParallelCompilation_FallsBackToSerial_ForSmallWorkflowsBelowThreshold() |
| 96 | + { |
| 97 | + // Below the minimum threshold, the engine declines to parallelize. Verify a |
| 98 | + // 5-rule workflow still works correctly with the flag enabled. |
| 99 | + var workflow = new Workflow |
| 100 | + { |
| 101 | + WorkflowName = "wf", |
| 102 | + Rules = Enumerable.Range(0, 5) |
| 103 | + .Select(i => new Rule { RuleName = $"R{i}", Expression = $"input1 >= {i}" }) |
| 104 | + .ToArray() |
| 105 | + }; |
| 106 | + var engine = new RulesEngine(new[] { workflow }, |
| 107 | + new ReSettings { EnableParallelRuleCompilation = true }); |
| 108 | + |
| 109 | + var results = await engine.ExecuteAllRulesAsync( |
| 110 | + "wf", new[] { RuleParameter.Create("input1", 3) }); |
| 111 | + |
| 112 | + Assert.Equal(5, results.Count); |
| 113 | + // input1=3 means R0..R3 succeed, R4 fails. Same outcome whether serial or parallel. |
| 114 | + Assert.Equal(4, results.Count(r => r.IsSuccess)); |
| 115 | + } |
| 116 | + } |
| 117 | +} |
0 commit comments