forked from microsoft/RulesEngine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIssue596Test.cs
More file actions
82 lines (72 loc) · 2.79 KB
/
Issue596Test.cs
File metadata and controls
82 lines (72 loc) · 2.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using RulesEngine.Actions;
using RulesEngine.Models;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Threading;
using System.Threading.Tasks;
using Xunit;
namespace RulesEngine.UnitTest
{
[ExcludeFromCodeCoverage]
public class Issue596Test
{
private class CountingAction : ActionBase
{
public static int RunCount;
public override ValueTask<object> Run(ActionContext context, RuleParameter[] ruleParameters)
{
Interlocked.Increment(ref RunCount);
return new ValueTask<object>("done");
}
}
private static Workflow WorkflowWithAction() => new Workflow
{
WorkflowName = "wf",
Rules = new[] {
new Rule {
RuleName = "R",
Expression = "true",
Actions = new RuleActions {
OnSuccess = new ActionInfo { Name = "counting", Context = new Dictionary<string, object>() }
}
}
}
};
[Fact]
public async Task AutoExecuteActions_True_RunsActions_DefaultBehavior()
{
CountingAction.RunCount = 0;
var settings = new ReSettings
{
CustomActions = new Dictionary<string, Func<ActionBase>> { { "counting", () => new CountingAction() } }
};
var engine = new RulesEngine(new[] { WorkflowWithAction() }, settings);
var results = await engine.ExecuteAllRulesAsync("wf", "x");
Assert.True(results[0].IsSuccess);
Assert.Equal(1, CountingAction.RunCount);
}
[Fact]
public async Task AutoExecuteActions_False_EvaluatesRulesButSkipsActions()
{
CountingAction.RunCount = 0;
var settings = new ReSettings
{
AutoExecuteActions = false,
CustomActions = new Dictionary<string, Func<ActionBase>> { { "counting", () => new CountingAction() } }
};
var engine = new RulesEngine(new[] { WorkflowWithAction() }, settings);
var results = await engine.ExecuteAllRulesAsync("wf", "x");
// Rule still evaluated...
Assert.True(results[0].IsSuccess);
// ...but the action did NOT run automatically.
Assert.Equal(0, CountingAction.RunCount);
// Caller can still run the action selectively afterwards.
var actionResult = await engine.ExecuteActionWorkflowAsync("wf", "R", new RuleParameter[0]);
Assert.Equal("done", actionResult.Output);
Assert.Equal(1, CountingAction.RunCount);
}
}
}